This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Detect long-press of two buttons simultaneously

I want to perform an action when two buttons are pressed for a while simultaneously. What is the best way of doing it? 

  • Hi,

    You can refer to the BSP library to see how you can implement long button presses (or simply use it directly). With that, yo can get an event when the button is pressed, or when it is released or has been pressed for long enough that a timeout occurs (indicating a long push).

    You could use that to make a simple logic of the type If button 2 is pressed amximum x amount of timer after button 1 and bothare pressed for at least a long push duration, you conclude that both were pressed for a while simultaniously after the last of the two buttons (2 in this case) gets a long push event (timeout).

  • Thanks Einar for the reply, this really helps. However,  since I am very new to NRF, I am facing some issues implementing the same, so would it be possible to share some reference codes to help implement this.

  • Hi,

    If you don't want to use BSP or want to do it simpler, perhaps just use the button handling library (app_button) and app_timer library. You can see how the app_button library is used in the BLE Blinky Application. For the app_timer you can refer to many SDK examples or the (slightly dated) app_timer tutorial.

    If you just want to require that the second button is released before the first button, and that both are pressed for a minimum amount of time you could combine that to do for instance like this (I am typing as I go so there may be some adjustments needed if I forget to think about something):

    1. Keep state variables of both buttons (pushed, released, long_pushed)
    2. Prepare two singleshot app timers that timeout at the minimum time for a long button press, one for each button
    3. When a button is pressed, change the state variable and start the respective timer.
    4. If button is released before the timeout, stop the timer.
    5. If timeout orrucs before button is released, check state of other button. If that is not pushed, then set state back to released. If it is pushed, then set state to long_pushed.
    6. at this point, the second button is still pushed. If timeout occurs for the second button, check the state of the first button. That also just had a long push, so now you can signal that both buttons were pushed for a long time. If not, check if button is released. If so, set this button to released as well. If it is pushed, set this to long_pushed so that it is remembered in case the first buttion is also long pushet. Etc..

    Einar

Related