Tuning in late, sry. I like your diagram, but I have said elsewhere that switches are a special case and that
that is to say that a press bounces but will ultimately settle closed and a release bounces but will settle open. If your switches do either, get new switches.
So the pattern I use immediately reports the event of being pressed or released indicated by the first beginnings of the transition, then ignores the switch for the bounce period.
Your debouncing is more robust and might be employed to deglitch a signal where there are transitions outside those around human acts.
Like a noisy digital signal from a receiver.
Some switches might open briefly, such as an alarm switch on a window, what could very briefly open do to rattling from the wind or a truck dribing by.
if the debounce period has not elapsed, return.
if the button differs from the internal state
raise the appropriate flag (press or release event)
change the internal state and
start the lockout debounce period
I've dressed it up many ways, including going all C++ on it to the very limited extent of my ability with the features it has that C does not.
Usually though it's as simple as the pseudocode above.
This is the update method of a half-baked C++ button object, it is meant to be called at the top of the loop function for all button objects:
void update()
{
// unsigned long now = millis(); // usually I have a global "now"
if (now - lastTime < DEBOUNCE) return;
unsigned char raw = !digitalRead(thePin); // 1 = pressed, button pulled up.
if (state != raw) {
if (raw) iPress = 1;
else iRelease = 1;
state = raw;
lastTime = now;
}
}
In the wokwi, I cheat and turn off the simulated bounce.
IRL I have used a small capacitor and debounce thereby in hardware. Not very often, but it works well and affords instant switch down recognition and a small constant delay on switch up reporting.
I don't like any method that delays switch down reporting, and I don't like techniques that take a non-constant delay to report.
I use interrupts when they are unavoidable. I can't recall using one for a pushbutton.
a7