DebouncedButtons (Arduino library)
02/25/2014 - 00:21
Debounce sketch in Arduino IDE default examples uses 8 lines of code and 3 additional variables for the sole purpose of debouncing one button.
If you have more buttons than that, it can be overwhelming to take care of all of them.
Here this tiny library comes handy. You just have to
1. include library
#include <DebouncedButtons.h>
2. init each button object
DebouncedButton myButtonObjectName = DebouncedButton(int pinNumber, unsigned long debounceDelay);
3. read each button state (almost as easy as digitalRead() )
boolean myButtonStateVariable = myButtonObjectName.dbRead();
This has to be called from inside loop (like you would call digitalRead) and so often that the button would be checked several times before period of debounceDelay passes! Make debounceDelay longer if your code runs slow.
Other similar libraries
- Debounce - very similar approach to mine, but needs calling of two functions instead of one for finding out the button state.
- Bounce - somewhat different, I understand that it is meant to detect changes in button state.
- Switch - more advanced library, handles long presses and doubleclicks also. Might be very useful for some projects of intermediate or advanced level.
Download
My libary zip-file is below. (How to install a library?)
File:
Example.ino
#include <DebouncedButtons.h> //CREATE INSTANCES OF DebouncedButton CLASS for each button //DebouncedButton myButtonObjectName = DebouncedButton(int pinNumber, unsigned long debounceDelay); DebouncedButton button1 = DebouncedButton(3,50); //Button attached to pin 3. //It has debounce delay 50 milliseconds. DebouncedButton button2 = DebouncedButton(4,2000); //Button attached to pin 4. //This has debounce delay 2000 ms = 2 seconds. //Debounce delay 2 seconds is very long, but lets you observe the meaning of debouncing. //The press or releasing of the button registers after the state has been stable for 2 seconds. //If you keep toggling the button during that time, it does not register at all. //50 ms is a good starting value if you don't know what to put in debounceDelay. void setup() { /*Serial is for example purposes*/ Serial.begin(9600); Serial.println("Button on pin 3 Button on pin 4"); } void loop() { //USAGE //the dbRead function has to be called from inside loop //and so often that reading is repeated at least few times before debounceDelay time passes //myButtonObjectName.dbRead() returns boolean value (1 or 0) of the last state seen stable //for the timespan of debounceDelay set for that specific buttonObject //this statement is a debounced equivalent of //boolean myButtonReading = digitalRead(myButtonPinNumber); boolean button1debounced = button1.dbRead(); boolean button2debounced = button2.dbRead(); /*Serial is for example purposes*/ Serial.print(" "); Serial.print(button1debounced); Serial.print(" "); Serial.println(button2debounced); }
DebouncedButtons.h
#ifndef DEBOUNCEDBUTTONS_H #define DEBOUNCEDBUTTONS_H #include "Arduino.h" class DebouncedButton { public: DebouncedButton(int pinNumber, unsigned long debounceDelay); boolean dbRead(); private: int pin; unsigned long dbDelay; boolean lastDbState; unsigned long lastDbTime; boolean value; }; #endif
DebouncedButtons.cpp
#include "Arduino.h" #include "DebouncedButtons.h" DebouncedButton::DebouncedButton(int pinNumber, unsigned long debounceDelay) { pin = pinNumber; dbDelay = debounceDelay; lastDbState = digitalRead(pin); value = lastDbState; lastDbTime = millis(); } boolean DebouncedButton::dbRead() { boolean dbState = digitalRead(pin); if (dbState != lastDbState) {// if reading changes lastDbTime = millis(); // we reset timer } lastDbState = dbState; //save current reading for next loop if ((millis() - lastDbTime) > dbDelay) { //if reading has been constant for long enough value = dbState; // we assign reading as new value } return value; }