Switch Debouncing and Waiting for a Button Press

For the several years I’ve been working with arduinos I have went thru quite a few variations of switch debouncing. Some I’ve found on line, some I’ve written myself. Most of the ones I’ve found online I felt were kind of complicating for what I needed, but then when I write my own they tend to be too simplistic.

In the past 6 months I have settled on the following procedure. It is fairly simple and, as of yet, I haven’t had a problem with it.

I don’t really know where this procedure originated. I suspect it is partially from the Arduino cookbook with probably a tweak or two of my own.

// Get current keypad signal, debouncing. TRUE is returned when key is pressed
// which is reverse of the actual signal due to pullup resistors.

boolean keyDeb(
    int                    pin,
    const int             bounceDelay = 10
    ) {

int                        counter;
boolean                    prevState;
boolean                    state;

prevState = digitalRead(pin);

for (counter = 0; counter < bounceDelay; counter = counter + 1) {
    state = digitalRead(pin);
    if (state != prevState) {
        counter = 0;
        prevState = state;
    } // for

// NOTE: since pullup resistors are used, pin logic is reversed (pressing button returns false). keyDeb
// returns the reverse state of the pin, so logic is what you expect (e.g. pressing button = true);

return !state;

} // keyDeb

To determine is a switch is closed just test for true

if (keyDeb(pin) == true) { do whatever}

keyDeb waits 10ms for the switch to settle. For the switches I’ve used, this has always been acceptable. If you have a particularly noisy switch, you can specify a different bounce delay:

if (keyDeb(pin, 20) = true) { do whatever }

I use momentary contact switches often. When using them,  a simple test, like the above,  isn’t really enough. If I see the switch is pressed, I want to then wait until it is released to execute the code. Otherwise, I’m probably going to execute the code repeatedly until the user released the key. To solve that problem, I use code something like this:

if (keyDeb(pin) == true) {
    while (keyDeb(pin) == true) {}     // wait for the switch to be released
    << do whatever >>

There are some programs where I do quite a bit of user interacting – waiting for them to press a switch to continue. For those instances I use a function I call keyWait:

void keyWait(
    int                    pin
    ) {

while (keyDeb(pin) == false) {}
while (keyDeb(pin) == true) {}

} // keyWait

When I need to wait for them to press the momentary switch connected to pin <p>, I just code:


Just remember, all the code comes to a stop when keyWait is called. If you need to be doing something else while waiting for the user, this is not the procedure for you.



This entry was posted in c-arduino and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s