Detect Long Press - Bike Light Simulation
-
- Posts: 11
- http://meble-kuchenne.info.pl
- Joined: Mon Dec 07, 2020 3:07 pm
- Has thanked: 3 times
- Been thanked: 2 times
Detect Long Press - Bike Light Simulation
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 ???
-
- Matrix Staff
- Posts: 1277
- Joined: Sat Dec 05, 2020 10:32 am
- Has thanked: 170 times
- Been thanked: 285 times
Re: Detect Long Press - Bike Light Simulation
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:
You would have a few variables:
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.
HTH,
Steve.
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
- 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
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.
HTH,
Steve.