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

How to calculate how much time the button is pressed?

I am working on requirement, where if you hold a button for "1 second " it should turn ON LED1 on the DK, likewise when you press button for "5 seconds" it should turn OFF the LED1 and turn ON LED2 .

Now I am facing issue how to calculate the amount of time the button is pressed.

I am using nrf52832, sdk 11.

Could anyone help me in solving this issue?

Thanks in advance.

Parents
  • I can help you =)

    First of all, there're many potential solutions for your problem but (in my opinion) the most elegant is to define buttons GPIO pins as interrupts. On button pressed event, call buttonPressedHandler and start timer (you can use RTC or APP timer). On buttonReleased event, stop the timer, subtract two times and you'll get pressed duration time. And there, a simple switch statement should be enough for your application logic. 

Reply
  • I can help you =)

    First of all, there're many potential solutions for your problem but (in my opinion) the most elegant is to define buttons GPIO pins as interrupts. On button pressed event, call buttonPressedHandler and start timer (you can use RTC or APP timer). On buttonReleased event, stop the timer, subtract two times and you'll get pressed duration time. And there, a simple switch statement should be enough for your application logic. 

Children
No Data
Related