You will be learning some new stuff in this lesson:
- How to make a repeated action sequence
- How to iterate with a for loop using different peripherals
We will need the following:
- A controller board from the Arduino family
- Four LEDs
Flow control
Sequential programming consists of executing a series of orders or commands one after the other. These commands can read information (via sensors), perform actions (for example, moving a servo), or simply carry out internal calculations (such as addition). We saw an example in which the order of execution of a program with the if-else statement was altered in the previous lesson, these instructions are called flow control instructions. An example: imagine a program where, if the reading of the amount of light is less than 300, and LED lights, otherwise, turn it off. According to a variable we perform the action switch on or switch off LED. You can see this clearly in the image below:
Performing tasks repeatedly with the for loop
The for loop is another way of modifying how program is executed, using a series of commands which are repeated a given number of times. For example, if we wanted to make an LED blink 10 times, the program flow would be something like this:
We will create this program giving step-by step explanations.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
int led_pin = 5; // pin connected to the LED bool primeraVez = true; //to know when the first time (primera vez) is void setup() { pinMode(led_pin, OUTPUT); } void loop() { // the first time we will make the LED blink 10 times if (primeraVez){ primeraVez = false; // it´s not the first time for (int i = 0; i<10; i=i+1){ digitalWrite(led_pin,HIGH); // light up LED delay(1000); // pause 1 second digitalWrite(led_pin,LOW); // switch off LED delay(1000); //pause 1 second } }else{ digitalWrite(led_pin,LOW); // switch off LED } } |
Let´s analyse the blocks:
1 2 3 4 5 6 7 |
int led_pin = 5; // pin connected to the LED bool primeraVez = true; // to know when the first time is void setup() { pinMode(led_pin, OUTPUT); } |
In this block we use global variables to indicate the number of the pin connected to the LED. We also create a boolean variable (which supports true and false values). We will use this variable to find out if it is the first time the program is being executed. This is a common resource in programming, on executing the loop () function again and again. If we want a program to run only once, it is necessary to do this need or something similar.
1 2 3 4 5 6 7 8 9 10 |
void loop() { // the first time we will make the LED blink 10 times if (primeraVez){ primeraVez = false; // it´s not the first time ... }else{ digitalWrite(led_pin,LOW); // switch off LED } } |
The first time the loop () function is executed, the primeraVez variable is true (verdadero), so we enter into the if, and execute what comes next. The first command is to assign the false (falso) value to the primeraVez variable, so the next time that the loop () function is executed, the condition will not be fulfilled and digitalWrite (led_pin, LOW) will then be executed, which switches the LED off indefinitely.
Now we will analyse the for loop:
1 2 3 4 5 6 |
for (int i = 0; i<10; i++){ digitalWrite(led_pin,HIGH); // switch on LED delay(1000); //pause 1 second digitalWrite(led_pin,LOW); //switch off LED delay(1000); //pause 1 second } |
First of all
1 |
for (int i = 0; i<10; i=i+1) |
The for loop has three parts:
- The variable it is iterated on, and the initial value, which is this case is int i=0. We will iterate with a variable called i, a whole number with an initial value of 0.
- The control condition, which means the for loop will be repeated as long as this condition is true: i < 10 . This verification will be made at the start of each iteration, meaning that if the condition is false the first time, it will not carry out any iteration.
- The action to be executed at the end of the iteration, in this case i=i+1. This means that after each iteration i, a unit is incremented (which could be any other operation). It´s important to note that this is done at the end of the iteration, as i is worth 0 in the first iteration.
The execution sequence would therefore be
- i=0, i < 10 ? : yes, if I blink once, I increment by i. Therefore i=1
- i=1, i < 10 ? : yes, if I blink once, I increment by i. Therefore i=2
- i=2, i < 10 ? : yes, if I blink once, I increment by i. Therefore i=3
- …
- i=9 , i < 10 ? : yes, if I blink once, I increment by i. Therefore i=10
- i=10 , i < 10 ? : no, I leave the for loop.
Iterating the for loop
Another use of the for loops is using the control variable, such as in the previous example i to perform a calculation. Do you remember the lights on the amazing car? We are going to make 5 LEDs switch on sequentially, so that one more lights up each time, and then they all go off. Let´s suppose that the LEDs are connected to pins 2 and 6, the sequence would then be:
- All off
- LED 2 on
- LEDs 2, 3 on
- LEDs 2, 3, 4 on
- LEDs 2, 3, 4, 5 on
- LEDs 2, 3, 4, 5, 6 on
…and so on. But what would the code be like?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
void setup() { // we use the for loop to put the pins on OUTPUT mode for (int i=2;i<=6;i=i+1) pinMode(i, OUTPUT); } void loop() { // we switch all LEDs off for (int i=2;i<=6;i=i+1) digitalWrite(i, LOW); // we switch off LED i delay(100); // pause for 100 milliseconds //we switch on LED 2, 3, 4, 5, 6 sequentially for (int i=2;i<=6;i=i+1){ digitalWrite(i, HIGH); // we switch on LED i delay(200); // pause for 200 milliseconds } } |
In the setup() function, we put all the pins with LEDs in OUTPUT mode. Instead of doing it again and again for each one, we can do it using a for, which saves on code. This would be the equivalent of:
1 2 3 4 5 |
pinMode(2,OUTPUT); pinMode(3,OUTPUT); pinMode(4,OUTPUT); pinMode(5,OUTPUT); pinMode(6,OUTPUT); |
In the loop() function, first we switch off all the LEDs (of the same mode) and then we pause for 100 milliseconds.
Then we switch them on one by one, at rate of 200 milliseconds to achieve the desired effect. Once you have switched them all on, we will start with the loop (), ie, we switch them on and off again, one after another, and so on and so forth.
What would a program that turns them off sequentially instead of all at once look like? In other words, it switches them on one by at a rate of 200 milliseconds, and then switches them off in the same way, but in reverse order.
Go to the Arduino for reference page to find out more.