View Full Version : Multiple loops running independently?
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:
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!
07-23-2010, 11:37 PM
The way to do that is to use a Propeller instead of a Stamp.
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
07-23-2010, 11:52 PM
It's possible to use a Stamp to flash two LEDs at unrelated rates because the Stamp is much faster than the blink rates you want. It makes the programming more complex though. Essentially you write one single loop that uses a PAUSE statement to set the timing say at 10ms, like
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:
IF counter1 = 100 THEN counter1 = 0
Assuming you want a 50% on / 50% off LED cycle, you'd then test for this with:
IF counter1 = 50 THEN TOGGLE LED1pin
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.
07-24-2010, 12:30 AM
Another way to do this is to make two counters advance at different rates, and couple the flash of the LED to the most significant bit. On the Stamp:
counterA VAR Word
counterB VAR Word
freqA CON 65 ' adjust freqA and freqB for LED flash rate.
freqB CON 130 ' they set how long it takes for the counter to advance one full cycle
LEDA PIN 0
LEDB PIN 1
LOW LEDA ' make LED pins output
counterA = counterA+freqA
counterB = counterB+freqB
LEDA = counterA.bit15
LEDB = counterB.bit15
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.
Post Edited (Tracy Allen) : 7/23/2010 4:36:01 PM GMT
07-24-2010, 01:32 AM
I read too much into the question - of course you can do it through programming, as Mike and Tracy pointed out.
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!
07-24-2010, 02:01 AM
I just realized that what I told you won't get you a 50% on / 50% off cycle. You also need to toggle the LED when the count wraps around. You could do this with:
IF counter1 = 100 THEN counter1 = 0 : TOGGLE LEDpinNo
instead of just setting the counter to zero