Contents
If the duration is longer than a defined time, the long-press event is detected. In the second use case, After the button is pressed, We continuously measure the pressing time and check the long-press event until the button is released.
Arduino can only detect the state of your button (pressed OR unpressed). You could use a timer variable (based on this example from their docs) to save the exact time when you pressed or released the button, so you can check the difference between both variables to calculate how long it is on hold or idle.
What does long press mean?
touch & hold
Many times, touch & hold lets you take action on something on your screen. For example, to move an app icon on your home screen, touch & hold, then drag it to the new location. Sometimes touch & hold is called a “long press.” Give feedback about this article.
How long is long press?
The standard long press time is what is returned by getLongPressTimeout(), which is currently 500ms but may change (in 1.0 it was 1000ms but changed in a later release; maybe in the future it will be user-customizable).
The long press tends to take about 4 to 5 seconds to activate with most of the phones I have used, so I would say 4 to 5 seconds is a good range.
You could use a timer variable (based on this example from their docs) to save the exact time when you pressed or released the button, so you can check the difference between both variables to calculate how long it is on hold or idle. The code should look something like this:
How to find how long a button is being held?
Trying to determine how long a button is pressed to set the speed of a character, but am not sure how to code it. I know the code below is wrong but it’s just to give an idea. You have the right idea. GetButtonDown to record a time. GetButtonUp to then calculate how much time has passed.
What can I do with the long pressed button?
You will need to perform some action while the button is pressed, like skipping some seconds in the MP3-track. Starting a timer that gets cancelled on mouseUp that triggers that kind of work on a regular interval (100ms?) while the button is down seems viable to me.
const unsigned long shortPress = 100; const unsigned long longPress = 500; For your project or application, you may need to change these values. They determine how much time, in milliseconds, must pass to register as a short or long press. For example, “shortPress” might be large for the type of button you’re using.