Buttons
Advanced button
This sketch demonstrates an more advanced implementation of a pull-up button or switch, with debouncing functionality for noisy buttons.
With this approach you can extend the button class (CtrlBtn) into your own class, and expand on the base functionality as you please. The sky is the limit here. The inheriting class overrides the onPress & onRelease methods. Within these methods you can add your desired actions.
The code
#include <CtrlBtn.h>
// Extend the CtrlBtn class into a CustomButton class.
class CustomButton : public CtrlBtn
{
public:
CustomButton(uint8_t sig, uint16_t bounceDuration) : CtrlBtn(sig, bounceDuration) { }
protected:
void onPress() override
{
Serial.println("Advanced button pressed");
}
void onRelease() override
{
Serial.println("Advanced button released");
}
};
// Create a button with the signal pin number & bounce duration.
CustomButton button(1, 15);
void setup() {
Serial.begin(9600);
// 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();
}