-1

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 = "";
    }
  }
}
5
  • What is the output of the serial terminal? That is exactly what is reaching the code, so this would help alot. I will update my answer, when you provide more information. Also: How have you connected the HC-05? Did you use a voltage divider for it's RX line? (The HC-05 works with 3.3V and most Arduino boards with 5V) And the if-statement 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
  • the serial monitor outputs "on" as 'o' and 'n' in separate lines. bluetooth terminal on my phone doesn't print anything. But the code works fine if I am using the normal (Rx,Tx) pins, the terminal on my phone also prints the strings it sends,that works fine even without the voltage divider. I just don't want to engage the pins(1,2). I don't know if there is a problem with the SoftwareSerial library – Sudhanshu Shivam Jan 24 '18 at 11:04
  • 1
    the chars come slower over SoftwareSerial and your code doesn't wait for them. if next char is not available immediately you continue in loop – Juraj Jan 24 '18 at 12:15
  • please elaborate I tried while(blurtooth.available()==0); to wait for the data but that is not working; – Sudhanshu Shivam Jan 24 '18 at 13:55
  • I was basically getting the problem due to some garbage values flowing in and I managed that by using if(message.substring(0,2).equals("on")). and now it works fine – Sudhanshu Shivam Jan 24 '18 at 19:18
0

Your logic is a complete mess. You have:

  1. If a character is available, read it and add it to message.
  2. If another character is available, compare what you have already stored with on.
    • If it's equal ('o' is not equal to 'on') turn on, otherwise turn off.
    • Print what you have stored.
  3. Forget what you stored
  4. Go to 1 (a character is still available since you never read it in stage 2).

Instead you need something more like:

  1. If a character is available, read it into a variable.
  2. If the character you read is a line ending character:
    • Compare your message with 'on' and take appropriate action
  3. If the character you read is not a line ending character:
    • Add the character to message.
  4. Go to 1.

Note how you only check once if there is a character available, and all your testing is done within that one if block.

3
  • so i basically tried that but it's not working – Sudhanshu Shivam Jan 24 '18 at 14:00
  • 1
    Comments are not places to dump code. – Majenko Jan 24 '18 at 14:01
  • really srry about that – Sudhanshu Shivam Jan 24 '18 at 14:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.