8 channel relay switching ON after app closes

Hi everyone, so.. this is my first post here and also my first proyect.

so im working on a simple bluetooth controller app where it sends signal to an HC-05 then to an arduino Mega and finally to controll an 8 channel relay module...

All is god up until if i need to force close the app or the bluetooth looses connection due to inactivity...
Some of the relays just switch ON..

i honestly don't know if there is a bug on my code or with the app...

My code:

char Incoming_value = 0;

int Relay1 = 44;
int Relay2 = 42;
int Relay3 = 40;
int Relay4 = 38;
int Relay5 = 36;
int Relay6 = 34;
int Relay7 = 32;
int Relay8 = 30;

void setup() 
  pinMode(Relay1, OUTPUT);       
  pinMode(Relay2, OUTPUT);   
  pinMode(Relay3, OUTPUT);   
  pinMode(Relay4, OUTPUT);   
  pinMode(Relay5, OUTPUT);   
  pinMode(Relay6, OUTPUT);   
  pinMode(Relay7, OUTPUT);   
  pinMode(Relay8, OUTPUT);   

void loop()
  if(Serial.available() > 0)  
    Incoming_value = Serial.read();      
    if(Incoming_value == 'C')             
      digitalWrite(Relay8, LOW); 
         else if(Incoming_value == 'c')       
      digitalWrite(Relay8, HIGH);   
          if(Incoming_value == 'D')                      
      digitalWrite(Relay7, LOW); 
         else if(Incoming_value == 'd')       
      digitalWrite(Relay7, HIGH);   

          if(Incoming_value == 'E')                      
      digitalWrite(Relay6, LOW); 
         else if(Incoming_value == 'e')       
      digitalWrite(Relay6, HIGH);   

          if(Incoming_value == 'F')                      
      digitalWrite(Relay5, LOW); 
         else if(Incoming_value == 'f')       
      digitalWrite(Relay5, HIGH);   

          if(Incoming_value == '1')  
      digitalWrite(Relay1, LOW); 
      digitalWrite(Relay2, LOW); 
      digitalWrite(Relay3, LOW); 
      digitalWrite(Relay4, LOW); 
      digitalWrite(Relay5, LOW); 
      digitalWrite(Relay6, LOW); 
      digitalWrite(Relay7, LOW); 
      digitalWrite(Relay8, LOW); 
         else if(Incoming_value == '0')       
      digitalWrite(Relay1, HIGH); 
      digitalWrite(Relay2, HIGH); 
      digitalWrite(Relay3, HIGH); 
      digitalWrite(Relay4, HIGH); 
      digitalWrite(Relay5, HIGH); 
      digitalWrite(Relay6, HIGH); 
      digitalWrite(Relay7, HIGH); 
      digitalWrite(Relay8, HIGH); 

Dear Ruben,
I think that the problem could be caused by the fact that you use the same Serial line for both HC05 and for the PC monitor: in this case you make a loop, because you send the last character received by the HC05 to both the PC monitor and the HC05 itself. Maybe this causes a local-loop-back and you receive again twice the command.
Normally if you want to monitor the BT line, you should use another line for the BT. If you use the MEGA you have 4 lines, so you can separate them.
Give it a look :slight_smile:
Cheers, Ugo.

if(Serial.available() > 0) // BT incoming line
Incoming_value = Serial.read();
Serial.print(Incoming_value); // Do you want to do an echo toward the HC05 or actuate a monitor on a PC ?
Serial.print("\n"); // if this is the case you shall use another serial line for the HC05

im trying to comprehend what you wrote and it looks like it makes total sense, but i am honestly suuuuper new at this and.... ok well this is what i (think) i understood

basically do an evaluation command where.. if the incoming command is destined to go to the HC-05 then.. send the command otherwise... well not?

is that it?

oh and also i HIGHLY appreciate your help. Thanx a LOT :slight_smile:

Hello Ruben,
what I mean is: the BT incoming line shall be on one serial line of the MEGA (for example Serial1), while the PC monitor shall be on another one (Serial with no numerical extension).
Otherwise when you write to Serial you write to both the HC05 and the PC. I'm not sure that this could cause a loop-back. Try to set something like this:
connect the HC05 Rx and Tx pins to pins respectively 18 and 19 of the MEGA and modify your sketch as follows:

Serial1.begin(9600); // Initialize BT line
Serial.begin(9600); // Initialize PC Monitor line

And modify your "loop" as follows:

if(Serial1.available() > 0)
Incoming_value = Serial1.read();


I've tried to do the same: a MEGA connected to an HC06 on pins 18 and 19 (i.e. Serial1) and one relay on pin 2.
The MEGA is connected to my PC via USB cable (therefore on Serial) for monitoring.
The annexed .aia has three buttons: RELAY1 ON , RELAY1 OFF. and EXIT.
The physical address of my HC06 must be substituted by yours.
Ruben.aia (65.1 KB)
And this is the MEGA code:
Ruben.txt (580 Bytes)
Merry Xmas !!!
Ciao, Ugo.


i see what u did there Thank u so much :slight_smile: <3

and a very merry xmas to u 2

Hi Ruben,
have a great 2021 !
Cheers, Ugo.