Built and maintained by Imogen Heard on behalf of Pan Galactic Tech
- Libaray to handle and debounce button inputs using OOP topology.
- Button is sampled periodically and history saved into a bitstream.
- Bitmasks used to compare bitstream to a rising edge or falling edge over a number of samples.
- This method is great at rejecting noise from dirty contacts or cheap buttons.
- Button defined as a Pull Up or Pull Down button in constructor.
- Method can differentiate between long press & short press.
- Adjustable long press duration.
- Returns total number of button presses.
- Returns total number of button releases.
- Returns total number of button longPresses (In resting state: releaseCount + longPressCount = pressCount).
- Ultra fast and low overhead method.
- Each buttonObject controls one input, add a new constructer for each button required.
- Clone or Download library folder into Arduino IDE libraries folder.
- Extract files from zipped folder if required.
- Open Arduino IDE and select example program.
No dependency for library function, however examples have the following dependencies:
- ledObject_library available from here - written by PanGalacticTech
#include <buttonObject.h> // Libary include
Button Pin Declaration
#define BUTTON_PIN 4 // Select Button input pin
Long Press Time Declaration
#define LONG_PRESS_TIME 750 // time in milliseconds
Declaring a button object:
buttonObject button(BUTTON_PIN, BUTTON_PULL_HIGH);
If button is LOW pulled HIGH on press, use: BUTTON_PULL_HIGH
Else If button is HIGH pulled LOW on press, use: BUTTON_PULL_LOW
Setup function must be run once before .buttonLoop() method.
button.begin();
.begin() Function sets up button input pin into input mode, and sets INPUT_PULLUP, if button is specified
BUTTON_PULL_LOW
Loop function must be called in every loop & as often as possible. Delays between loop executions will affect function.
button.buttonLoop(LONG_PRESS_TIME); // long int controls the time required for a long press. (defaults to 1000 mS)
This function will record the bitstream and return boolean flags when button states are detected.
A short press is detected when the user releases the button in a time shorter than LONG_PRESS_TIME.
bool is set true on button release.
button.shortPress
A long press is detected when the user keeps button depressed longer than LONG_PRESS_TIME.
bool is set true while button is depressed and button will not trigger short press untill
it is released and reset.
button.longPress
button.shortPress & button.longPress bools act as triggers and must be reset after triggering associated behaviour.
button.buttonReset(); // .buttonReset method resets longPress & shortPress variables once action has been taken
button.buttonLoop(); // long int passed controls the time required for a long press. (defaults to 1 second)
if (button.shortPress) {
led.toggleLED();
button.buttonReset(); // .buttonReset method resets longPress & shortPress variables once action has been taken
}
- Please report any bugs or issues found.
- Arduino IDE - Default IDE
- Email: Personal | Company
- Github Profile