I'm using the OneButton library to implement functions for short and long press buttons with a good deal of success. At this stage I'm just experimenting with code.
However, there's one thing I want to do and I can't think how to do it. Maybe someone can help.
This involves a button with "Short Press" which also has "Long Press - and HOLD".
I can't work out how to achieve the "Hold" condition as all the examples trigger off the long press "after" the button is released, not when it is being held down.
Essentially, I'm looking at programming a counter to count freely one by one on a short press but count up (or down, using another button) with 0.5 second intervals on a "press and hold". It has to detect the short press to allow for a one by one increment.
I can't see how this can be implemented using the onebutton library...
Am I missing something very fundamental here?
Also, if I do find a way to "detect" the hold function how can I use this to trigger a counter to count up in x second intervals? Incrementing a variable on each button press is easy but how do I get it to increment up and then stop when the button is released? I'm assuming set a boolean to detect button true when held down - but what exactly will be set to true?
I'm very new to Arduino and so far I'm getting to grips with most things but I can't work out how to achieve what seems to be a simple function!
Can anyone give me some pointers how I could achieve this?
capture a timestamp when the button is pressed and run a timer checking for the time since the button press to expire
if the button is released before the timer expires, recognize a "short" button press
if the timer expires before a button release, i would normally say recognize a "long" press, but you could possibly reset the expiration period to a longer value
if the button is released before the timer exceeds the "longer period", then recognize a long press
and if the timer exceeds the longer value, recognize a "hold"
seems it might be difficult for someone to know when the button is pressed long enough for a long press but not too "long" to be recognized as a "hold"
/**
* Attach an event to fire when the button is pressed and held down.
* @param newFunction
*/
void attachLongPressStart(callbackFunction newFunction);
void attachLongPressStart(parameterizedCallbackFunction newFunction, void *parameter);
/**
* Attach an event to fire as soon as the button is released after a long press.
* @param newFunction
*/
void attachLongPressStop(callbackFunction newFunction);
void attachLongPressStop(parameterizedCallbackFunction newFunction, void *parameter);
/**
* Attach an event to fire periodically while the button is held down.
* @param newFunction
*/
void attachDuringLongPress(callbackFunction newFunction);
void attachDuringLongPress(parameterizedCallbackFunction newFunction, void *parameter);
So if you want to be notified once the button is held down long enough, you configure a callback with attachLongPressStart()
If you want to be notified repeatedly during press you configure attachDuringLongPress()
And if you want to know when the button is released you configure attachLongPressStop()
The 3 can be configured at the same time, they are not exclusive
Note that with the Toggle Library, debouncing is inherent to its design, meaning that all "events" are glitch free, as they are determined from the output of this debounce algorithm.