Menu
CoddyTech

Five-LED Chaser

Part of the Outputs section of Coddy's Arduino journey. Lesson 17 of 24.

Put the for loop in loop() and it runs again and again. With one LED lit at a time, a light runs along the row like a chaser:

for (int i = 0; i < 5; i++) {
  digitalWrite(ledPins[i], HIGH);
  delay(150);
  digitalWrite(ledPins[i], LOW);
}

Each round of the for loop lights one LED, waits, and turns that same LED off before moving on, so the next round lights the next one.

When the for loop ends, loop() ends too and starts over, so the light jumps back to the first LED and runs along the row again.

challenge icon

Challenge

Easy

Make a light run along the five LEDs, again and again:

  • light each LED in the order of ledPins for 150 ms
  • only one LED is on at a time

Press Run, then Check.

Try it yourself

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

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

void loop() {
  // light each LED in turn for 150 ms, one at a time
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Outputs

Practice on your own: Arduino playground