2017年1月12日 星期四

藍芽

HC-06 藍芽模組的設定

http://opensource.ntpc.edu.tw/moodle/mod/url/view.php?id=4992
 http://gsyan888.blogspot.tw/2014/03/arduino-hc-06-at-command.html 

#include  <SoftwareSerial.h>
SoftwareSerial BTSerial(8, 9); //   TX|RX
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial.println("BTSerial is ready");
  BTSerial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
   if (Serial.available())
   {
    BTSerial.write(Serial.read());
   }
   if (BTSerial.available())
   {
    Serial.write(BTSerial.read());
   }
}
=============================
AT ==>OK
AT+VERSION  ==>1.8
=============================
98:D3:31:FD:2F:D5----------pin
1234----------------------------password
===================================
#include  <SoftwareSerial.h>
SoftwareSerial BTSerial(8, 9); //   TX|RX
int led1=5;
int led2=6;
int led3=7;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial.println("BTSerial is ready");
  BTSerial.begin(9600);

  pinMode(led1,OUTPUT);
  pinMode(led2,OUTPUT);
  pinMode(led2,OUTPUT);
}
void cleared()
{
  digitalWrite(led1,LOW);
  digitalWrite(led2,LOW);
  digitalWrite(led3,LOW);
}
void loop() {
  // put your main code here, to run repeatedly:

   if (Serial.available())
   {
    BTSerial.write(Serial.read());
   }
   if (BTSerial.available())
   {
     char key=BTSerial.read();
   
      if (key=='L'||key=='l')
      {
        cleared();
      }
      else if(key=='F'||key=='f')
      {
        cleared();
        digitalWrite(led1,HIGH);
       }
       else if(key=='R'||key=='r')
      {
        cleared();
        digitalWrite(led2,HIGH);
      }
      else if(key=='B'||key=='b')
      {
        cleared();
        digitalWrite(led3,HIGH);
      }
   }
}
==================================
Arduino Libraray自訂函數庫;
1
========================
//放在library Serialcom3資料夾 Serialcom3.h
#ifndef Serialcom3_h
#define Serialcom3_h

class Serialcom3
{
public:
int fun1(int x,int b);
void fun2(int a,int y);
};
#endif
  ======================
Serialcom3.cpp
#include<Serialcom3.h>
#include<Arduino.h>

int Serialcom3::fun1(int x,int y)
{
int total=x+y;
return total;
}
void Serialcom3::fun2(int a,int b)
{
Serial.begin(9600);
        Serial.println(a+b);
}
=============================

沒有留言:

張貼留言