Buttons
Pull-down Button
This sketch demonstrates the basic implementation of a button or switch, in pull-down configuration, with debouncing functionality for noisy buttons.
The code
#include <CtrlBtn.h>
// Define an onPress handler.
void onPress() {
Serial.println("Pull-down button pressed");
}
// Define an onRelease handler.
void onRelease() {
Serial.println("Pull-down button released");
}
/*
Create a button with:
- signal pin.
- bounce duration.
- onPress handler.
- onRelease handler.
*/
CtrlBtn button(1, 15, onPress, onRelease);
void setup() {
Serial.begin(9600);
// For this example we use a board with internal pull-down resistors:
button.setPinMode(INPUT_PULLDOWN);
// If your board does not support that, you can use an external
// pull-down resistor. In that case you call the following method:
// button.setPinMode(INPUT, PULL_DOWN);
}
void loop() {
// Handle all button functionality.
button.process();
}