Pages: [1]   Go Down
Author Topic: Led activating sequentially  (Read 606 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi!

We are stuck in our science project and we are in desperate need of your help! We need to be able to sequentially control eight LEDs using an Arduino Uno. The problem is we have no idea of how to write a suitable code. The LEDs are to be activated in intervals with specific time for on/off for each one. In the first sequence the LEDs must have a special timing unlike the following which will have the same. We have read about delay, loop and interrupt but are unsure of how to use them to fit our purpose.

Best regards
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 212
Posts: 8975
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Here is some untested code that meets your vague requirements.  Perhaps you can use it as an example.

Code:
const int LEDs = 8;

int pins[LEDs] = {2,3,4,5,6,7,8,9};
int initial_on_intervals[LEDs] = {26, 35, 197, 15, 3, 254, 1078, 25};
int initial_off_intervals[LEDs] = { (eight numbers) };
int ongoing_on_intervals[LEDs] = { (eight numbers) };
int ongoing_off_intervals[LEDs] = { (eight numbers) };

void setup()
{
for (int i=0; i<LEDs; i++)
{
pinMode(pins[i], OUTPUT);
digitalWrite(pins[i], LOW);
for (int i=0; i<LEDs; i++)
{
digitalWrite(pins[i], HIGH);
delay(initial_on_intervals[i]);
digitalWrite(pins[i], LOW);
delay(initial_off_intervals[i]);
}
}

void loop()
{
for (int i=0; i<LEDs; i++)
{
digitalWrite(pins[i], HIGH);
delay(ongoing_on_intervals[i]);
digitalWrite(pins[i], LOW);
delay(ongoing_off_intervals[i]);
}
}

Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Colorado
Offline Offline
Edison Member
*
Karma: 47
Posts: 1562
Reviving dead brain cells with Arduinos.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I prefer not using delay() because that will interfere with other parts of your code that you may be waiting on.  So with that in mind, I use millis(0 to determine whether something needs to happen or not.  The following code will turn everything on in sequence, then turn everything off in sequence.  You can adjust it to your specific application.
Code:
DISCLAIMER: this is untested code, which could contain errors. use at your own discretion.

int resetDone = 0;        // Used to reset the full array of LEDs
int pauseInterval = 50;   // how long to pause between each LED
long lastUpdate;          // lastUpdate will get big rather fast

void setup() {
  // Assume you're using 5 sequential pins (starting at 2) for your LEDs, let's set their output mode
  for (int p = 2; p <= 7; p++) {
    pinMode(p, OUTPUT);
  }
}

void resetAll() {
  for (int p = 2; p <= 7; p++) {
    digitalWrite(p, LOW);
    resetDone = 1;
  }
}

void loop() {
  // I like to reset everything before starting up again
  if (!resetDone) {
    resetAll();
    // p is also used as a counter down below
    p = 2;
  }
  if (millis() - lastUpdate > pauseInterval) {
    // anything in here will happen only if time elapsed is bigger than pauseInterval
    digitalWrite(p, (digitalRead(p) == LOW ? HIGH : LOW));
    lastUpdate = millis();
    p++;
    if (p > 7) {
      p = 2;  // Start over
    }
  }
}
If you need them to turn on, then off before moving on to the next LED, then change the time code if routine to turn on and off before incrementing the value of p.
Logged

Vejen, Denmark
Offline Offline
Edison Member
*
Karma: 9
Posts: 1178
RAWR! ^,..,^
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

http://captain-slow.dk/2010/10/31/using-millis/
Logged

captain-slow.dk | non contagious!

Pages: [1]   Go Up
Jump to: