Need help troubleshoot Debounce lib

Hi, it’s been several days trying to figure out my debounce problem, and i am stuck. i was trying to do multiple switches debounce. at first, i looked adafruit codes here, but since the codes looks incomplete, i used another codes from here.

to my surprise, it doesn’t work. the bounce or the noise still caught up by arduino.

i modified the code a bit.

header file:

#ifndef debounce_h
#define debounce_h

#include "Arduino.h"

class debounce
    debounce(byte (&switchArray)[7]);
    void check_switches(byte (&switchArray)[7], byte (&switchStatus)[7]);
    byte _switchprevstate[7];
    byte _switchcurrstate[7];
    byte _switchflipstate[7];
    unsigned long _lastDebounceTime;


source file:

#include "Arduino.h"
#include "debounce.h"
debounce::debounce(byte (&switchArray)[7])
  byte i;
  for (i=0; i< 7; i++) 
    pinMode(switchArray[i], INPUT);
    digitalWrite(switchArray[i], HIGH);
    _switchprevstate[i] = HIGH;
    _switchflipstate[i] = HIGH;
void debounce::check_switches(byte (&switchArray)[7], byte (&switchStatus)[7])
  byte index;
  unsigned long temp;
  temp = millis();
  if (temp < _lastDebounceTime) _lastDebounceTime = millis();
  if ((temp - _lastDebounceTime) > 50)
    for (index = 0; index < 7; index++)
      switchStatus[index] = 0;
      _switchcurrstate[index] = digitalRead(switchArray[index]);
      if (_switchcurrstate[index] == _switchprevstate[index])
        if (_switchcurrstate[index] != _switchflipstate[index])
          _switchflipstate[index] = _switchcurrstate[index];
          if (_switchcurrstate[index] == LOW) switchStatus[index] = 1;
      _switchprevstate[index] = _switchcurrstate[index];
    _lastDebounceTime = millis();

using it in the main:

byte switches[7] = {39,40,41,42,43,44,45};
byte swstat[7];
debounce mydebounce(switches);

void loop()
  mydebounce.check_switches(switches, swstat);
  if (swstat[1]) //do count up and display

at one time, it worked. but i felt the switch respon is rather slow. so, i changed the debounce delay from 50 to 10. compiled and uploaded, things don’t work. each switch contact giving around 10 counts. so i changed it back to 50, and wierdly, it still don’t work. now it counts around 50 contacts for one switch contact.

actually, when i used the adafruit codes, this is similar symptom. when i set the debounce constant to 50, the counts will be around 50 or so. if i set to 100, it will follow to around 100. this is bizarre, and i can not really think my logic out of this. my logic said if the constant is greater, then there should be no noise caught up at all. instead, this is vise versa effect.

also lately, i tried the Bounce2 library, it also didn’t work. btw, i used arduino mega 2560. the IDE version is 1.0.6. i don’t use late version because they’re really buggy.

could someone test my codes here? i am really curious, is there really problem with my arduino hardware or else?

thank you, and sorry for long post.

I just use Mr. Gammon's switch manager: From his thread:


Thanks, that lib is working out of the box. i guess the bug is in my code.