BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Create a safety button

As a title, to avoid accidental pressures, i need to create a button mode push with a delay of 3 seconds before start the code, and break if the time is less of 3 seconds.
I try with this sketch but there is something wrong

long currentMillis = 0;
int periodo = 3000;
long startMillis = 0;

 BLYNK_WRITE(V1) {
 int state = param.asInt();
 bool premuto = false;
 if (state == 1) {
 premuto = true;
 startMillis = millis();
 currentMillis = startMillis;
 while (currentMillis < (startMillis + periodo)) {
    currentMillis = millis();
    if (param.asInt() == 0) premuto = false;
    else premuto = true; 
 } 
if (currentMillis >= startMillis && premuto)
  digitalWrite(15, LOW);
}
else
  digitalWrite(15, HIGH);
}

did you seen my post ?

Not exactly, I don’t have to protect it from a password, I have to protect it from accidental pressure

yes that the way I use it .
slider is locked until I press the button !
Video_00460

Where is the example sketch of this gif?

Here is my example of a safety switch using a slider… I will eventually get around to making one for a button that requires a long press… but probably not before Blynk 2.0 is released, thus nullifying the need anyhow as it will apparently have such a feature? :stuck_out_tongue:

1 Like

thanks, why didn’t I think of it before …?:sweat_smile:

@Andrewa80

else you can do that too, press 2 sec to unlock any buttons!

Video_2019-05-13_181245 2019-05-13_183235

/**************** LOCK UNLOCK **************/
BLYNK_WRITE(V50) {  // button to be held down to activate
    if (param.asInt() and  !LongHold ) {
    ButtonTimer = timer.setTimeout(2000, LongHoldDetect); // press 2 sec
    ButtonPressed = true;
    
// Button has been released
  } else {
    ButtonPressed = false;        // Reset press flag
 
    // If the long hold function wasn't called, it's a short press.
    if (!LongHold) {
      timer.deleteTimer(ButtonTimer);   // Kill the long hold timer if it hasn't been activated.

      // Reset the long press flag
      LongHold = false;
      greenLed.off();
      redLed.on();
    }
  }
}

// Checks for long press condition on SETTINGS button
void LongHoldDetect() {
  // If the button is still depressed, it's a long hold
  if (ButtonPressed) {
    LongHold = true;
    greenLed.on();
    redLed.off();
     }
}

BLYNK_WRITE(V51) {  // Reset button
  if (param.asInt()) {
    if (LongHold = true) {
      ButtonPressed = false;
      LongHold =  false;
      greenLed.off();
      redLed.on();
        }
  }
}