View previous topic :: View next topic |
Author |
Message |
E_Blue
Joined: 13 Apr 2011 Posts: 417
|
I need ideas about how to handle a LED in open loop |
Posted: Mon Mar 18, 2024 6:22 pm |
|
|
I need ideas about how to handle a LED in open loop to indicate two different things.
I have one LED and need to indicate two different things, one is power source that can be 12V external(continuous ON) or internal Lithium battery(flashing). This is what is working; now I need also to indicate signal level with the same LED, to meet this goal the idea is to indicate the battery first during 10 seconds, as is working now, then the LED goes off by 2 seconds, and blinks one to five times(100mS) to indicate signal level, then a 2 seconds pause(LED OFF) to separate de modes and the cycle starts again indicating the power source for 10 seconds again.
Currently I have a routine that is periodically called every 30ms(approx.) and was thinking in to add this new routine inside but can't figure out what approach use to get the sate machine running. "if then", "switch" a combination of those?
I need, not the code, but a way to think this differently because when I start to think in so many conditionals my brain goes overflowed and reboot. _________________ Electric Blue |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9232 Location: Greensville,Ontario
|
|
Posted: Tue Mar 19, 2024 5:23 am |
|
|
Any chance you have a spare I/O pin ? Connecting LED to TWO I/O pins gives you Charliplexing option. |
|
|
jeremiah
Joined: 20 Jul 2010 Posts: 1351
|
|
Posted: Tue Mar 19, 2024 7:21 am |
|
|
you might consider just doing the flashing (5 pulses) but vary the speed of the pulsing for the different power sources. make one flash twice as fast as the other |
|
|
E_Blue
Joined: 13 Apr 2011 Posts: 417
|
|
Posted: Tue Mar 19, 2024 11:02 am |
|
|
temtronic wrote: | Any chance you have a spare I/O pin ? Connecting LED to TWO I/O pins gives you Charliplexing option. |
I can't, the PCB and case has been done. _________________ Electric Blue |
|
|
E_Blue
Joined: 13 Apr 2011 Posts: 417
|
|
Posted: Tue Mar 19, 2024 11:04 am |
|
|
jeremiah wrote: | you might consider just doing the flashing (5 pulses) but vary the speed of the pulsing for the different power sources. make one flash twice as fast as the other |
But how I can do flashes in open loop, that's what I need to figure out. _________________ Electric Blue |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19529
|
|
Posted: Tue Mar 19, 2024 12:08 pm |
|
|
Just have a timer interupt at (say) 1/4 second. Feed this with two
global variables. The first a 'time' value, and the second a 'count' value.
Inside the interrupt have a static local 'loop' value.
On the interrupt, if loop == 0, copy thet time value into loop. Then on
each call decrement loop, and laternate the LED when this is zero, and
reload it. So you can set 'time' to 1, and you will get a 2Hz flash. Set
time to 2 and you get a 1Hz flash. Obviously adjust the interrupt time
and time value to give the flah rates you want. Then whenever the
loop reloads, decrement the count value. Outside, you can set time to
1, and count to 20 (say), and wait till [spam]==0. Then load the time with
2 and count with 10, and you will get twenty flashes at 2Hz, then ten at
1Hz. Choose the counts and intervals to give what you want. |
|
|
PrinceNai
Joined: 31 Oct 2016 Posts: 480 Location: Montenegro
|
|
Posted: Wed Mar 20, 2024 1:44 am |
|
|
I'd go with a state machine (switch) inside the timer interrupt. 100ms or so interrupt, since that is the shortest time you need to blink your LED. 4 states, PowerState, Pause1, SignalLevelState, Pause2. One variable which you decrease till it reaches 0 to count the interrupts and with that measure the time you stay in each state, preloaded with desired values at the moment you change state. Maybe three more to control the behavior and blinking speed in PowerState and the number of blinks in SignalLevelState. |
|
|
pmuldoon
Joined: 26 Sep 2003 Posts: 218 Location: Northern Indiana
|
|
Posted: Wed Mar 20, 2024 9:08 am |
|
|
I kinda like jeremiah's idea, but with a twist. Since it's hard to differentiate between a long pulse and a short one without something to compare to, I would consider something like this:
Just do the pulse to indicate signal strength. If the LED pulse is mostly on, that would indicate line power. if the pulse is mostly off, that would indicate battery power (and conserve power as well).
It's hard to tell a long pulse from a short one, but it should be easy to tell a 10% duty cycle from a 75% duty cycle. |
|
|
E_Blue
Joined: 13 Apr 2011 Posts: 417
|
|
Posted: Wed Mar 20, 2024 7:28 pm |
|
|
Thanks for your ideas, now the code is running fine.
Now I'm fighting against a nasty bug that is making the PC jumps from one routine to another that shouldn't. Later I will create a new post explaining better. _________________ Electric Blue |
|
|
|