Learning   Examples | Foundations | Hacking | Links

Examples > Control Structures

For Loop

(aka Knight Rider)

Often you want to iterate over a series of pins and do something to each one. For example, this example lights up a series of LEDs attached to pins 2 through 7 of the Arduino.

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.

This example makes use of 6 LEDs connected to the pins 2 - 7 on the board using 220 Ohm resistors. The sketch makes the LEDs blink in a sequence, one by one using only digitalWrite(pinNumber,HIGH/LOW) and delay(time).

Circuit

click the image to enlarge

image developed using Fritzing. For more circuit examples, see the Fritzing project page

Schematic:

click the image to enlarge

Code