Best practice question

My project will include a couple of buttons. Is there a "best practice" recommendation on whether the action should happen immediately when the button on PRESSED or should the system wait for the button to be released??

Coding one .vs. the other isn't difficult, just want it to work in a manner that people expect.

John

I have an alarm fob that sets the alarm when the button is released (or if it's held down longer).
I don't like that - others couldn't get it despite repeated "training sessions".

  • For an N.O. switch, I usually have it so on closed (pressed/pushed) start the process. However there are situations where a long press might add some security, example >= 2 seconds.

For me it is application dependent. Some stuff I wrote changes mode when a button is pressed for a short time but does the mode when held. IE light, tap it and the light turns on or off, hold it and it will get brighter or dimmer. Nice thing about this several buttons can be wired in parallel as in my kitchen lighting. It controls both mains lighting and LED lighting. Did that with a UNO about 6 years ago.

1 Like

Most button libraries have functions for pressed, released, long press, double click, sometimes triple click and I may have missed a few.

The depends on what you want the button to do and what makes the most sense. Short and long presses often initiate different actions.

1 Like

What do you expect. It might depend on what that button must do.

Let's take a TV.

  1. If I press a button on the TV or the TV remote to change the volume or channel I expect immediate effect and I expect that effect to repeat every N milliseconds while the button is pressed.
  2. If I press the standby button I expect immediate effect and I don't expect repeat while the button is pressed.
2 Likes

Expectations will depend on the action that the switch initiates. Mostly I press a button I expect to see a result. The only exception I can think of is a "dead man" switch as found on trains (and suicide bombers).

If you have to distinguish between a single press and a double press then the single part must activate on a timeout following release, that is when you are sure that no second press will follow. The second part of a double press can activate on either press or release.

In general, however, you act immediately on detecting the press, if possible, but there are also other special cases, for example, in an electrically noisy environment, you may want to debounce the button first.

Nope. Assuming you have some "debounce" solution (via software or hardware), the behavior following the action (and the code to implement it) depends on a few conditions:

  1. what you need to do as soon as it is pressed
  2. if and what you need to do while the button is still pressed
  3. if you need to distinguish between a single button press, long press, double press, etc.
  4. if the code has other things to check in a timed manner

If you provide us with the answers to these points (and any other information related to what you have in mind to achieve), we can possibly give you more targeted advice.

No there is not.