Alarm Clock with Tetris to Prove You're Awake

 by nolte919
Featured

Step 5: Button debouncing

My favorite document about debouncing switches is "A Guide to Debouncing " by Jack G. Ganssle. It has lots of great information but the important part (in my opinion) is that you should debounce switches for 50 ms. In other words a button has to be pressed for 50 ms before it’s declared pressed. This is long enough even for the roughest switches but short enough that the human brain can’t detect a delay. You want to check multiple times in that 50 ms period, not just once and then one more time after 50 ms.

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 adsRemove these ads by Signing Up
Pro

Get More Out of Instructables

Already have an Account?

close

PDF Downloads
As a Pro member, you will gain access to download any Instructable in the PDF format. You also have the ability to customize your PDF download.

Upgrade to Pro today!