I am using the EasyButton library to detect button presses on an arduino.
I want do execute a function when the user
- Presses a button
- Presses and holds a button
- Releases a button
Detecting when a user presses or holds a button is straight forward
#include <Arduino.h>
#include <EasyButton.h>
# define BUTTON_PIN 2;
EasyButton button(BUTTON_PIN);
void onPressedCallback() {
Serial.println("Button was pressed and released");
}
void onHoldCallback() {
Serial.println("Button was pressed and held");
}
void setup() {
button.begin();
button.onPressed(onPressedCallback);
button.onPressedFor(buttonHoldDuration, onHoldCallback);
}
The easyButton library has the following events
- onPressed
- onPressedFor
- onSequence
It has the following states
- isPressed
- isReleased
- wasPressed
- wasReleased
- pressedFor
- releasedFor
How would you trigger an event when a button is 'released'?
Full Documentation
https://easybtn.earias.me/docs/on-single-press-api
What I've tried
- Check the state from inside an event
void onHoldCallback() {
while (button.isPressed() {
Serial.println("you are still holding the button");
}
Serial.println("Button was released");
}
This unfortunately creates an infinite loop since the button state is never refreshed (button.begin();)