Can the Bounce library do multiple switches

Hello all,

Is there a way for the Bounce library to do multiple switches?



I haven't used it for a while (I now use hardware debouncing), but I believe that you just need to declare (instantiate) multiple objects each with their own pins.

Is this the library you are referring to?


No, I don't use a library at all. The reason for using the debounce library is that the software compensates for the mechanical noise that happens when the switch when it is pressed. I use a hardware (RC network) to supress this noise so I get a clean siognal from the switch. No need for any software tricks, I just read the digital input directly.

Debouncing is especially important if you are using interrupts as the bouncing around can cause multiple interrupts to fire off and you really can't rely on a software solution like this library in that case.

joshuabain: Is there a way for the Bounce library to do multiple switches?

Yes, just instantiate more objects.

Bounce bouncer2 = Bounce( 2, 5 ); 
Bounce bouncer3 = Bounce( 3, 5);

Make sure you update all of them.

Here is a simple bit of code I use that was adapted from a PIC project I did years ago. The routine returns quickly if button is not pressed but takes just over debounceDelay time to return if button pressed. My buttons are pulled down so adapt as needed for pullup buttons.
You could expand the code and add a button release loop so routine does not return until button released but I use it in a clock adjustment code where you hold multiple buttons down to adjust time so its needed to return.

const long debounceDelay = 20; //The button debounce time

// Button = pin number to read
int checkButton(int Button) {
  int buttonState = digitalRead(Button);    // Read button
  if (buttonState == HIGH) {                // If button pressed then wait a bit to allow for debounce
    long Time = millis(); 
    while ((millis() - Time) < debounceDelay) {    
    buttonState = digitalRead(Button);      // Read button again
    return buttonState;                     // Return button state
  else {
    return LOW;