Simple Button Debounce Library

I'm using this for debouncing my microswitches. It's my first library XD

I hope someone else can find some use for it too.

Reflecting Improvements suggested by robtillaart & PaulS

// Usage : Button <YOUR_BUTTON_NAME> ( <PIN_#> , <DEBOUNCE_TIME>, <PIN_MODE> );
DebounceButton testButton (9, 50, INPUT);
DebounceButton testButton2 (10, 100, INPUT_PULLUP);

void setup() {

// --- Setup your buttons --- //
testButton.setupButton();      // basically an automatic "pinMode(buttonPin, INPUT);" 
testButton2.setupButton();
}

void loop() {

// --- use your buttons!
testButton.read();   // debounced version of digitalRead(PIN)
testButton2.read();
}
//deBounce.h

#ifndef DEBOUNCE_MUNT_H
#define DEBOUNCE_MUNT_H

#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif

class DebounceButton {
public:
  DebounceButton(int b_pin, unsigned long debounceTime,unsigned char inputMode);
  void setupButton();
  int read();
private:
  int _b_pin;
  unsigned long _debounceTime;
  unsigned char _inputMode;
  unsigned long _debounce;

};
#endif
//deBounce.cpp

#include "debounce.h"
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
DebounceButton::DebounceButton(int b_pin, unsigned long debounceTime,unsigned char inputMode)
{
  _b_pin = b_pin;
  _debounceTime = debounceTime;
  _inputMode=inputMode;
  pinMode(_b_pin, _inputMode);
}

void DebounceButton::setupButton() {
  pinMode(_b_pin, _inputMode);
}

int DebounceButton::read()    {

  if (millis()-_debounce < _debounceTime){
    return LOW;
  }
  if  (digitalRead(_b_pin) == HIGH){
    if (millis()-_debounce > _debounceTime){
      _debounce=millis();
      return HIGH;
    }
    else {
      return LOW;
    }
  }
}

deBounce_muntv_1_2.zip (2.85 KB)

thanks for sharing,

some thoughts...
:

  • pinMode(_b_pin, OUTPUT); ==> pinMode(_b_pin, INPUT);

  • debounce time parameter should be unsigned (long!) to have bigger range

  • debouncing tunable in micros() iso millis() ??? // they are called micro switches after all :wink:

  • is this code needed?

#if defined(ARDUINO) && ARDUINO >= 100
  #include "Arduino.h"
#else
  #include "WProgram.h"
#endif
  • move pinMode(_b_pin, INPUT); ==> constructor??

  • option to use INPUT_PULLUP ?

  • check() --> read(); ??

Excellent suggestions, thank you!

debouncing tunable in micros() iso millis() ??? // they are called micro switches after all

I'm having difficulty with this part :blush:
I've tried various ways and keep getting type errors and when I fix the type errors I get things such as " milli is not defined in this scope". Any help is much appreciated :slight_smile:

  • is this code needed?
#if defined(ARDUINO) && ARDUINO >= 100

#include "Arduino.h"
#else
 #include "WProgram.h"
#endif

It seems to necessary to include the Arduino.h for some operations.

I updated the original post to include the updates.

  • move pinMode(_b_pin, INPUT); ==> constructor??

Not a good idea. Global class instances result in calling the constructor before init() is called. Diddling with the mode of a pin will prove useless when init() sets the mode of all pins later.

There are already several libraries around for dealing with switches. They all use the name Button. Why escapes me. Buttons are for keeping shirts closed.

Use a unique name for your class, to prevent conflicts with other libraries written by equally unimaginative people.

There are already several libraries around for dealing with switches. They all use the name Button. Why escapes me. Buttons are for keeping shirts closed.

might be the difference between American English and other kinds of English?

In Dutch I know a switch (schakelaar) can have a button (knop) so that might explain it to you.

And Paul, you know languages (and their words/meaning ) change during time, it is called evolution.
See e.g. - http://www.lel.ed.ac.uk/~simon/Papers/Kirby/The%20Evolution%20of%20Language.pdf - :wink:

Not a good idea. Global class instances result in calling the constructor before init() is called. Diddling with the mode of a pin will prove useless when init() sets the mode of all pins later.

Thanks for the input!

Use a unique name for your class, to prevent conflicts with other libraries written by equally unimaginative people.

I wrote this code for myself, I'm sharing it incase someone else finds it of use. Presumably they wont be using more than one button class? If they are then they can change it if they wish.

I would suggest that your inability to perceive of a reason as to why a push switch could be called a button might betray your own lack of imagination.

I would suggest that your inability to perceive of a reason as to why a push switch could be called a button might betray your own lack of imagination.

And, I would suggest that using a name that refers to a specific kind of switch as a general name referring to all kinds of switches betrays your lack of imagination.

Yes ... you already said that. :roll_eyes:

Send me your postal address and I'll mail you a 3D printed medal for exemplary achievements in unnecessary passive aggressive behaviour. :sleeping: