Arduino - Bluetooth - Programming problem

Hi everyone,

My project works, when I switch on the controller the red LED turn on next when I press the "button" in the app I have a green and yellow light without red. I have a problem with arduino and bluetooth. I would like do the project when I will lost the signal I want that the red LED turn on automatically. What should I do ? I send you my program in IDE.

#include <SoftwareSerial.h>

SoftwareSerial bluetooth(10, 11);


int bt; //Bluetooth
int L1=2; //Green LED
int L2=3; //Red LED
int L3=4; //Yellow LED

void setup() { 
  pinMode(L1,OUTPUT); 
  pinMode(L2,OUTPUT);
  digitalWrite(L2,HIGH); //Red LED
  pinMode(L3,OUTPUT);
  Serial.begin(9600);
}

void loop() {
  if(Serial.available() > 0){
    bt = Serial.read();}

  if (bt == "on") {
    digitalWrite(2,HIGH); //Green LED
    digitalWrite(3,LOW); //Red LED
    digitalWrite(4,HIGH);} //Yellow LED
   else if (bt == '2'){
    digitalWrite(L1,LOW);}
    
  if (bt == "off") {
    digitalWrite(2,LOW); //Green LED
    digitalWrite(3,HIGH); //Red LED
    digitalWrite(4,LOW);} //Yellow LED
   else if (bt == "on"){
    digitalWrite(L2,LOW);}
  }

I thought about diffrent code with "state", but I don't know how I do write it. Or sb knows how the program should be write.

That can never work - read returns a single character.

Please remember to use code tags when posting code

Please follow the advice given in the link below when posting code , use code tags and post the code here to make it easier to read and copy for examination

Now should be ok :smiley:

Could you explain ?

"on" is a string - it occupies three bytes (or chars)

Serial read returns a single char, encoded in an int.

You cannot compare a single char to a string, even if the string contains only a single character.

If I will use only string "state" ?

Also, even if you had a string, you couldn't compare it with another string using "==", you would have to use strcmp.
Or (INCOMING!) use Strings.

Could you show me on a program how and where write it ?

You said the project works.

Yes but this last part about lost signal not.

How can the code you posted be said to be working?
Have you tested it thoroughly?

It looks like you are using a Serial-Bluetooth module. So, you application does not even about Bluetooth. I see two options.

You figure out how to talk to the Bluetooth module and make use low level functions to get the connection status or you send data on the application layer on a regular basis to allow the receiver to detect when the data is no longer received.

How do I do ?

Because it work on about 97 % because I can't "restart" bt( come back to only red LED turn on), after losing the signal.

Yes I've tested.

Can you just take another look at the code you posted, please?

I think you posted some other code, rather than the one you've described

Whenever you do not send any information in your sketch you will need to send some short "keep alive" message. Something that is otherwise ignored. Then you write some code that will measure when the last message was received. If the time is too long you set your LED.

I dont' get it.

Are you using an HC05 module?

There is a good discussion of using the state pin to detect connected status in this thread
https://forum.arduino.cc/t/hc-05-state-pin/573388

Yes I'm using HC-05.

I saw this discussion, and I tried but it doesn't work.