[SOLVED] Change status on the button widget code

Hello to all (excuse my english google translate)

I can not understand where I’m wrong …
I am creating an alarm system for my house, to activate or deactivate the system using an RFID reader and even Blynk of course !.

I wrote two functions and are called to time with the library SimpleTimer, one turns on and off with a button on Blynk and the other turns on and off with the RFID tag. Used together can not make them work.
Are so simplified:
button widget virtual pin5

////////////////////// LOGIC BLYNK/////////////////////////////

BLYNK_CONNECTED() {
  if (isFirstConnect) {
    Blynk.syncAll();
  }
}

BLYNK_WRITE(V5){ button = param.asInt(); }

void logicaBLYNK(){
if ((button == HIGH)&&(stateAllarm == 0)) {
Serial.println(“allarm on”);
activateAllarm();
} else if ((button == LOW)&&(stateAllarm == 1)) {
Serial.println(“allarm off”);
deactivateAllarm();
}
}

  ////////////////////////////// LOGIC RFID/////////////////////////////
if (((tag1== codeRead)||(tag2 == codeRead))&&(stateAllarm == 0)) {
          Blynk.virtualWrite(V5, HIGH);
          Serial.println("allarm on");
          activateAllarm();
} else if (((tag1 == codeRead)||(tag2 == codeRead))&&(stateAllarm == 1)) {
          Blynk.virtualWrite(V5, LOW);
          Serial.println("allarm off");
          deactivateAllarm();
}
}

with this code if I use Blynk smartphone works ok, but if such active with RFID tags alarm alarm turns on and turns off immediately.

Example: active RFID tags with alarm, is activated, then run the “logicaBLYNK” and located in the LOW and then immediately off the alarm button was.
When enabled with RFID tag performs “Blynk.virtualWrite (V5, HIGH);” because then you see the state LOW?

On smatrphone Blynk you see that the button that lights up / lights but the state remains LOW? changes state only if it is pressed from the phone app …
what I did not understand? sorry but I’m a beginner …
Thanks bye

I think you need to catch alle the cases in 1 function and do your logic there. I see you now have a separate function for the Blynk logic and one for the RFID logic. Are all your variables declared global? That would make life also better of course.

If you do the Blynklogic you should also know the state of the rfid and the other way around.

You could use switch for that, it may be more readable:

switch(alarmState)
// Alarm is off
case 0:
    if(button == 1) // Blynk controlled alarm on
    {
         if(rfid == 1) // rfid controlled
         {
              activateAlarm();        
        exit(); // this leaves the case and other code not executed
        }
        activateAlarm();
}

And so on :slight_smile:

Thank Lichtsignaal advice was helpful I solved thanks again!
:slight_smile:

You’re welcome! I’m looking forward to seeing the final project in actio. :slight_smile: