The Debounce Filter library implements a filter to eliminate bounce in physical devices such as push buttons or encoders. The library allows setting an interval in milliseconds. After a change, any subsequent changes during the interval are ignored, resulting in the filtering of possible bounces.
The Debounce Filter class has an AddValue method to introduce the new state value to filter. This way, we can use the Debounce Filter with any input method, including GPIO, interrupts, calculated values, or received by any means of communication.
The Debounce Filter library allows adding callback functions for OnRising, OnFalling, and OnChanging events, so we can use the Debounce Filter class to manage the program flow without having to perform these tasks from the main loop.
For more information, check the post “>Read a button with Arduino using interrupts and debounce.
User Manual
Constructor
The Debounce Filter filter is instantiated through one of its constructors.
DebounceFilter();
DebounceFilter(bool state);
DebounceFilter(unsigned long interval);
DebounceFilter(unsigned long interval, bool state);
Using the filter
// Add a new value to the filter and return the filtered value
bool AddValue(bool newState);
// Get the last state
bool GetState() const;
// Returns true if the last state was a falling edge
bool IsFalling() const;
// Returns true if the last state was a rising edge
bool IsRising() const;
// Change the interval value
void SetInterval(uint16_t interval);
// Callback functions
DebounceAction OnChange;
DebounceAction OnFalling;
DebounceAction OnRising;
Examples
The Debounce Filter library includes the following examples to illustrate its use.
- debounceFilter: Example of filtering and callback functions
Installation
- Download the latest version from GitHub
- Unzip the file
- Copy to your libraries folder (usually My Documents\Arduino\libraries)
- Restart the Arduino IDE