loop-arduino

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:
if_else

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:
for

We will create this program giving step-by step explanations.

Let´s analyse the blocks:

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.

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:

First of all

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:

  1. All off
  2. LED 2 on
  3. LEDs 2, 3 on
  4. LEDs 2, 3, 4 on
  5. LEDs 2, 3, 4, 5 on
  6. LEDs 2, 3, 4, 5, 6 on

…and so on. But what would the code be like?

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:

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.