Longpress button code

I’m creating a PID sous vide controller, and i found it to easy to turn it on and of with just a button widget. So i created this Long press button. I thought others might find it usefull.
It doesn’t seem to slow down or affect code(but i haven’t done heavy testing, and im no expert :smile:

int longPressMillis = 1000;    // time in millis needed for longpress
boolean buttonState = false

// Blynk On/Off button in app
BLYNK_WRITE(1) {
  if (param.asInt() == 1) {     // if button is pressed
     longPressTimer = timer.setTimeout(longPressMillis, onOffToggle);  // start the timer to run the function in defined time
     } else {
       timer.deleteTimer(longPressTimer);  // Stop and delete the timer if button is released to early
     }
   }
    

// Function called by BLYNK_WRITE(1)
// This function toggles something
void onOffToggle() {
  if (buttonState == true) {
    // do something if true
    buttonState = false;
  } else {
    // do something if not true
   buttonState = true;
  }
}
8 Likes