The advanced debounce removes the resulting ripple signal and provides a clean transition at its output with delayed
and instant
modes.
The delayed mode responds to the signal to update the output when input is stable for the fully qualified debounce period.
The instant mode provides an instant response to the first edge on the signal to update the output and wait for the input to stable for the fully qualified debounce period for the next update.
#include "ADebouncer.h"
ADebouncer variableName;
ADebouncer debouncer;
variableNam.mode(debounce_t debounceMode, unsigned long debouncePeriod, bool initOutput);
debounceMode
Debounce Mode.
DELAYED
Delay ModeINSTANT
Instant Mode
debouncePeriod
Debounce Period in milliseconds.
initOutput
Initial output state.
Define the debouncer in delayed mode with debounce period of 10 milliseconds and initial output with a HIGH state.
debouncer.mode(DELAYED, 10, HIGH);
result = variable.debounce(input);
input
:bool
Bounce input.
debounced
:bool
Get the debounced output state.
bool debounced = debouncer.debounce(digitalRead(buttonPin));
bool debouncing = variable.debouncing();
Get the debouncing state.
bool debouncing = debouncer.debouncing();
digitalWrite(LED_BUILTIN, debouncing);
Or,
digitalWrite(LED_BUILTIN, debouncer.debouncing());
bool debounced = variable.debounced();
Get the debounced state.
bool debounced = debouncer.debounced();
digitalWrite(LED_BUILTIN, debounced);
Or,
digitalWrite(LED_BUILTIN, debouncer.debounced());
bool risingEdge = variable.rising();
Get the rising edge of the output.
bool risingEdge = debouncer.rising();
if (risingEdge) toggle = !toggle;
Or,
if (debouncer.rising()) toggle = !toggle;
bool fallingEdge = variable.falling();
Get the falling edge of the output.
bool fallingEdge = debouncer.falling();
if (fallingEdge) toggle = !toggle;
Or,
if (debouncer.falling()) toggle = !toggle;
Declare debounce mode as delayed mode. Debounce the input signal from the button and update LED_BUILTIN with the debounced state.
Click here the Button sketch.
#include "ADebouncer.h"
#define buttonPin 2 // Define the button input pin.
#define debouncePeroid 10 // Define the debounce period in milliseconds
ADebouncer debouncer; // Declare debouncer variable.
void setup() {
pinMode(buttonPin, INPUT_PULLUP); // Set the button mode as input pullup.
pinMode(LED_BUILTIN, OUTPUT); // Set the LED_BUILTIN mode as output.
debouncer.mode(DELAYED, debouncePeroid, HIGH); // Set the debounce mode as delayed mode and debounce period as 10 ms, with the initial output in a HIGH state.
}
void loop() {
bool buttonState = debouncer.debounce(digitalRead(buttonPin)); // Save the debounced of the button state.
digitalWrite(LED_BUILTIN, buttonState); // Update LED_BUILTIN with the button state.
}
Declare debounce mode as delayed mode. Debounce the input signal from the button. Toggle the state when pressing the button and update LED_BUILTIN with the toggle state.
Click here the Toggle sketch.
#include "ADebouncer.h"
#define buttonPin 12 // Define the button input pin.
#define debouncePeroid 10 // Define the debounce period in milliseconds
ADebouncer debouncer; // Declare debouncer variable.
bool state; // Declare state variable.
void setup() {
pinMode(buttonPin, INPUT_PULLUP); // Set the button mode as input pullup.
pinMode(LED_BUILTIN, OUTPUT); // Set the LED_BUILTIN mode as output.
debouncer.mode(DELAYED, debouncePeroid, HIGH); // Set the debounce mode as delayed mode and debounce period, with the initial output in a HIGH state.
state = HIGH; // Initial state in a HIGH state.
}
void loop() {
debouncer.debounce(digitalRead(buttonPin)); // Debounce input of the button state.
if (debouncer.falling()) state = !state; // Toggle state of the state variable.
digitalWrite(LED_BUILTIN, state); // Update LED_BUILTIN with the state.
}
This example is designed to have a set button and a reset button to Reset-Set the state.
- Declare debouncer for the set button. set the debounce as a delayed mode
- Declare debouncer for the reset button. Set the debounce as an instant mode
Both set and reset buttons have the same debounce period. in this example, will be set to debounce period for 1 second,
- To set the state, press the set button for 1 second.
- To reset the state, the state will be instantly reset after pressing the reset button.
The LED_BUILTIN will be updated with the state value.
Click here the ResetSet sketch.
#include "ADebouncer.h"
#define setPin 12 // Define the set input pin.
#define resetPin 11 // Define the reset input pin.
#define debouncePeroid 1000 // Define the debounce period in milliseconds
ADebouncer setButton; // Declare set debouncer variable.
ADebouncer resetButton; // Declare reset debouncer variable.
bool state; // Declare state variable for ResetSet.
void setup() {
pinMode(setPin, INPUT_PULLUP); // Set the button mode as input pullup.
pinMode(resetPin, INPUT_PULLUP); // Set the button mode as input pullup.
pinMode(LED_BUILTIN, OUTPUT); // Set the LED_BUILTIN mode as output.
setButton.mode(DELAYED, debouncePeroid, HIGH); // Set the debounce mode as delayed mode and debounce period, with the initial output in a HIGH state.
resetButton.mode(INSTANT, debouncePeroid, HIGH); // Set the debounce mode as instant mode and debounce period, with the initial output in a HIGH state.
state = LOW; // Initial state in a LOW state.
}
void loop() {
setButton.debounce(digitalRead(setPin)); // Debounce input of the set button state.
resetButton.debounce(digitalRead(resetPin)); // Debounce input of the reset button state.
state = (state | !setButton.debounced()) & resetButton.debounced(); // Reset and Set the state
digitalWrite(LED_BUILTIN, state); // Update LED_BUILTIN with the state.
}