I am monitoring a pushbutton with a microcontroller where I am trying to take care of four things together:
- 50-msec debounce upon push-begin and 25-msec debounce upon push-release
- identify a short-press, defined as when button released within < 1 second
- identify a long-hold, defined as when 1 second passes since button push-begin
- sleep as much as possible when not doing anything else
Below is a short pseudocode of what I have implemented so far. I think it covers all of these cases.
Do you see any possible refinements or potential issues? (E.g., I am interested in any subtle cases that might be blindspots for my approach.)
Pseudocode:
Main loop {
Sleep
}
Falling-Interrupt {
Disable Falling-Interrupt
Enable 50-millisecond-Debounce-Timer-Interrupt
}
50-millisecond-Debounce-Timer-Interrupt {
if PushButton state is still LOW {
Enable Rising-Interrupt
Enable 1000-millisecond-Hold-Timer-Interrupt
}
}
1000-millisecond-Hold-Timer-Interrupt {
Register as Pushbutton long-hold
}
Rising-Interrupt {
if (Time since Falling-Interrupt < 1000 millisecond) {
Register as Button Short-press
}
Disable 1000-millisecond-Hold-Timer-Interrupt
Enable 25-millisecond-Debounce-Timer-Interrupt
}
25-millisecond-Debounce-Timer-Interrupt {
Enable Falling-Interrupt
}