Have you ever wished you had your own Knight Industries Two Thousand (KITT) car — you know, from the Knight Rider? Take your dream one step closer to reality by creating an LED scanner! Here is the end result:

What you need

This project does not need many details, and there can be many:

  • 1 x Arduino UNO or similar
  • 1 x breadboard
  • 8 x red LEDs
  • 8 x 220 ohm resistors
  • 1 x 10 kΩ potentiometer
  • Man to man connect wires

If you have an Arduino starter kit you probably have all these parts (what can you do with a starter kit? )

Almost any Arduino will work as long as it has eight available pins (Never used an Arduino before? here). You can use shift register programming to drive the LEDs, although this is not required for this project as the Arduino has enough pins.

Construction plan

Arduino-Knight-Rider-Led-Scanner-Complete
This is a very simple project. Although it may look complicated due to the large number of wires, every single part is very simple. Each LED is connected to its own Arduino pin. This means that each LED can be turned on and off individually. The potentiometer is connected to an analog Arduino at the pins that will be used to adjust the speed of the scanner.

Scheme

Arduino-Knight-Rider-Circuit
Connect the outer left pin (when viewed from the front, pins down) of the potentiometer to ground. Connect the opposite outer pin to +5v. If this doesn’t work properly, swap those pins. Connect the middle pin to the Arduino analog via 2.

Connect the anode (long leg) of each LED to digital pins one through eight. Connect the cathodes (short leg) to Arduino ground.

The code

Create a new sketch and save it as «knightRider». Here is the code:

const int leds[] = {1,2,3,4,5,6,7,8}; // Led pins const int totalLeds = 8; int time = 50; // Default speed void setup() { // Initialize all outputs for(int i = 0; i <= totalLeds; ++i) { pinMode(leds[i], OUTPUT); } } void loop() { for(int i = 0; i < totalLeds - 1; ++i) { // Scan left to right time = analogRead(2); digitalWrite(leds[i], HIGH); delay(time); digitalWrite(leds[i + 1], HIGH); delay(time); digitalWrite(leds[i], LOW); } for(int i = totalLeds; i > 0; --i) { // Scan right to left time = analogRead(2); digitalWrite(leds[i], HIGH); delay(time); digitalWrite(leds[i - 1], HIGH); delay(time); digitalWrite(leds[i], LOW); } } 

Let’s deal with this. Each LED pin is stored in an array:

 const int leds[] = {1,2,3,4,5,6,7,8}; 

An array is essentially a collection of related elements. These elements are defined as «const», which means that they cannot be changed later. You don’t need to use a constant (the code will work just fine if you remove «const»), although it’s recommended.

Array elements are accessed using square brackets («[]”) and an integer called the index. Indexes start at zero, so «leds [2]» would return the third element in the array — pin 3. Arrays make code faster and more readable, they make the computer do the hard work!

The for loop is used to set each pin as output:

 for(int i = 0; i <= totalLeds; ++i) { pinMode(leds[i], OUTPUT); } 

This code is inside the "setup()" function as it only needs to be run once when the program starts. Very useful for loops. They allow you to run the same code over and over again, each time with a different value. They are ideal for working with arrays. An integer "i" is declared, and only code inside the loop can access this variable (this is called "scope"). The value of i starts from zero, and for each iteration of the loop, i is incremented by one. As soon as the value of i is less than or equal to the variable "totalLeds", the loop "breaks" (stops).

The i value is used to access the "leds" array. This loop accesses each element in the array and sets it up as an output. You can manually type "pinMode (pin, OUTPUT)" eight times, but why write eight lines when you can write three?

While some programming languages ​​can tell you how many elements are in an array (usually with a syntax like array.length), Arduino doesn't make it that easy (it involves a bit more math). Since the number of elements in the array is already known, this is not a problem.

arduino-knight-rider-leds

Inside the main loop ( void loop() ) there are two more for cycle . The first one sets the LEDs to ON and then OFF from 1 to 8. In the second loop, the LEDs turn on and then OFF. This ensures that there are always two LEDs at the same time, making the scanner more realistic.

At the beginning of each cycle, the value of the bank is read into the variable "time":

 time = analogRead(2); 

This is done twice, once inside each cycle. This needs to be constantly checked and updated. If it were outside of the loops, it would still work, however there would be a slight delay - it would only work after the loop had completed. Pots are analog, which is why "analogRead(pin)" is used. This returns values ​​from zero (minimum) to 1023 (maximum). The Arduino is capable of converting these values ​​into something more useful, however they are ideal for this use case.

The delay between LED changes (or scanner speed) is set in milliseconds (1/1000 of a second), so the maximum time is just over 1 second.

Advanced Scanner

arduino-leds-outside-par

Now that you know the basics, let's look at something more advanced. This scanner will light the LEDs in pairs, starting on the outside and working inside. Then he reverses it and goes from inside to outside in pairs. Here is the code:

 const int leds[] = {1,2,3,4,5,6,7,8}; // Led pins const int totalLeds = 8; const int halfLeds = 4; int time = 50; // Default speed void setup() { // Initialize all outputs for(int i = 0; i <= totalLeds; ++i) { pinMode(leds[i], OUTPUT); } } void loop() { for(int i = 0; i < (halfLeds - 1); ++i) { // Scan outside pairs in time = analogRead(2); digitalWrite(leds[i], HIGH); digitalWrite(leds[(totalLeds - i) - 1], HIGH); delay(time); digitalWrite(leds[i], LOW); digitalWrite(leds[(totalLeds - i) - 1], LOW); delay(time); } for(int i = (halfLeds - 1); i > 0; --i) { // Scan inside pairs out time = analogRead(2); digitalWrite(leds[i], HIGH); digitalWrite(leds[(totalLeds - i) - 1], HIGH); delay(time); digitalWrite(leds[i], LOW); digitalWrite(leds[(totalLeds - i) - 1], LOW); delay(time); } } 

This code is a little more complicated. Notice how both cycles go from zero to "halfLeds - 1" (3). It makes the scanner better. If both loops came out of 4 - 0 and 0 - 4, then the same LEDs would flash twice in the same sequence - it would not look very good.

You should now have a working Knight Rider LED Scanner! It would be easy to change this to use more or more LEDs, or implement your own pattern. This circuit is very easy to port to Raspberry Pi (new to Pi? Start here) or ESP8266

Are you building a KITT replica? I would love to see all the Knight Rider stuff in the comments.

Похожие записи