I want to perform an action when two buttons are pressed for a while simultaneously. What is the best way of doing it?
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).
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):
Einar