Overview

If you already go through the Blinking LED article, the experiment present in this article will be your next experiment.

running LED, KITT, Knight Rider

Now, let us expand the basic LED setup with a number of blinking LEDs and make them as if they are running back and forth just like KITT in the TV series Knight Rider. If you wonder how the outcome will looks like, you can see the GIF image shown here.

So, in this experiment, we will be using 8 LEDs, each LED is connected to the digital pin available in the Arduino UNO board. If you prefer your experiment to be colorful, you can use various colors of LEDs. There are various colors available in the electronic shop, and the most common colors are red, blue, green, and yellow.

Be Creative

And, if you want to make your project looks aesthetic, instead of placing the LEDs on a breadboard, why not create your own board! Let your creativity bloom!

What You Need?

Before starting the experiment, you may prepare the following components :

  • 9 units of jumper wires (male to male)
  • 8 units of 330 ohm resistor (Orange-Orange-Brown-Gold)
  • 8 units of LED (red/green/yellow)
  • 1 unit of solderless breadboard
  • 1 unit of Arduino UNO board
Electronic components required for building blinking LED circuit (jumper wires, 330 ohm resistors, LEDs, a solderless breadboard, and Arduino UNO board)

Hardware Setup

The hardware setup for this experiment is shown in figure below:

The hardware setup for constructing running LED experiment with Arduino

The hardware setup is similar to the blinking LED experiment, the only difference is that you need to duplicate the LED connection, instead of connecting 1 LED, in this experiment, you will be connecting 8 LEDs. And the connection is from pin D6 to pin D13 with a common ground.

How It Works? (Hardware Setup)

The operation of this setup is similar with the blinking LED, it just that it is copied over 7 additional circuits set in parallel turning on and off in turns, making a movement of “running LEDs”.

Software Setup

In the Arduino IDE, open up a new sketch in the IDE and type the following code for the experiment.

int ledCount = 8;
int ledDelay = 300;
int ledPins[] = { 6, 7, 8, 9, 10, 11, 12, 13 };
void setup() {
  for (int thisLed = 0; thisLed < ledCount; thisLed++){
    pinMode(ledPins[thisLed], OUTPUT);
  }
}
void loop() {
  for (int thisLed = 0; thisLed < ledCount; thisLed++){
    digitalWrite(ledPins[thisLed], HIGH);
    delay(ledDelay);
    digitalWrite(ledPins[thisLed], LOW);
  }
  for (int thisLed = ledCount-1; thisLed >= 0; thisLed--){
    digitalWrite(ledPins[thisLed], HIGH);
    delay(ledDelay);
    digitalWrite(ledPins[thisLed], LOW);
  }
}

How it Works? (Software Setup)

In this sketch, we introduce a few of new things in the programming. In this section, we will cover variable declaration, array, and for loop.

Variable Declaration

Variables are used to store information to be referenced and manipulated in a computer program. There are many types of variables such as integer, float, string, character, float and Boolean. In this code, we use integer type variable because the value that we want to assign to that variable is an integer number. Note that integer number is a whole number (number that is not a fraction or decimal) , and the syntax for assigning integer is int as shown below.

int ledCount = 8;  //variable declaration
int ledDelay = 300; //variable declaration

Every integer has their own name. Here, we have two variables named ledCount, and ledDelay. Since we will be using 8 LEDs, we assigned ledCount with value 8. In this experiment, we want the delay time to be 0.3 seconds, or 300 milliseconds, so we are going to assign ledDelay with value 300. Now let us convert this to a graphical way to understand this better.

Characters explaining the concept of integer in programming

Array

Now that we had understand variable declaration, we need to know what array in programming is. An array is a data structure consisting of a collection of elements, each identified by at least one array index or key. Notice that in the code, we only have one array:-

Characters explaining the concept of array in programming

Every value stored in the array is assigned with array index begin with 0. In this code, we had assigned 8 values inside ledPins[] array which are 6, 7, 8, 9, 10, 11, 12 and 13.

The Use Of for Loop In The void loop()

The use of for loop in the void loop() body function in this code is to initialize the digital pin as output. We can initialize the digital pin one by one. However, to optimize the code structure, we use for loop. So, instead of writing them line by line, we construct a for loop that will give similar result. The format for writing for loop is as shown below:-

for ( initialization; condition; increment ) {
  statement(s);
}

Here is how we are going to achieve it using for loop. First, we initialize the index array, named thisLed to be 0. Then we will increase thisLed value one by one until it reaches 7, which is less than the value of ledCount (ledCount = 8).

for (int thisLed = 0; thisLed < ledCount; thisLed++){
  pinMode(ledPins[thisLed], OUTPUT);
}

for Loop Condition

Notice that we are using increment operators (++) and decrement operators (--) in the code. The operator ++ will increase the values associated with the variable by 1 after the for loop line is executed while the operator –- will decrease the values associated with the variable by 1 after the for loop line is executed.

In this sketch, the first for loop condition starts off with thisLed set as 0 and incremented by 1 after each time it is executed and will stop when the condition thisLed < ledCount which translates to “thisLed is less than 8” is no longer met. The same applies for the second for loop, only in reverse direction where thisLed will decrease by 1 (when thisLed-- is executed) in each iteration from 7 and the loop ends when it reaches 0.

Further Experiments

We can further test the function of array in this section. Instead of having the array from D6 to D3, we will try setting the array from to 13 and back to 6 again in a single line. Try to code as follows:

int ledCount = 14;
int ledPins[] = {6, 7, 8, 9, 10, 11, 12, 13, 12, 11, 10, 9, 8, 7};
int ledDelay = 300;

void setup() {
  for (int thisLed = 0; thisLed < ledCount; thisLed++){
    pinMode(ledPins[thisLed], OUTPUT);
  }
}

void loop() {
  for (int thisLed = 0; thisLed < ledCount; thisLed++) {
    digitalWrite (ledPins[thisLed], HIGH);
    delay (ledDelay);
    digitalWrite (ledPins[thisLed], LOW);
  }
}

So, what is the difference compared to the last one? We can see that in the void loop() function, we need only one for loop instead of two since the setting of HIGH status to the pins is done in a single coding as we have set the array to count up and down which is in this line:

int ledPins[] = {6, 7, 8, 9, 10, 11, 12, 13, 12, 11, 10, 9, 8, 7};

This, however, means that we will invoke pinMode() and set OUTPUT twice for each pin but as this is done only once in the void setup(), it is not much of an issue.

End Of Article

End Of Article