I have some examples of timers here…
- C++ Blynk - Code Examples for Basic Tasks (Work in Progress) Timers
- C++ Blynk - Code Examples for Basic Tasks (Work in Progress) Lambda format timers
Something like this should work for you (untested)…
int doorTimer = 0;
BLYNK_WRITE(V12) // Garage Door # 2 Button set as switch
{
if (doorTimer == 0) { // Run if Flag is LOW
doorTimer = 1; // Set Flag
// Timed Lambda Function - Flag reset
timer.setTimeout(30000L, []() { // Run Flag reset in 30 seconds, meanwhile carry on...
doorTimer = 0;
}); // END Timer Function
if (param.asInt() == 0) { // Run if state of V12 is LOW (and Flag is LOW)
digitalWrite(12, LOW); //GPIO12
}
else { // Run if state of V12 is HIGH (and Flag is LOW)
digitalWrite(12, HIGH); // GPIO12
}
}
}
However it will not take into consideration what the current button/switch state is, so it is possible to switch from one state to another within the 30 seconds, and get stuck “reopening” an open door for another 30 seconds, etc.
…
A better way would be more logic or different way of triggering (momentary button to trigger another door state flag to open if closed or close if opened, etc.)