September 2, 2026
An edge interrupt on a mechanical contact fires several times per press. Bounce usually lasts a few milliseconds, sometimes longer. The simplest fix is to read the input from a timer and accept a new state only after several identical samples in a row.
if (input == stable) {
count = 0;
} else if (++count >= LIMIT) {
stable = input;
count = 0;
event = 1;
}
The rejection time is the polling period multiplied by LIMIT. It has to be longer than the bounce and shorter than the shortest pulse you cannot afford to lose. The quickest way to measure bounce is an oscilloscope in single-shot mode.
If you need an accurate timestamp, combine both methods: hardware capture stores the time of the first edge, and polling confirms it was not a glitch.