Buttons
Dual action button
This sketch demonstrates the basic implementation of a dual action pull-up button or switch, that differentiates between a short and long press.
The code
#include <CtrlBtn.h>
// Define an onRelease handler.
void onRelease() {
Serial.println("Short press");
}
// Define an onDelayedRelease handler.
void onDelayedRelease() {
Serial.println("Long press");
}
/*
Create a button with:
- signal pin.
- bounce duration.
- empty onPress handler (nullptr).
- onRelease handler.
- onDelayedRelease handler.
*/
CtrlBtn button(1, 15, nullptr, onRelease, onDelayedRelease);
void setup() {
Serial.begin(9600);
button.setDelayedReleaseDuration(1000); // This is optional (default is 500ms).
// If you use an external pull-up resistor, uncomment this:
// button.setPinMode(INPUT, PULL_UP);
// If you use an external pull-down resistor, uncomment this:
// button.setPinMode(INPUT, PULL_DOWN);
}
void loop() {
// The process method will poll the button object and handle all it's functionality.
button.process();
}