CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

I need ideas about how to handle a LED in open loop

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
E_Blue



Joined: 13 Apr 2011
Posts: 417

View user's profile Send private message

I need ideas about how to handle a LED in open loop
PostPosted: Mon Mar 18, 2024 6:22 pm     Reply with quote

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: 9120
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue Mar 19, 2024 5:23 am     Reply with quote

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: 1319

View user's profile Send private message

PostPosted: Tue Mar 19, 2024 7:21 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Mar 19, 2024 11:02 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Mar 19, 2024 11:04 am     Reply with quote

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: 19238

View user's profile Send private message

PostPosted: Tue Mar 19, 2024 12:08 pm     Reply with quote

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: 456
Location: Montenegro

View user's profile Send private message

PostPosted: Wed Mar 20, 2024 1:44 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Mar 20, 2024 9:08 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Mar 20, 2024 7:28 pm     Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group