The DigitalDebounce library provides an easy and reliable way to debounce digital inputs in embedded systems, particularly for Arduino-based projects. It is designed to monitor specific bits within bytes, making it ideal for applications where button states or similar digital signals are packed into byte arrays.
- Debounce individual bits within a byte.
- Flexible initialization with customizable debounce intervals.
- Lightweight and efficient.
- Suitable for single or multiple button debouncing.
- Copy the
digitalDebounce.h
anddigitalDebounce.cpp
files into your Arduinolibraries
directory under a folder namedDigitalDebounce
. - Restart your Arduino IDE to recognize the library.
#include "digitalDebounce.h"
To debounce a specific bit within a byte:
// Parameters: reference to the byte, bitmask for the bit, debounce interval in ms, initial state
DigitalDebounce button(&byteVariable, 0x01, 50, false);
&byteVariable
: Pointer to the byte containing the bit to monitor.0x01
: Bitmask for the bit to debounce.50
: Debounce interval in milliseconds.false
: Initial state of the bit (default).
In your loop()
function, call the update()
method:
if (button.update()) {
if (button.read()) {
// Bit is HIGH (pressed)
} else {
// Bit is LOW (released)
}
}
update()
: Returnstrue
if the state has changed.read()
: Returns the current debounced state of the bit.
Here is a complete example for toggling LEDs based on button presses:
#include "digitalDebounce.h"
// Simulated byte array representing input states
byte inputStates[10];
// Create debounce instances
DigitalDebounce button1(&inputStates[5], 0x04, 50, false);
DigitalDebounce button2(&inputStates[6], 0x01, 50, false);
void setup() {
Serial.begin(9600);
}
void loop() {
if (button1.update()) {
if (button1.read()) {
Serial.println("Button 1 pressed");
} else {
Serial.println("Button 1 released");
}
}
if (button2.update()) {
if (button2.read()) {
Serial.println("Button 2 pressed");
} else {
Serial.println("Button 2 released");
}
}
}
DigitalDebounce(const byte* byteRef, byte bitMask, unsigned long interval, bool initialState = false)
Constructor to initialize a debounce instance.
Call this method in the main loop to update the debounce logic. Returns true
if the state has changed.
Returns the current debounced state of the monitored bit.
This library is provided "as-is" without warranty of any kind. You are free to use and modify it for your projects.