Step 5: Button debouncing
The interrupt happens every 4.1 ms (see Step 4) and I wait for 8 consecutive reading of the button open or closed to declare the button pressed or not. So the button has to be pressed or released for 8 x 4.1 ms or 32.8 ms. I handle this by shifting an unsigned 1-byte variable to the left by 1 and putting the current state of the button in the least significant bit. If the resulting variable is 0xFF I declare it pressed and if it's 0x00 I declare it not pressed. Also, you don't want multiple button presses to register if the switch is held longer than the 32.8 ms so the state of the button has to be currently not pressed to declare it pressed and vice versa.
I also increment a variable to keep track of the total button presses and the number of particular button presses. These variables are decremented when the button is taken care of in the standard loop() function. This way button presses are recorded immediately but the code that actually does something useful is not in the interrupt routine bogging it down. An interrupt routine should be kept as short as possible.
There are other, perhaps simpler, ways to debounce switches but I have found this way quite robust, reliable and user friendly in a variety of applications.
Remove these ads by
Signing Up

































Not Nice















Visit Our Store »
Go Pro Today »



