Long Press using OneButton library

Hi.

Hopefully quick question...

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?

one approach is

  • 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"

There are 3 dedicated callbacks you can use for long press


  /**
   * 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

Try the Wokwi examples here. The "Retrigger_While_Pressed" and "Press_Code" examples might be of interest.

Lots of good suggestions guys! Thank you.

I'll have to work out the best implementation. There are many ways to do this it seems :grinning:

@gaztech
imho the OneButton Library fires the events correctly. So it should be no problem.
See the state diagram:
http://www.mathertel.de/Arduino/OneButtonLibrary.aspx

please make a full compileable example with the right member functions which shows your problem.

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.

OneButton is also debounced but indeed Toggle is quite rich

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.