Read and debounce buttons and switches.
See Example Public Methods Release NotesRead and debounce buttons and switches without delay or interrupts. React to button events with the onPressed() and onReleased() commands. Control debounce time with setDebounceTimeout().
This library sets each button up as input_pullup by default, allowing you to wire the button to any digital input and ground. Input_pullup can be disabled by passing an optional flag to the constructor() and wiring the button with your own resistor.
#include <RBD_Timer.h> #include <RBD_Button.h> RBD::Button button(2); // input_pullup by default void setup() { Serial.begin(115200); } void loop() { if(button.onPressed()) { Serial.println("Button Pressed"); } if(button.onReleased()) { Serial.println("Button Released"); } }
RBD::Button
Create a new button and pass in the Arduino pin number. This library will default to input_pullup and invert the button readings to simplify wiring for you. Pass in the second [optional] parameter as false to disable input_pullup.
RBD::Button button(2); // RBD::Button button(3, false); // disable input pullup void setup() { ... }
Provide an unsigned long value greater than zero to set the debounce time in milliseconds. Most mechanical buttons will flip between on and off very quickly before settling in the correct state after being pressed or switched. Adjusting the debounce timeout will help remove these extra state-change events.
void setup() { button.setDebounceTimeout(10) // default ms }
Flip the value of the raw button state reading so when the button isPressed() it will register as isReleased(), and vice versa. This method changes the behavior for onPressed() and onReleased() as well.
void setup() { button.invertReading(); }
Returns true while the button is being pressed, or is on. Returns false while the button is not being pressed, or is off.
This method is good for real-time actions that must fire many times during a button press. Use the onPressed() event to fire only one action on each button press.
void loop() { if(button.isPressed()) { // code executes on each loop Serial.println("Button Pressed"); } }
Returns true while the button is released, or is off. Returns false while the button is being pressed, or is on.
This method is good for real-time actions that must fire many times while the button is not pressed. Use the onReleased() event to fire only one action on each button release.
void loop() { if(button.isReleased()) { // code executes on each loop Serial.println("Button Released"); } }
This will return true only once, after the button has been pressed (and debounced). The button must be released and then pressed again for this method to return true again.
void loop() { if(button.onPressed()) { // code only runs once per event Serial.println("Button Pressed Event"); } }
This will return true only once, after the button has been released (and debounced). The button must be pressed and then released again for this method to return true again.
void loop() { if(button.onReleased()) { // code only runs once per event Serial.println("Button Released Event"); } }