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
EasyMake a light run along the five LEDs, again and again:
- light each LED in the order of
ledPinsfor 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
}
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Outputs
4Loops and Arrays
Repeat With a for LoopAn Array of LED PinsFive-LED ChaserKnob Bar GraphBinary Counter on LEDsPractice on your own: Arduino playground