Pages: [1] 2 3   Go Down
Author Topic: For loops??  (Read 2881 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 46
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

California
Offline Offline
Faraday Member
**
Karma: 92
Posts: 3441
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Look at the Blink Without Delay example.
Logged

Fort Lauderdale, FL
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6144
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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/
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.c

Sydney, Australia
Offline Offline
Edison Member
*
Karma: 33
Posts: 1285
Big things come in large packages
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

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

Offline Offline
Newbie
*
Karma: 0
Posts: 46
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Andrew
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 46
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Sydney, Australia
Offline Offline
Edison Member
*
Karma: 33
Posts: 1285
Big things come in large packages
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

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

Offline Offline
Newbie
*
Karma: 0
Posts: 46
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

New Hampshire
Offline Offline
God Member
*****
Karma: 17
Posts: 781
There are 10 kinds of people, those who know binary, and those who don't.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 46
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
/ 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)
« Last Edit: September 04, 2012, 04:49:10 am by Nick Gammon » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26495
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you put code tags around it, we'll be able to read it as you intended.
Logged

"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.

Offline Offline
Newbie
*
Karma: 0
Posts: 46
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

New Hampshire
Offline Offline
God Member
*****
Karma: 17
Posts: 781
There are 10 kinds of people, those who know binary, and those who don't.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged


Gosport, UK
Offline Offline
Faraday Member
**
Karma: 21
Posts: 3113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 46
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1] 2 3   Go Up
Jump to: