Detect Long Press - Bike Light Simulation

Post Reply
Posts: 11
Joined: Mon Dec 07, 2020 3:07 pm
Has thanked: 3 times
Been thanked: 2 times

Detect Long Press - Bike Light Simulation

Post by ctksfc »

(12.32 KiB) Downloaded 34 times
Afternoon ALL - n00b question coming your way - hope someone can help;

Running a project with students to develop a NEW Bikelight solution - this involves designing and 3Dprinting cases plus makinga a basic LED circuit + developing 1 button solution to cycle through programs. Managed to do the Mode Cycle BASICS but stuck on the LongPress function / reset etc.
Specification ;
Long Press LED on/off
Short press LED Flash modes/Low Power etc.

anyone ???
(12.32 KiB) Downloaded 34 times
fc9Bikelight.PNG (105.51 KiB) Viewed 408 times

Matrix Staff
Posts: 559
Joined: Sat Dec 05, 2020 10:32 am
Has thanked: 72 times
Been thanked: 144 times

Re: Detect Long Press - Bike Light Simulation

Post by Steve-Matrix »

One approach could be to have a main loop that cycles every 1ms or so and has a counter that essentially gives a 'tick count' for your system. Also within the loop you would check for any changes in state of the switch and set the mode accordingly, and then also output the state of the LED depending of the mode. In pseudocode:

Code: Select all

loop forever
  increment count
  check for switch state change
    (if released, check count since it was pressed and set mode accordingly)
  set the state of the led depending on the mode
    (if mode is flashing, flip led state if count has increased by a set value since it was last flipped)
  small delay (e.g. 1ms)
end loop
You would have a few variables:
  • the counter which increments continually around the loop giving an ongoing 'time'
  • the 'time' (i.e. value of the counter) when the button was last depressed
  • the last known state of the switch
  • the mode of operation
  • the current state of the led
  • the 'time' since the last flip in the led state
You probably want to avoid having any long delays (e.g. to implement the flashing).

A neater solution would be to use interrupts - a time to use for the flashing and an interrupt-on-pin-change to react to the button presses.

And if you were after something really special, you could put the microcontroller into a low-power sleep state in the 'off' state and only wake it up when the button is pressed again.


Post Reply