For Loop Iteration (aka The Knight Rider)

Control multiple LEDs with a for loop and.

Often you want to iterate over a series of pins and do something to each one. For instance, this example blinks 6 LEDs attached to the Arduino by using a for() loop to cycle back and forth through digital pins 2-7. The LEDS are turned on and off, in sequence, by using both the digitalWrite() and delay() functions .

We also call this example "Knight Rider" in memory of a TV-series from the 80's where David Hasselhoff had an AI machine named KITT driving his Pontiac. The car had been augmented with plenty of LEDs in all possible sizes performing flashy effects. In particular, it had a display that scanned back and forth across a line, as shown in this exciting fight between KITT and KARR. This example duplicates the KITT display.

Hardware Required

  • Arduino Board

  • 6 220 ohm resistors

  • 6 LEDs

  • hook-up wires

  • breadboard

Circuit

Connect six LEDS, with 220 ohm resistors in series, to digital pins 2-7 on your Arduino.

circuit

Schematic:

schematic

Code

The code below begins by utilizing a

for()
loop to assign digital pins 2-7 as outputs for the 6 LEDs used.

In the main loop of the code, two

for()
loops are used to loop incrementally, stepping through the LEDs, one by one, from pin 2 to pin seven. Once pin 7 is lit, the process reverses, stepping back down through each LED.

1/*
2
3 For Loop Iteration
4
5 Demonstrates the use of a for() loop.
6
7 Lights multiple LEDs in sequence, then in reverse.
8
9 The circuit:
10
11 - LEDs from pins 2 through 7 to ground
12
13 created 2006
14
15 by David A. Mellis
16
17 modified 30 Aug 2011
18
19 by Tom Igoe
20
21 This example code is in the public domain.
22
23 https://www.arduino.cc/built-in-examples/ForLoopIteration
24
25*/
26
27int timer = 100; // The higher the number, the slower the timing.
28
29void setup() {
30
31 // use a for loop to initialize each pin as an output:
32
33 for (int thisPin = 2; thisPin < 8; thisPin++) {
34
35 pinMode(thisPin, OUTPUT);
36
37 }
38}
39
40void loop() {
41
42 // loop from the lowest pin to the highest:
43
44 for (int thisPin = 2; thisPin < 8; thisPin++) {
45
46 // turn the pin on:
47
48 digitalWrite(thisPin, HIGH);
49
50 delay(timer);
51
52 // turn the pin off:
53
54 digitalWrite(thisPin, LOW);
55
56 }
57
58 // loop from the highest pin to the lowest:
59
60 for (int thisPin = 7; thisPin >= 2; thisPin--) {
61
62 // turn the pin on:
63
64 digitalWrite(thisPin, HIGH);
65
66 delay(timer);
67
68 // turn the pin off:
69
70 digitalWrite(thisPin, LOW);
71
72 }
73}

Learn more

You can find more basic tutorials in the built-in examples section.

You can also explore the language reference, a detailed collection of the Arduino programming language.

Last revision 2015/07/29 by SM

Contribute to Arduino

Join the community and suggest improvements to this article via GitHub. Make sure to read out contribution policy before making your pull request.

Missing something?

Check out our store and get what you need to follow this tutorial.