Blinking LEDs - Subloops?
JackMac
Posts: 3
Hi Forum - I'm doing my first project with a BS2 on a BOE board with PBasis2.5.
I'm controlling several LEDs (basically easy), but then I need to make a selected one blink at a fast rate (like 5/sec), while other LEDs stay lit. I'd like to somehow generate a 5/sec clock signal ("Blink") that continues outside (independent of) the main loop. Then I can just set RedLED (a PIN variable) = Blink to make it flash. So, in essence, I need to have TWO loops running; one to control the blink rate, plus the main loop.
If I just define a bit variable called "Blink" like this,
' Generate Blink squarewave 200ms (5/second)
BlinkCtr = BlinkCtr + 1
SELECT BlinkCtr 'BlinkClk counts to 200, then resets
CASE 100 'Assuming main loop time ~1ms
Blink = 1
CASE 200
Blink = 0
BlinkCtr = 0
ENDSELECT
then this Blink timing is screwed up if the main loop goes off and does something else for a few seconds.
Any ideas on how to make Blink remain a fixed squarewave (or some other approach to making the LED blink fast)?
Thx,
Jack
I'm controlling several LEDs (basically easy), but then I need to make a selected one blink at a fast rate (like 5/sec), while other LEDs stay lit. I'd like to somehow generate a 5/sec clock signal ("Blink") that continues outside (independent of) the main loop. Then I can just set RedLED (a PIN variable) = Blink to make it flash. So, in essence, I need to have TWO loops running; one to control the blink rate, plus the main loop.
If I just define a bit variable called "Blink" like this,
' Generate Blink squarewave 200ms (5/second)
BlinkCtr = BlinkCtr + 1
SELECT BlinkCtr 'BlinkClk counts to 200, then resets
CASE 100 'Assuming main loop time ~1ms
Blink = 1
CASE 200
Blink = 0
BlinkCtr = 0
ENDSELECT
then this Blink timing is screwed up if the main loop goes off and does something else for a few seconds.
Any ideas on how to make Blink remain a fixed squarewave (or some other approach to making the LED blink fast)?
Thx,
Jack
Comments
This may help you understand what you want to do ·····
http://www.parallax.com/Portals/0/Downloads/docs/books/edu/Wamv2_2.pdf
What’s a Microcontroller?· Chapter #2: Lights On – Lights Off · Page 37
This is where it start read all of Chapter 2
JackMac
So, in essence, I need to have TWO loops running; one to control the blink rate, plus the main loop.
You can ONLY do one thing at a time with a BASIC STAMP
Now you can·run thing one at time and make it look like you are doing two thing at one time but you are not
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
··Thanks for any··that you may have and all of your time finding them
·
·
·
·
Sam
Post Edited (sam_sam_sam) : 7/7/2008 7:08:54 PM GMT
When I jump out of my main loop with a gosub to set the LED states, I set some LEDs high or low and others to my variable "Blink" before returning to the main loop. But even though Blink is a 200ms squarewave, the LED is told to go to the value of Blink at the instant it's sampled, not the alternating value over time.
Seems like I may have to assign the blinking LED state value within a subloop inside the main loop, thus increasing the duration of the main loop (which I don't want to do).
Suggestions?
Thx,
Jack
Jack