Multiple loops running independently?
Zan
Posts: 6
Hey guys!
I recently received the BASIC Stamp Activity Kit from Parallax.
I've been following through the tutorials in the book. Going back to the basic ones of flipping an LED on and off, I started to experiment a little.
I want to be able to have my first LED pulse at a certain rate and another LED pulse at another rate. Using the simple code, I could do something like this:
DO
HIGH 15
PAUSE 100
LOW 15
HIGH 14
PAUSE 250
Low 14
LOOP
Of course, when I do this, the pauses that affect the 14 LED also affect the cycle of the 15 LED.
What I want to be able to do is have one LED flash and the other flash, and the pauses in between flashes of one LED do not affect the flashes of the other LED. Is there a way to go about doing this?
Thank you for your time!
I recently received the BASIC Stamp Activity Kit from Parallax.
I've been following through the tutorials in the book. Going back to the basic ones of flipping an LED on and off, I started to experiment a little.
I want to be able to have my first LED pulse at a certain rate and another LED pulse at another rate. Using the simple code, I could do something like this:
DO
HIGH 15
PAUSE 100
LOW 15
HIGH 14
PAUSE 250
Low 14
LOOP
Of course, when I do this, the pauses that affect the 14 LED also affect the cycle of the 15 LED.
What I want to be able to do is have one LED flash and the other flash, and the pauses in between flashes of one LED do not affect the flashes of the other LED. Is there a way to go about doing this?
Thank you for your time!
Comments
A Stamp is only capable of doing one thing at a time, and you've come up to the limitations that puts on what you can do. You'll find the same limitations with almost any other microprocessor you try to use (e.g., the Arduino's Atmel processor, the PIC processors, etc.). However, the Parallax Propeller is capable of doing 8 different things at a time, and in fact getting LEDs to blink independently at different rates is the first parallel processing activity in the manual. When I had that working, I jumped for joy. It sounds simple, but it's a whole new ball game as far as programming goes.
If you really need to do this (blink LEDs at different rates), learn what you can from the Stamps (they'll teach you plenty!), and then move up to the Propeller.
Don't be discouraged by this. You're at a very important point in learning about these devices.
================
Alternatively, if you REALLY needed to get LEDs blinking at different rates using a Stamp, you could connect the Stamp I/O pins to digital potentiometer chips, and use those to vary the resistor value in a 555 timer circuit to change the rates of LED blinking. There may be other ways as well, using external circuits.
Post Edited (sylvie369) : 7/23/2010 3:42:43 PM GMT
You have as many counters (variables) as you have LEDs and, each time through the loop, you increment all the counters and do a wraparound (to zero) when each counter reaches its limit. If you want an LED to cycle once a second, it would wraparound at 100 (because there are 100 x 10ms in one second). This would look like:
Assuming you want a 50% on / 50% off LED cycle, you'd then test for this with:
If you have a lot of LEDs, all these tests will take quite a bit of time and you may want to reduce the PAUSE time to 9 to allow for that. You'll have to experiment.
I'm figuring that the above loop will execute in something like 1 to 2 milliseconds, so the 16 bit counters will run a full cycle in on the order of 1 second. If you add additional code to the loop, you will have to adjust the freqX values to compensate for the extra loop time.
This is the principle of a numerically controlled oscillator, or direct digital synthesis--The propeller has lots of capability in that direction.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com
Post Edited (Tracy Allen) : 7/23/2010 4:36:01 PM GMT
In some sense that's not truly "multiple loops running independently", but it will probably accomplish what you're after. Sorry about that.
I tried using a counter, and it works great!
IF counter1 = 100 THEN counter1 = 0 : TOGGLE LEDpinNo
instead of just setting the counter to zero