Arduino Knight Rider KITT Lights

After I had the LEDs plugged into my breadboard for the watch project I’m working on I couldn’t help but make a quick sketch that flashes the lights like the light on KITT from Knight Rider.

 

The code is very simple.  First we just set up all of the pins we use as output pins:

void setup(){
//Set the pins we use as OUTPUT
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
}

The next and last thing we do is turn on and off the lights in sequence with a small delay in between each like so:

void loop(){
//Flash from the lower numberd pins to the higher numbered ones...
digitalWrite(3, HIGH);
delay(80);
digitalWrite(2, LOW);
delay(80);
digitalWrite(4, HIGH);
delay(80);
digitalWrite(3, LOW);
delay(80);
digitalWrite(5, HIGH);
delay(80);
digitalWrite(4, LOW);
delay(80);
digitalWrite(6, HIGH);
delay(80);
digitalWrite(5, LOW);
delay(80);
digitalWrite(7, HIGH);
delay(80);
digitalWrite(6, LOW);
delay(80);
digitalWrite(8, HIGH);
delay(80);
digitalWrite(7, LOW);
delay(80);
//and back the other way again
digitalWrite(7, HIGH);
delay(80);
digitalWrite(8, LOW);
delay(80);
digitalWrite(6, HIGH);
delay(80);
digitalWrite(7, LOW);
delay(80);
digitalWrite(5, HIGH);
delay(80);
digitalWrite(6, LOW);
delay(80);
digitalWrite(4, HIGH);
delay(80);
digitalWrite(5, LOW);
delay(80);
digitalWrite(3, HIGH);
delay(80);
digitalWrite(4, LOW);
delay(80);
digitalWrite(2, HIGH);
delay(80);
digitalWrite(3, LOW);
delay(80);
}

This could also be done much more efficiently would be much easier to change the number of and position of the lights or the speed the array blinks at just using the loop and incrementing the value of the pins being set high or low.  A while loop could even be used in the set up to make the pins all output pins and the highest and lowest value pins as well as the speed could all be defined at the top of the sketch.  Maybe I’ll code that up sometime and post it but as this sketch stands now it is really just a glorified Arduino “Hello World.”

Here is the source in a .zip folder: Knight Rider KITT Lights Arduino Sketch.