This library encapsulates tracking a momentary button's pressed state, reporting whether it was tapped (pressed and released quickly) or held (closed for longer than some threshold).
Example:
// One side of the button is attached to pin 2, the other to ground.
#define PIN_BUTTON 2
#include "MomentaryButton.h"
MomentaryButton button(PIN_BUTTON);
void setup() {
button.setup(); // set as INPUT, set HIGH
}
void loop() {
button.check();
if (button.wasClicked()) {
// Respond to the button being briefly closed, then released.
} else if (button.wasHeld()) {
// Respond to the button being closed for longer.
}
}
To install, add the .cpp and .h files to your project, and #include "MomentaryButton.h". (The files are not presented as an Arduino library, though a github fork to do so is welcome.)
We care about the privacy and personal data of our users.
To continue, please give us your consent:
Please confirm that you have read the privacy policy
Thank you for subscribing!
Curious to learn more?
Are you also a teacher, student, or professional that loves using Arduino in your day-to-day activities?
Then keep up-to-date with either our STEM or Professional monthly newsletters.
Arduino weekly newsletter (already subscribed)
Educators can benefit from the ever growing tech that shapes our environment through fun cool projects.
Why not awe your boss with highly innovative ways to help keep your enterprise connected at no extra cost?
Arduino Survey
We'd like to get to know you little better.
Please help us improve by answering this super short optional survey.