Go Down

Topic: For loops?? (Read 3 times) previous topic - next topic

Nick Gammon

Please edit your post, select the code, and put it between [code] ... [/code] tags.

You can do that by hitting the # button above the posting area.

marco_c

#26
Dec 01, 2012, 01:54 am Last Edit: Dec 01, 2012, 01:59 am by marco_c Reason: 1
Here is the explanation:
* MB_HIGH and MB_LOW are pretty obvious. They keep the LED on or off for the specified time.
* MB_LOOP makes the pattern loop back to the nominated array element in the pattern for the specificed number of times.

So the data
Code: [Select]
 { 3, 0, 0, {{MB_HIGH, 25, 0}, {MB_LOW, 25, 0}, {MB_LOOP, 3, 0}, {MB_LOW, 300, 0}}, 0 },

does this:
0 LED on for 25 ms
1 LED off for 25 ms
2 Loop back to element 0 and repeat it 2 more time (3 total)
3 LED off for 300 ms - this is just a delay while the 'other' LED does its thing

So if we want to have a paired LED that works in tandem with this one we need to have the logic
0 LED off for 300 ms - this is just a delay while the 'other' LED does its thing
1 LED on for 25 ms
2 LED off for 25 ms
3 Loop back to element 1 and repeat it 2 more time (3 total)

which provides to table entry
Code: [Select]
 { 4, 0, 0, {{MB_LOW, 300, 0}, {MB_HIGH, 25, 0}, {MB_LOW, 25, 0}, {MB_LOOP, 3, 1}}, 0 },


So if we look at them as a pair:
Code: [Select]

 { 3, 0, 0, {{MB_HIGH, 25, 0}, {MB_LOW, 25, 0}, {MB_LOOP, 3, 0}, {MB_LOW, 300, 0}}, 0 },
 { 4, 0, 0, {{MB_LOW, 300, 0}, {MB_HIGH, 25, 0}, {MB_LOW, 25, 0}, {MB_LOOP, 3, 1}}, 0 },


You can see that the second MB_LOOP (pin 4) is in a different spot in the sequence and it loops back to a different place in the array. That is because we need to add the initial delay to the second LED while the sequence for pin 3 is executing.

Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

Antennas

Thanks Marco. It is making more sense now. I will play around with it and see what I can get going. I learn a tidbit from each piece of code others write, so thank you.

I am sure I will have other questions and I'll post them soon!!  :) 

Thank you to everyone for their input so far.  I'll try and remember the "code" thingy to make it easier for you to read.

Andrew

Antennas

#28
Dec 02, 2012, 09:17 pm Last Edit: Dec 02, 2012, 09:39 pm by Antennas Reason: 1
I am digging around in that code Marco, thanks. Now can I just load that into an ATTiny85? I think it will fit?
Does most code work with the different chips?

I tried to just upload the code and it took it. But...the LED's stay on after they blink instead of stay off. I tried playing with some numbers in the table but no luck. Any ideas? (I say that but I am sure you already know...LOL)

Sorry for the noob questions, but I find you folks are very helpful!!

Andrew

dhenry

Fairly easy: send a set of patterns to the leds and that pattern determines how fast they blink.

For example, if '1' means on and '0' means off,

0b11 -> both leds are on
0b01 -> bit 0 is still on, but bit 1 is blinking
0b10 -> bit 0 is blinking and bit 1 is blinking
0b00 -> bit 1 is blinking and bit 0 isn't.

A sequence like that will blink one led at 1/2 of the speed of the other led.

You can expand this approach to cover multiple leds at multiple speed.

Go Up