Debouncing and interrupts vs polling

Most beginner projects that involve button pressing will also recommend including a routine to deal with debouncing the button. As I am now moving on to looking at interrupts to detect a button press, is it still necessary to include a debounce routine?

Scarebird:
interrupts to detect a button press, is it still necessary to include a debounce routine?

Yes but, if your sketch is well structured, you shouldn't normally need to use interrupts to detect slow button presses.

Should you decide to proceed, then read Nick Gammon's notes on interrupts first.

In many cases I have seen a lot of confusion between debouncing and state-change detection.

In general debouncing is only needed if the same button is needed to do two different things. For example if a button just turns something off or sends a servo to 15° it hardly matters if it bounces and sends the same command several times.

Debouncing is probably easier to achieve with polling simply by imposing a short interval between successive calls to the polling function.

...R

In my case, the button press is being used to increment a counter, so debouncing is pretty important to maintain a correct count.

I guess a little experimenting is in order.

To increment a counter, you need debouncing, and even more so if you're using an interrupt, since it will fire many times, very quickly.

For normal user input buttons, don't use interrupts. It just makes everything much more complicated than it has to be, and it's just not necessary.
It's always a good idea to keep your loop non-blocking and short, so you can do things like polling inputs. Don't use delay, use millis for timing (see Blink Without Delay).

This is the code I use for pretty much all simple button inputs in my projects. It's really simple, and really effective. Just put it at the top of your code, paste it in a header (.h) file in new tab (CTRL+SHIFT+N in the IDE) and #include it, or place it in your ~/Arduino/libraries folder in a subfolder as a header file if you need it across multiple sketches.

PushButton.h


---




```
class PushButton
{
public:
PushButton(uint8_t pin) // Constructor (executes when a PushButton object is created)
: pincolor=#000000[/color] { // remember the push button pin
pinMode(pin, INPUT_PULLUP); // enable the internal pull-up resistor
};
bool isPressedcolor=#000000[/color] // read the button state check if the button has been pressed, debounce the button as well
{
bool pressed = false;
bool state = digitalReadcolor=#000000[/color];               // read the button's state
int8_t stateChange = state - previousState;  // calculate the state change since last time

if (stateChange == falling) { // If the button is pressed (went from high to low)
       if (milliscolor=#000000[/color] - previousBounceTime > debounceTime) { // check if the time since the last bounce is higher than the threshold
         pressed = true; // the button is pressed
       }
     }
     if (stateChange == rising) { // if the button is released or bounces
       previousBounceTime = milliscolor=#000000[/color]; // remember when this happened
     }

previousState = state; // remember the current state
     return pressed; // return true if the button was pressed and didn't bounce
   };
 private:
   uint8_t pin;
   bool previousState = HIGH;
   unsigned long previousBounceTime = 0;

const static unsigned long debounceTime = 25;
   const static int8_t rising = HIGH - LOW;
   const static int8_t falling = LOW - HIGH;
};
```

|

Thanks for the PushButton code - works perfectly - better than any debounce library I've tried.

Just scan your switches every 50ms at which time you then look for a change in state.

I know that I risk a huge flame job, but for switches I often just hardware debounce with a 0.1uF cap across the switch.