Using for() for multiple LEDs

I’ve just been learning how to use Arduino so I’m quite new at this…

I want to blink 8 LEDs simultaneously on 8 different pins. I can do it writing out code for each pin, e.g.

int ledPins[] = {2, 3, 4, 5, 6, 7, 8, 9};

void setup() {
  int index;
  for (index = 0; index <= 7; index++) {
    pinMode(ledPins[index], OUTPUT);
  }
}

void loop() {
  lightAll();
}

void lightAll() {
       digitalWrite(ledPins[0], HIGH);
       digitalWrite(ledPins[1], HIGH);
       digitalWrite(ledPins[2], HIGH);
       digitalWrite(ledPins[3], HIGH);
       digitalWrite(ledPins[4], HIGH);
       digitalWrite(ledPins[5], HIGH);
       digitalWrite(ledPins[6], HIGH);
       digitalWrite(ledPins[7], HIGH);       
       delay(1000);    
       digitalWrite(ledPins[0], LOW);
       digitalWrite(ledPins[1], LOW);
       digitalWrite(ledPins[2], LOW);
       digitalWrite(ledPins[3], LOW);
       digitalWrite(ledPins[4], LOW);
       digitalWrite(ledPins[5], LOW);
       digitalWrite(ledPins[6], LOW);
       digitalWrite(ledPins[7], LOW);
       delay(1000);
     }

but I’d like to be able to use for() instead of writing out every line. e.g…

int ledPins[] = {2, 3, 4, 5, 6, 7, 8, 9};

void setup() {
  int index;
  for (index = 0; index <= 7; index++) {
    pinMode(ledPins[index], OUTPUT);
  }
}

void loop() {
  lightAll();
}

void lightAll() {
  int i;
  for (i = 0; i <= 7; i = i++) {
       digitalWrite(ledPins[i], HIGH);
      delay(1000);    
       digitalWrite(ledPins[i], LOW);
       delay(1000);
    }
}

However, this only blinks an individual LED, not all at once, which is what I’d like to do. Is there a way to ask:
for (all pins at once) {
digitalWrite(ledPins*, HIGH);*

  • delay(1000); *
    _ digitalWrite(ledPins*, LOW);_
    _
    delay(1000);_
    _
    }*_
    Thanks!
 for (i = 0; i <= 7; i = i++) {
       digitalWrite(ledPins[i], HIGH);
      delay(1000);   
       digitalWrite(ledPins[i], LOW);
       delay(1000);
    }

Turn all LED’s on in one for () loop , than them turn off in anther for() loop is one way

while(1) // blink all LED's forever 
{
 for (i = 0; i <= 7; i = i++) {
       digitalWrite(ledPins[i], HIGH);
//      delay(1000);   
 //      digitalWrite(ledPins[i], LOW);
//       delay(1000);
    }
delay(1000);

 for (i = 0; i <= 7; i = i++) {
 //      digitalWrite(ledPins[i], HIGH);
//delay(1000);   
       digitalWrite(ledPins[i], LOW);
 //      delay(1000);
    }
delay(1000);
}

There is a huge problem with writing i = i++. That gives undefined behavior. You can write either i = i+1 OR just i++. But you shouldn't use assignment AND increment in the same expression.on the same variable.

Vaclav made the same mistake and his code is subject to undefined behavior as well. But he does have a point about doing it in two separate loops if you want them to act together.

Thanks folks! This works - onto the next challenge...!

Delta_G: Vaclav made the same mistake and his code is subject to undefined behavior as well. But he does have a point about doing it in two separate loops if you want them to act together.

..and it throws the for loop into infinite loop to boot! Nice to know. Thanks.

Vaclav:
…and it throws the for loop into infinite loop to boot!
Nice to know. Thanks.

It depends on what compiler version you’re using. There were some changes after 1.0.6 and the behavior of that sort of construct has changed. Though to be fair it was always “undefined” so they could have had it do anything and it still would have been compliant.