Arduino Code for Bluetooth modules
/*BLUETOOTH SLAVE HC-06*/ #include <SoftwareSerial.h> const int rxPin = 4; //SoftwareSerial RX PIN, connect to HC-06 TX PIN const int txPin = 2; //SoftwareSerial TX PIN, connect to HC-06 RX PIN //level shifting to 3.3V maybe needed SoftwareSerial mySerial(rxPin, txPin); //RX; TX const int ledPin = 13; //LED connected to digital pin 13 of slave Arduino int state = 1; //if state is 1, the LED is TURN ON //if state is 0, the LED is TURN OFF int flag = 0; //flag to prevent duplicate messages void setup() { pinMode(ledPin, OUTPUT); mySerial.begin(9600); digitalWrite(ledPin, LOW); //LED is initially TURN OFF } void loop() { //read serial input and saves it in the state variable if(mySerial.available() > 0) { state = mySerial.read(); flag = 0; //clear flag } //if the state is '0' the LED is TURN OFF if(state == '0') { digitalWrite(ledPin, LOW); if(flag == 0) { mySerial.println("Slave LED: OFF"); flag = 1; } } //if the state is '1' the LED is TURN ON else if(state == '1') { digitalWrite(ledPin, HIGH); if(flag == 0) { mySerial.println("Slave LED: ON"); flag = 1; } } }