Help using Bounce2 and detecting "Long Press" buttons

Just wondering if anybody has any examples that show differentiating between normal presses and "Long presses" on buttons? Maybe using the bounce2 library, since I'm using that already..

I'm worried about not triggering the "short press" behavior on a long press..

I'm guessing that for every button I have, a time counter needs to be involved and triggered on every button press and then reset on button release. then at the time of release, the "held time" would be known and then you can decide if it was a long press or short press?

You can refer to this Arduino button long press and short press tutorial

Interesting tutorial.

It finally gets around to mentioning, and handling, contact bounce.

It eventually throws in the towel and uses a library, ezButton, which gets my high praise when I say of it that it doesn't suck.

With ezButton, you have to, it seems, handle long and short with your own code. Other libraries may do it all by themselves.

a7

Thinking about it more.. as soon as a program wants to have a distinction between a "short" and "long" button press, it now has to wait for the RELEASE of the button to know how long it was held for before doing any action.

I'll have to go around the house pressing buttons on everything, but I think it would be weird to press a button and not have anything happen until I release it.

Hello blackstarred

Welcome to the world's best Arduino forum ever.

In general, no library is required for this function.
Two timers based on the millis() function are required.
Timer1 to debounce the key.
Timer2 to measure the time the button is pressed and released.
I use the IPO model for the description:

  • INPUT: read in button and debounce
  • PROCESSING1: if (ButtonBecomesPressed) stores current millis()
  • PROCESSING2: if (ButtonBecomesReleased) calculate current millis() minus saved millis
  • OUTPUT: Result of processing2

Have a nice day and enjoy coding in C++.

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