ButtonDebounce
ButtonDebounce.h - Arduino Library for Button Debounce.
Install / Use
/learn @maykon/ButtonDebounceREADME
Button Debounce
Pushbuttons often generate improper open/close transitions when pressed, due to mechanical and physical issues: these transitions may be read as multiple presses in a very short time fooling the program, without debouncing, pressing the button once may cause unpredictable results.
This library will sample the button repeatedly; and if it sees the button in its new state for 8 consequtive checks (equally spaced within 'delay' milli seconds) -- will report the state as changed.
Usage
SETUP
Declare a ButtonDebouce object with pinNumber and timeMillis to debounce as parameter, and defined a callback function.
Example:
void onButtonChange(const int state) {
Serial.println("Changed: " + String(state));
}
ButtonDebounce button(3, 250); // PIN 3 with 250ms debounce time
button.setCallback(onButtonChange);
Or - in a more asynchroneous style:
void onButtonChange(const int state) {
Serial.println("Changed: " + String(state));
}
ButtonDebounce button(3, 250); // PIN 3 with 250ms debounce time
button.setCallback([](const int state) {
Serial.println("Changed: " + String(state));
});
Important: buttons don´t share resources. The following methods are avaliable.
update()called inloop method, used to verify the debounce timer and the button state changes.state()return the last button state.setCallback(function)set the callback function that will executed on button state changes.
Note that the constructor also sets the INPUT_PULLUP pinmode() on the pin.
LOOP
Due the time buffer used in each button, please call the update() method in loop, so it can be update and reflect all state changes.
Be advised: Do not declare any delay() before the update functions, as it may interfere with the bounce time buffer.
void loop() {
buttonUp.update();
buttonDown.update();
}
Contributors
Thank you for all contributions:
Related Skills
node-connect
343.1kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
90.0kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
343.1kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
343.1kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
