Go Down

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

Antennas

I am not really sure what I need just yet. I only have about 3 days under my belt with the arduino.

What I am looking to do is blink a pair of LED's at one rate while blinking another pair at a different rate. What I have now is using delay functions and it makes it go through the whole sequence before it starts again which isn't working. I want to blink lights like in a police car. The red and blue go fast and the headlights blink slowly (wig,wag). I think it would be a good learning project but I haven't found anything similar yet online to look at.
Just a nudge in the right direction would be appreciated.
It's taking longer to pick this up than I thought, I am not 20 anymore darn it. LOL.... :smiley-red:

Thank you all.

Andrew

Arrch

Look at the Blink Without Delay example.

James C4S

After looking at blink without delay, look at this tutorial I wrote on millis(), which shows how to blink 2 LEDs at different rates:

http://www.cmiyc.com/blog/2011/01/06/millis-tutorial/
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

marco_c

And just to add another, you can look at MultiBlink. This is probably a bit challenging at your level of understanding, but it will stretch your thinking.
Arduino libraries http://arduinocode.codeplex.com
Parola for Arduino http://parola.codeplex.com

Antennas

Thanks Gents. I will look and try to learn as I read. I appreciate your replies, thank you.

Andrew

Antennas

Hey Marco-C I am looking at that multibink. But I think it needs a library MultiBlink.h ?? Where do I get that or how does that work...you are right that is very new for me..but hey I can maybe learn about it now.

Thank you.

Andrew

marco_c

The header file is part of the posting. You will need to split them as there was no easy way to do that in the playground. It is the first part of the code up to the #endif, as per the comments in the program.
Arduino libraries http://arduinocode.codeplex.com
Parola for Arduino http://parola.codeplex.com

Antennas

So I saved the header file as a .h in the directory of the sketch. But now the sketch won't compile as it doesn't like the table T..says <Multi_blink:47: error: 'T' was not declared in this scope"  what do I need to modify in this line??
Also if you say that it needs to go into the sketch dir, well I can't find it..tried many things..it's not in the documents and I don't see one where it should be..I know it's obviously around somewhere but I can't find it. It's supposed to work with the .ino file right??

Andrew


jraskell

Post your code.  We can't tell you what you did wrong without seeing what you did.  Sounds like you aren't properly declaring your LedTable T, but that's just a guess without any code to look at.

Antennas

#9
Aug 28, 2012, 05:42 pm Last Edit: Sep 04, 2012, 11:49 am by Nick Gammon Reason: 1
It wasn't my code it is from the Muilti_Blink on the play ground. Everyone seemed so aware of it I didn't post. But here it is.


Code: [Select]
/ Multi_Blink
//
// Blink lots of LEDs at different frequencies simultaneously
//
// Marco Colli - May 2012
//
// Demonstrates the way to carry out multiple time based tasks without using the delay() function
// Demonstrates the use of structures (and structures within structures)
// Demonstrates a data driven approach to programming to create compact, reusable code
//

#include "Multi_Blink.h"  // type definitions

// Blink Table T - Modify this table to suit whatever the output requirements are
// Add or delete lines as required to achieve the desired effects.
//
ledTable  T[] =   // THIS LINE HERE
//Pin  St   State 0      State 1  LastTime
{
 { 3, 0, {{HIGH, 300}, {LOW, 300}}, 0 },
 { 4, 1, {{HIGH, 300}, {LOW, 600}}, 0 },
 { 5, 0, {{HIGH, 500}, {LOW, 500}}, 0 },
 { 6, 1, {{HIGH,  50}, {LOW, 100}}, 0 },
 { 7, 0, {{HIGH, 100}, {LOW,  50}}, 0 },
 { 8, 1, {{HIGH, 500}, {LOW, 500}}, 0 },
 { 9, 0, {{HIGH, 400}, {LOW, 600}}, 0 },
 {10, 0, {{HIGH, 600}, {LOW, 400}}, 0 },
};

// Self adjusting constants for loop indexes
#define  MAX_STATE  (sizeof(T[0].state)/sizeof(stateDef))
#define  MAX_LED    (sizeof(T)/sizeof(ledTable))

void setup()
{
 for (int i=0; i < MAX_LED; i++)
 {
   pinMode(T[i].ledPin, OUTPUT);

   T[i].lastTransTime = millis();
   digitalWrite(T[i].ledPin, T[i].state[T[i].currentState].activeVal);
 }
}

void loop()
{
 for (int i=0; i < MAX_LED; i++)
 {
   // check if the state active time has expired (ie, it is less than current time)
   if (millis() >= T[i].lastTransTime + T[i].state[T[i].currentState].activeTime)
   {
     // switch to the next state with wrapround
     T[i].currentState = (T[i].currentState + 1) % MAX_STATE;

     // write out the next state value
     T[i].lastTransTime = millis();
     digitalWrite(T[i].ledPin, T[i].state[T[i].currentState].activeVal);
   }
 }
}


Moderator edit: [code] ... [/code] tags added. (Nick Gammon)

AWOL

If you put code tags around it, we'll be able to read it as you intended.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Antennas

I'll look for code tags, but that what I intended. It works now I guess I needed to restart the sketch or arduino. I tried to add more lines but it won't work it says ulti_blink:22: error: too many initializers for 'stateDef [2]'"
As I modified the line to read..  { 5, 0, {{HIGH, 100}, {LOW, 100}, {HIGH, 100}, {LOW, 100}}, 0 },

Can you not do that? I want to make the LED flash quickly 3 times then wait as another LED flashes the same , then back.

jraskell


I'll look for code tags, but that what I intended. It works now I guess I needed to restart the sketch or arduino. I tried to add more lines but it won't work it says ulti_blink:22: error: too many initializers for 'stateDef [2]'"
As I modified the line to read..  { 5, 0, {{HIGH, 100}, {LOW, 100}, {HIGH, 100}, {LOW, 100}}, 0 },

Can you not do that? I want to make the LED flash quickly 3 times then wait as another LED flashes the same , then back.


No, you can not do that.  Statedef is a struct that declares two fields, a state (either HIGH or LOW) and a duration.  LedTable is another struct that declares a statedef array of two elements, so you can't put more than 2 elements into it, and you are trying to put 4 in.

Multiblink isn't really suited to what you want to do because it's designed to flash multiple LEDs completely asynchronously, and you want synchronous behavior.

dxw00d

Quote
I'll look for code tags, but that what I intended.

You intended it to turn to italics two thirds of the way through?

Antennas

Ha, no I did not dxwood. Well taken. I meant the code looked as it was right to me. Formatting aside.

jraskell, what do you suggest? I thought multiblink was exactly what I wanted as the LED's don't have to have any relation at all except for in pairs. Such as two blink back and forth or two blink fast then off as the other blinks.

I would welcome any code help at all. Thanks for your replies.

Go Up