Hi,
I am working with the "Arduino-Helpers" library. THe author says, that there is the function of detect long press button events. I wasnt able to figure out how to use this functionality.
Anybody know how to use it?
Here is the define for the long press: https://github.com/tttapa/Arduino-Helpers/blob/master/src/AH/Settings/Settings.hpp#L79
constexpr unsigned long LONG_PRESS_DELAY = 450; // milliseconds
Here is an example for a debounced butten: https://github.com/tttapa/Arduino-Helpers/blob/master/examples/Arduino-Helpers/Hardware/2.Button/2.Button.ino
Then you need a magic spell during full moon and a crystal ball, and then this appears:
void loop() {
if (pushbutton.update() == Button::Falling) {
Serial.println( "Button pressed");
}
if (pushbutton.stableTime() >= LONG_PRESS_DELAY) {
Serial.println( "Long press");
}
}
If you want to know how I came up with that, well, I just told you: full moon and magic spell and so. I did not test it, and it is probably wrong. Perhaps I need a better spell.
The long press is implemented by the (somewhat misleadingly named) IncrementButton
class. You can get some inspiration from the implementation:
I have not found the time yet to add a truly general long press button class, but IncrementButton
provides everything you need (and more): it notifies you for all interesting events, such as start of a press (IncrementShort
), start of a long press (IncrementLong
), released after a short press (ReleasedShort
), or released after a long press (ReleasedLong
). There's also another event, IncrementHold
, that is returned on a timer when the button is held for long periods of time, to mimic the behavior of a keyboard key (i.e. quickly insert the same letter when you keep it pressed down).
See Arduino Helpers: IncrementButton Class Reference for the details.
The usage of the IncrementButton
is almost exactly the same as the Button
class (see the Button example linked to by Koepel), except for the fact that IncrementButton::getState()
returns different kinds of states.
Hey thanks a lot for the explanation. What I did is:
Button b1 = mcp1.pinA(0); // GPIOA0
IncrementButton button1 = b1;
setup(){
mcp1.begin(); // Initialize the MCP23017
b1.setDebounceTime(50);
b1.begin(); // Initialize the button (enables pull-up resistor)
}
loop(){
switch (button1.update())
{
case IncrementButton::ReleasedShort:
{
// do something on short release
}
break;
case IncrementButton::ReleasedLong:
{
// do something on long release
}
break;
default:
break;
}
}
This works nice. I tried using IncrementButton::IncrementHold
but this will not only show 3
once. It will show up as long as the button is pressed. So I had to register the first 3
(long press), do some action an reset this status as soon as i release the button.
Is there a way to recognize only one long press event with your class?
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.