4 – A circuit with multiple LEDs

Objectives

• Learn how to use iterations in C++, the for statement
• First variables in C++
• Assemble a prototype with several LEDs.
• Learn to program… by programming

Bill of materials

 Arduino Uno or equivalent. We can use any other Arduino model in this chapter. We will also need a PC computer with the Arduino IDE properly installed and set up. A Breadboard. 8 x LED diodes. A 330 Ohm resistor. Some jumper wires

a CIRCUIT with several LED: for ITERATIONS

In previous chapters we learned how to handle a LED diode connected to the breadboard. If we would like to assemble a prototype with eight LEDs, where the light shifts from one LED to another, we could repeat several times the same sequence of instructions we have used before with only one LED.

If we connect multiple LEDs to different digital Arduino pins we must set them up first inside the setup() function.

```void setup()
{
// initialize the digital pins as outputs
pinMode( 13, OUTPUT);
pinMode( 12, OUTPUT);
pinMode( 11, OUTPUT);
...
pinMode( 6, OUTPUT);
}```

And, of course, the loop() function should repeat, as many times as LEDs we use, the sequence of instructions needed to turn on and off each LED in order, from pin 13 to 6 in our case.

We call this a brute force solution, but it is not very smart and is quite hardworking besides. We also would probably make some mistakes when typing, because people are likely to make mistakes while doing boring and repetitive tasks (and this one is boring to death! Could you imagine programming a circuit with 16 LEDs?).

Computers don’t get bored, however, and C++ provides an easy method to tell them that they must repeat something a defined number of times. The method uses the for statement that we can use in combination with a variable.

• A variable is like a box that can contain several values. In this case it will accept values ranging from 6 to 13.
• In C++ we must declare the type of each variable before using it. In this case we use the integer data type, int, to indicate that the variable is numeric and integer, without any decimal places.
• We will see later that there are other types of variables. We will deal with this matter again in the following chapters.

So, for instance, we could use the for instruction as follows to initialize the pins from 13 to 6 as outputs:

``` void setup()
{
int i = 0;    // Initialize the variable i as an integer
for ( i = 6 ; i < 14 ; i++)
{
pinMode(i,OUTPUT);
}
}```

Although syntax seems to be complicated at first, you get used quickly. The important thing to remember is that the for statement needs three parameters separated by a semicolon.

These are the three parameters in order:

• A variable that takes some values according to a certain rule. We must assign it an initial value. In this case: i = 6.
• The loop continues as long as the condition fulfils. In this case it will happen while i is less than 14, that is, until i is 13: i <14.
• The third parameter indicates how the variable does change in each iteration. In this case, i++ asks C++ to increase by one the value of the i variable at the end of each iteration.

We can also use the for statement inside the loop() function and write the following sketch:

``` void loop()
{
int i = 0 ; // We initialize the variable i as an integer
for ( i = 6 ; i < 14 ; i++)
{
digitalWrite(i,HIGH);
delay(500);
digitalWrite(i,LOW);
delay(500);
}
}```

The sketch we did in the chapter 3 was quite similar but in that case we only assigned the value for the pin 13, the one that had a LED connected. Here we choose what pin to use via the i variable, that takes values from 6 to 13.

• You must realize that you must not write a semicolon after a for instruction as it applies to the block of instructions that follows inside the braces as a whole, as in the case of the loop() function. Each iteration executes the four instructions that follow the for statement because they are inside a block of instructions.
• The instructions related to blocks of code don’t take a semicolon at the end of the line.
• When we only need one line of code inside a block we can omit braces, as in the case of a for statement or in the setup() function.

Circuit schematic diagram

.

The circuit schematic diagram is very similar to that of the chapter 3, except for the fact that we place now 8 LEDs in the breadboard.

The only difference between them, since the function of the resistor is to limit the current intensity along the circuit and all diodes have common ground, is that we only need one resistor between this point and ground.

When our program set high the corresponding output pin, the circuit will close and the corresponding LED will light.

Using this circuit and the program 4.1, described before, we can obtain a lighting effect similar to KITT’s lights in “Knight Rider” (or to the Zylon’s helmet for those who are fans of Galactica).

We include next the circuit  wiring diagram for the breadboard:

• In general, it is considered good practice (we do strongly recommend it) to assemble the prototypes following the circuit schematic diagram instead of using the circuit wiring diagram (the one for the breadboard).
• The main reason is that if you use the circuit schematic diagram, means that you fully understand how the circuit works and the temptation to copy the practice without understanding it is avoided.
• In addition, the circuit schematic diagram is the full description of itself and its function tends to be easier to understand. However, as circuits become more complex, to understand its function from a circuit wiring diagram can get very complicated, and even worse, lead to misinterpretation.

Variants of the sketch with the same circuit

.

The assembly we have just made let us play with LEDs so that we can write several different sketches to get different lighting effects.

For example, using the previous sketch 4.1, the lighting effect is not exactly that of Knight Rider’s car because when the for statement ends each iteration the sketch starts again from the beginning, what makes the light jump from pin 6 to pin 13.

So, could we make a light bounce?  Think about it.

Of course, we only have to use two for loops as in the following example.

```void setup()
{
int i = 0 ;                         // We initialize the variable i as an integer
for ( i = 6 ;  i < 14 ; i++)
pinMode(i,OUTPUT);              // We set all the pins as OUTPUT
}
void loop()
{
for ( int i = 6 ;  i < 14 ; i++)  // We define the variable i on the go
{
// We turn on and off the LEDs forward incrementing the variable i, i++
digitalWrite(i,HIGH);
delay(500);
digitalWrite(i,LOW);
delay(500);
}
for (  int i = 12 ;  i >6 ; i--)   // We define the variable i on the go
{
// We turn on and off the LEDs backward decrementing the variable i, i--
digitalWrite(i,HIGH);
delay(500);
digitalWrite(i,LOW);
delay(500);
}
}```
• The first for loop makes the LEDs turn on and off in sequence, from pin 6 to pin 13. The second for loop starts immediately after, beginning with the pin 12 (not to repeat the last pin) and ending with the pin 7 (not to repeat the first pin), and then starts again.
• In the second for loop we have used a countdown instead, decrementing the i variable in each iteration by means of the decrement operator –, i–.
• We have also taken advantage of that C ++ allows us to define variables on the fly within the for instruction itself, without having to spend a full line to declare and initialize it.
[/three-fourth]

We could try another variant, a wave effect in which the lights go up leaving the previous LEDs lit until the maximum is reached and then shutting down the upper LEDs. We recommend trying to solve the problem as a challenge, before seeking a solution.

• The art of programming involves to learn the instructions of a language (the “easy” part) and, on the other hand (the most difficult), to learn how to split a problem in pieces so that we can use these instructions to give instructions to a computer to carry them out.
• These sequential procedures that allow us to solve a certain type of problems is what is known as an algorithm.
• Depending on the problem we address, the algorithm will be more or less complicated, but learning to program has more to do with developing the skill to solve problems in a logical sequence of steps so that can be later encoded in a computer.
• By the way, anyone can learn to program, trust in me, but to some may take longer than to others to develop the necessary skills. At the beginning most people tell they have headaches when thinking on these matters, but we encourage you to keep on (little by little if necessary) because it does worth the while.

Summary

.

In this chapter we have learnt some important things:

• The for statement let us iterate through a block of instructions as many times as we like.
• We have seen our first data type in C++: the integer data type.
• We have introduced the concept of algorithm, that is, a sequential procedure to solve a specific problem and we have later applied it to several simple programs using LED diodes.