BS2: 2 leds ...one flashing fast to slow and the other going slow to fast. SIMULTANEOUSLY
adam1971
Posts: 1
in BASIC Stamp
Hello,
I'm a newbee to the BS2 world. I'm trying to create a code that will run 2 leds simultaneously .
One flashing from slow 1000ms to fast 1ms and the other doing the opposite ....1ms to 1000ms at the SAME TIME. I know it probably has to do with a nested loop or an IF THEN type command but i'm at a loss. Any help would be greatly appreciated.
Thank you,
I'm a newbee to the BS2 world. I'm trying to create a code that will run 2 leds simultaneously .
One flashing from slow 1000ms to fast 1ms and the other doing the opposite ....1ms to 1000ms at the SAME TIME. I know it probably has to do with a nested loop or an IF THEN type command but i'm at a loss. Any help would be greatly appreciated.
Thank you,
Comments
Unfortunately, you can not do what you want with the Basic Stamp, as it has to complete the one task before doing the next. It would be easy with the Propeller chip.
Maybe I don't completely understand the original question.
OP: can you post what you have so far?
Do you mean that you want led A on for 1000mS then off for 1000mS while led B is on for 1mS then off for 1mS, then have the on/off time for led A decrease while the on/off time for led B increases, and when led A reaches 1mS on/off and led B reaches 1000mS on/off they reverse? Sort of a time/direction see-saw?
I wrote a program to do kwinn's interpretation; was able to get the minimum and the step size down to 2.5 msec. Even made the on period and off period equal.
In any case, as Publison suggests, you can do it better and cheaper with a prop. I think the cheapest BS-2 is a homework board for $40; you can get a flip for $35. And the flip has the leds on board!
One of the first things to understand about the Stamp is that it executes one.line.at.a.time.
So any code programmed to cause one LED to delay will cause the other to delay at an undesired rate because of this One-Line-At-A-Time operation.
Now I'm sure the Forum Wizards can comment further.
Publison made the suggestion of using another Parallax product called the Propeller which contains 8 separate processors that can be doing different things, essentially at the same time.
Regards,
Dave
Suppose RED is on p0 and GREEN is on p1.
The Stamp is slow, (~140µs per instruction minimum), so the following loop will itself take on the order of one millisecond, not accurate. It could be sped up by using shifts and periods in powers of two instead of the division. Oh, the Propeller chip mentioned above has 16 dedicated numerical oscillators built into its hardware. They could handle this task easily even at much much higher rates and resolutions.
Would you be so kind as to 'splain what is going on in your code snippet?
It has stumped me.
DJ
Consider what happens if grnFreq=32768/1. Then the value of grnCount will alternate between 32768 and zero, because 32768+32768 rolls over to zero in the 16 bit word. By transferring the most significant bit to the output pin, the green LED flashes at 295 Hz. That is about the maximum frequency that can be achieved with this program due to the speed of the PBASIC interpreter.
With larger values in the denominator of the division, the rate slows down. With grnFreq=32768/20, it spends 20 periods low and 20 periods high, for an output frequency of about 68Hz.
With 500 in the denominator, the period is about 1.7 second, 0.6 Hz. In that case, a value of 65 is added to the count each time around the loop (32768/500 = 65), so it takes 1000+ times through the loop to complete a cycle. (65536/65 = 1008) . Note that the integer division is not exact, so there is a bit of bobble in the output frequency.
This is the same logic as the NCO modes of the Cog counters in the Propeller. On each clock cycle the value of FRQA is added to the phase accumulator PHSA, and the msb is automatically transferred to the assigned pin.
A program to scan the frequencies simultaneously up and down "simply" needs to update the values of redFreq and grnFreq. I'm not sure what the OP has in mind.
I get what you're saying. Thank you for the description. This is a perfect example of someone who knows the intricate abilities of the BS2.
When the relocation to Idaho is complete, and I have my lab back, I'll set this up and observe. There is a lot of meat here, methinks.
Dave
Dave, it's fun (in Idaho!) to set up a row of LEDs all at somewhat different frequencies so that they drift in and out of sync. People will swear that it is random, mesmerizing.
Sometimes posters will not wait a day or so to get answers to their homework assignments.
We may never know. He/she has not logging in since the original posting.
that is why I assumed figured red on for 1000, green on for 1, red on for 999, green on for 2, etc.
The granularity of PAUSE is 1ms, but there is the interpreter overhead, which adds about 230µs of dead space. According to my old database, PULSOUT also adds about 230µs for the interpreter, but with a granularity of 2 microseconds it could be tuned to hit 870µs to make a total of 1ms. Similarly for the 1000ms flash. Then, there may be need to account for the time to execute a GOTO, or DO:LOOP, and that adds another 250µs. Uh...
I do still love programming the Stamp, but not to the point of splitting hairs!
As Tracy says, we are way into splitting hairs!