Advertisement
gregwa

FCM120 - Bluetooth HC06.ino

Apr 2nd, 2017
368
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.14 KB | None | 0 0
  1. #include <SoftwareSerial.h>
  2.  
  3. int bluetoothTx = 2; // TX-O pin of bluetooth mate, Arduino D2
  4. int bluetoothRx = 3; // RX-I pin of bluetooth mate, Arduino D3
  5.  
  6. int led = 13;
  7.  
  8. int buttonPin1 = 7;
  9. int buttonPin2 = 8;
  10. int button1State = 0;
  11. int button2State = 0;
  12.  
  13. int dataFromBt;
  14.  
  15. boolean lightBlink = false;
  16.  
  17. SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
  18.  
  19. void setup()
  20. {
  21.      Serial.begin(9600); // Begin the serial monitor at 9600bps
  22.      
  23.      bluetooth.begin(115200); // The Bluetooth Mate defaults to 115200bps
  24.      bluetooth.print("$"); // Print three times individually
  25.      bluetooth.print("$");
  26.      bluetooth.print("$"); // Enter command mode
  27.      delay(100); // Short delay, wait for the Mate to send back CMD
  28.      bluetooth.println("U,9600,N"); // Temporarily Change the baudrate to 9600, no parity
  29.      // 115200 can be too fast at times for NewSoftSerial to relay the data reliably
  30.      bluetooth.begin(9600); // Start bluetooth serial at 9600
  31.      pinMode(led, OUTPUT);
  32.      pinMode(buttonPin1, INPUT);
  33.      pinMode(buttonPin2, INPUT);
  34. }
  35.  
  36. void loop()
  37. {
  38.      if(bluetooth.available()) // If the bluetooth sent any characters
  39.      {
  40.          // Send any characters the bluetooth prints to the serial monitor
  41.          //Serial.println((char)bluetooth.read());
  42.      
  43.          dataFromBt = bluetooth.read();
  44.      
  45.          if(dataFromBt == '1'){
  46.             Serial.println("led on");
  47.             digitalWrite(led, HIGH);
  48.             //bluetooth.print("1");
  49.             bluetooth.println("Temp: 105.32");
  50.          }
  51.          if(dataFromBt == '0'){
  52.             Serial.println("led off");
  53.             digitalWrite(led, LOW);
  54.             //bluetooth.print("0");
  55.             bluetooth.println("Humidity: 100.0");
  56.          }
  57.          if(dataFromBt == 'b'){
  58.             Serial.println("a");
  59.             lightBlink = true;
  60.             }else{
  61.             lightBlink = false;
  62.          }
  63.      
  64.      }
  65.      
  66.      if(Serial.available()) // If stuff was typed in the serial monitor
  67.      {
  68.         // Send any characters the Serial monitor prints to the bluetooth
  69.         bluetooth.print((char)Serial.read());
  70.      }
  71.      
  72.      // and loop forever and ever!
  73.      if(lightBlink){
  74.         digitalWrite(led, HIGH);
  75.         bluetooth.print("1");
  76.         Serial.println("HIGH");
  77.         delay(500);
  78.         digitalWrite(led, LOW);
  79.         bluetooth.print("0");
  80.         Serial.println("LOW");
  81.         delay(500);
  82.      }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement