I need to detect if a button is pressed, if it is a short or a long press. While in the long press a counter to be activated for an x amount of time (6 Seconds). If I release the button in the middle the counter, counter should be stopped and reset. But if it reaches the total count a relay should be activated. Can anyone help me with this. Thank you advance
I need to detect if a button is pressed, if it is a short or a long press.
When do you want to detect that? The state change detection example is a good starting point.
You can learn when the switch became pressed. You can learn when the switch became released. You can then measure the interval to know how long it was pressed.
Or, you can, on every pass through loop(), see if the switch is still pressed, and, if so, for how long.
While in the long press a counter to be activated for an x amount of time (6 Seconds).
"a counter needs to be activated" makes no sense. Activating a timer would make some sense. The specific time mentioned doesn't make sense, either.
But if it reaches the total count a relay should be activated.
PaulS:
When do you want to detect that? The state change detection example is a good starting point.
You can learn when the switch became pressed. You can learn when the switch became released. You can then measure the interval to know how long it was pressed.
Or, you can, on every pass through loop(), see if the switch is still pressed, and, if so, for how long.
"a counter needs to be activated" makes no sense. Activating a timer would make some sense. The specific time mentioned doesn't make sense, either.
What do you think you are counting?
I want to know the state of the button press. (Short press or long press) depending on the amount of time the button is being pressed. If it is a long press a long press while the button is still been pressed it should run on the serial monitor displaying you are holding the button for this amount of time. (Holding for 1sec, 2sec, 3 sec.,)
I want to know the state of the button press. (Short press or long press)
The state is pressed or released. There are no other states. The duration is not a state.
You can easily measure the duration, if you know when an event occurred and what time it is now.
If it is a long press a long press while the button is still been pressed it should run on the serial monitor displaying you are holding the button for this amount of time. (Holding for 1sec, 2sec, 3 sec.,)
That is possible. The blink without delay example shows how to do things periodically, like printing to the serial port, without using delay. The state change detection example shows how to determine that some state changed (i.e. a switch became pressed).
Combine them. Record when the switch becomes pressed. Periodically (on every pass through loop()), see if the switch is pressed (this is independent of the test for the switch becoming pressed). If it is, and it is time to print, print. If it is, and enough time has passed, do whatever it is you plan to do when the switch has been held for 6 or more seconds.
Do you want to act on a long press when they release the button, or at the 6 second mark? If you want to do it at the six second mark, then you need to handle what happens after that - the button is down, but it's not down because it's been pressed, it's down because the operator hasn't released it yet.
jeromesack:
I want to know the state of the button press. (Short press or long press) depending on the amount of time the button is being pressed.
You need to say how long the different button presses are.
In the normal way of thinking a short press is very short and a long press might be for 200 or 400 millisecs - long enough for it to be obviously a long press.
The important point is that both types end after a relatively short time (short in human terms).
I have a suspicion that what you are thinking about is continuously holding a button for several seconds (which could get very tiresome for a human) and, if so I think I would program that differently.