Dashboard onChange events queued

Hi, Newbie here.
I've recently started with the Arduino cloud and managed to connect and sync code to my ESP8266. I figured I'd make a garage door opener. On my dashboard I have a push button and a status widget. the code works as expected, I receive the onchange events at my ESP8266.
My problem is this, if I fat finger the pushbutton lets says 4 times then I get 4 onChange events. Does anybody have any thought on how I could sort of debounce the button or throw away events created too quickly.
TIA

Hi @lostbits.

If you set a timestamp variable when an action is taken in the callback function, you can check whether a sufficient interval has passed since the last time the action was taken.

For example:

unsigned long buttonTimestamp;

void onButtonChange() {
  if(millis() - buttonTimestamp > 2000) {  // Only take action if more than 2000 ms have passed since the last time
    // Code for whatever action to perform when there is a legitimate button press
    buttonTimestamp = millis();
  }
}

Thank you. I'll give it a try.
Edit.
Ha. So simple but works a treat.

You are welcome. I'm glad if I was able to be of assistance.

Regards,
Per

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.