[Solved] Button in Push Mode

I have a button on the app on V3 pin in PUSH mode. I try this code and i go crazy…

BLYNK_WRITE(V3) {
  
  int SwitchStatus = param.asInt();
  if (SwitchStatus == 1) {
    digitalWrite(relay1, LOW);
    lock_v3 = 1;
    Serial.println("on");
    Serial.println(lock_v3);
   }
  else
    digitalWrite(relay1, HIGH);
    lock_v3 = 0;
    Serial.println("off");
    Serial.println(lock_v3);
 }

The result on serial monitor is this:

Connecting to Telecom-67470869
[30840] Connected to WiFi
[30840] IP: 192.168.1.7
[30840] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.4.7 on NodeMCU

[30912] Connecting to 192.168.1.3:8442
[31031] Ready (ping: 1ms).
off
0         <---- Initial Sync

on      |
1        | 
off      | Long pression on button
0        |

off    
0        <---- Release button

Why button pressed and hold, bypass ELSE condition?

What do you expect happens when you push a button in PUSH mode that has 2 states?

Press the button = 1
Release button = 0

Simplfy your code and report the serial montior back here:

BLYNK_WRITE(V3) {
    lock_v3 = param.asInt();
    Serial.println(lock_v3);
 }
1 Like

You code is wrong. You are missing brackets for your else statement.

Fixed below

if (SwitchStatus == 1) {
    digitalWrite(relay1, LOW);
    lock_v3 = 1;
    Serial.println("on");
    Serial.println(lock_v3);
   }
  else
   {     // <- missing 
    digitalWrite(relay1, HIGH);
    lock_v3 = 0;
    Serial.println("off");
    Serial.println(lock_v3);
    }// <- missing 
2 Likes

Thanks @Fettkeewl damn brackets :joy:

1 Like