2

I'm trying to control a relay both via Bluetooth and using a switch. The problem is that, when the LED is off, I can turn it on with the switch but then if I turn off the switch the relay will stay on. Is there something wrong with my code? Thanks in advance. The switch is installed as follows: Switch

Here is my code: Switch is connected to pin 3 and the relay is connected to pin 2.

//bluetooth hc-06
int ledPin = 2; // usamos un pin de salida al LED
int state = 0; // Variable lectrura serial
int interruptor=3;

void setup() {
    pinMode(ledPin, OUTPUT);   //Declara pin de Salida
    pinMode(interruptor, INPUT);
    digitalWrite(ledPin, LOW); //Normalmente Apagado
    Serial1.begin(9600);
    }

void loop() {
  int valor=digitalRead(interruptor);

  if(Serial1.available() > 0){
       state = Serial1.read();
  } // esta parte del código es para solo 1 Carácter o Unidad. 


 if (state == '0'&& valor== LOW) {
    digitalWrite(ledPin, LOW);
    Serial1.println("LED: off");
 }


 else
 if (state == '1' || valor== HIGH) {
     digitalWrite(ledPin, HIGH);
     Serial1.println("LED: on");
 }


}

New code:

//bluetooth hc-06
int ledPin = 2; // usamos un pin de salida al LED
int state = 1; // Variable lectrura serial
int interruptor=3;

void setup() {
    pinMode(ledPin, OUTPUT);   //Declara pin de Salida
    pinMode(interruptor, INPUT);
    digitalWrite(ledPin, LOW); //Normalmente Apagado
    Serial1.begin(9600);
    }

void loop() {
  int valor=digitalRead(interruptor);
 //si el modulo ha mandado dato, guardarlo en estado.

  if(Serial1.available() > 0){
       state = Serial1.read();
  } // esta parte del código es para solo 1 Carácter o Unidad. 

 // si el estado es 0 ese sería Apagado “OFF”
 if (valor== LOW) {
    digitalWrite(ledPin, LOW);
    Serial1.println("LED: off");
 }

 // de lo contrario si el estado es 1 ese sería Encendido “ON”
 else
 if (valor== HIGH) {
     if (state== 1){  
     digitalWrite(ledPin, HIGH);
     Serial1.println("LED: on");
     }
     else if (state== 0){
     digitalWrite(ledPin, LOW);
     Serial1.println("LED: off");

     }
 }


}
  • 1
    The circuit is working properly when using bluetooth commands, in case that helps. – Daniel Navarro Dec 22 '18 at 13:17
  • Should the switch work independently from the bluetooth command on the relay? Currently you can only turn it on with state='1' AND switch on, and you can only turn it off with state='0' AND switch off. It is not completely clear, what the desired behavior is. Please explain further, what the code should do. – chrisl Dec 22 '18 at 13:27
  • @chrisl: No you can turn it on with state='1' OR switch is closed. That's why he can switch the LED on with the switch but not off. – PimV Dec 22 '18 at 13:48
  • Okay, I modified my code so I can only control via bluetooth when the switch is ON, however it is still not working... – Daniel Navarro Dec 22 '18 at 16:04
  • 1
    You might be better off with boolean state = true, then using state = (bool)Serial1.read() and 'if (state)...' There's nothing shown here that guarantees Serial1.read()` will return a value of 1 – Jim Mack Dec 22 '18 at 16:30
0

You have to invalidate the ‘state’ variable when Serial is not available, otherwise you will produce strange behaviours.

if(Serial1.available() > 0){ state = Serial1.read(); } else { state =-1; // not valid }

Also, you have to check ‘1’ and ‘0’ characters and not 1 and 0 values. ‘if (state == ‘1’)..’

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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