I am trying to control the LED on pin13 using the below code but it's not working. With this code I am able to print to the Serial Monitor but the code has no effect on the any of the digital pins. I am unable to control the LED and so I have gotten stuck and can't advance further. I can't understand why it is happening even if I have excluded the new line from the Bluetooth app that I am using to send the strings and same on the Serial Monitor.
#include<SoftwareSerial.h> // import the serial library
SoftwareSerial bluetooth(6, 7); // RX, TX
void setup(){
Serial.begin(9600); //set baud rate
bluetooth.begin(9600);
pinMode(13, OUTPUT);
digitalWrite(13, LOW);
}
void loop(){
while (bluetooth.available()){
char ch = bluetooth.read();
message += ch;
}
if(!bluetooth.available()){
if(message != ""){
if(message.equalsIgnoreCase("on")){
digitalWrite(13, HIGH);
}else{
digitalWrite(13, LOW);
}
Serial.println(message);
message = "";
}
}
}
if(!bluetooth.available())
isn't necessary here, because when you are leaving the while loop there is nothing available, unless it is received on this few clock cycle of the execution between. – chrisl Jan 24 '18 at 9:33