In our previous tutorial, we discussed variables in arduino programming and today, we shall discuss arduino control flow, which spans control structures and control statements.
Arduino Control Flow
Suffice it to say that realization of a good program lies on the use of program control flow. Arduino program is single threaded and sequential.
Single threaded means lines of codes are executed one code at a time, while sequential means onecode line is executed after another. This method of program execution can be referred to as “program control flow” as the name implies, we actually ‘mean how to control the flow of the arduino program.’
As stated above, the arduino code executes sequentially one after the other down the code lines, however, situations may arise in a program where the program execution is expected to jump steps or return backwards even without getting to the last line of the code, even in situations like this, the rule of thumb is that the program flow must remain logical without errors.
To tackle such situations, we need to structure the program in such a way that the flow will be logical and error free using structural logical statements to control the program flow.
Arduino control structure and control statements
The control structure tells how the codes are organized to enable the program take actions based on certain conditions. Take for example; you want your program to turn on an LED only when a sensor recorded 50°C, you have to structure your code in such a way to execute such command accurately.
The method of ensuring such actions in a program can be seen as program control structuring, also known as “control structure”. This is seen especially when certain conditions are present in a program, just as the one stated above. In other to realize a smart and effective control structure in arduino programming we use control statements.
Control statements are elements (functions) in a source code that control the flow of program execution, either to wait, jump, repeat, etc.
Arduino control statements include:
If statement
Else statement
Else if statement
For statement
While statement
Do while statement
Switch case
Continue
IF STATEMENT
IF statement is basically the simplest form conditional control statements, it is a conditional statement. An “if statement” code evaluates a unique condition, and executes a series of instructions or just an instruction if the condition is true.
An if control statement is basically the type of control statement used to program dark activated street lights, the statement evaluates if the environment is dark or not, if the environment is dark, the if statement code instructs the microcontroller to execute a code instruction that will turn on the street light, and if the environment is not dark, the if statement code instructs the microcontroller to execute a code instruction that will not turn off the street light and keep it off until the environment gets dark.
The arduino IF statement flow chart and syntax are shown below:
ELSE STATEMENT
Most time, an IF statement is immediately followed by an ELSE statement, the ELSE statement tells the alternate instruction that should be executed when the IF statement is false. Check the sketch below.
The sketch in the image above is better than the sketch that has only if statement for designing the dark activated street light.
ELSE IF STATEMENT
“Else if statement” is used when we want to check for three different conditions. It includes an IF statement, ELSE IF statement and ELSE statement all in same sketch. See image below.
For statement
For statement is also a conditional statement for arduino control structure used for repetitive operation. As the name implies, it is used to carry out a repetitive operation for a true condition. Take for example, when a telecommunication company warns you that “if you try recharging your phone number with a wrong recharge pin FOR 5 times you will be barred” the condition there is, you can try recharging that number repetitively with a wrong recharge pin for up to four times without being barred, however, at the fifth trail, the condition breaks and you are barred.
For statement gives a condition and checks if the condition still holds, if it does, an action is taking and a repetition can take place, this will keep happening until the condition no longer holds the action that is tied to the condition stops being executed. This can be used to increment an event. For instance, we can use a For statement to fade an LED up and down. See image below.
Usually, for loop is used to repeat a block of code enclosed within curly braces. An increment counter is usually used to increment and terminate the loop.
For Loop Syntax
for(initialization; condition; increment)
{
Statement(s);
}
initialization
: Happens first and once.
condition
: Each time through the loop, condition
is tested; if it’s true
, the statement block, and the increment is executed, then the condition is tested again. When the condition becomes false
, the loop ends.
increment
: executed each time through the loop when condition
is true.
The image in figure 5 above is a simple sketch that can fade an LED connected to on the pulse width modulation (PWM) pins of an arduino board. For more on arduino PWM, check out Arduio tutorial for beginners
While Statement
A while statement is just like an “if statement”except it continues to repeat block of code (a block of code is what is within the curly braces) as long as the condition is true. See example below
Do While Statement
A do while statement is like the else if statement but works in the same manner as the while loop, except that the condition is tested at the end of the loop, hence, the do statement will always run at least once. See the sketch below:
Switch Case Statement
There comes a time in a design, when we wish to have an action taking with respect to a specific result, in a wide range of results. Take for example, let’s say you are trying to monitor the level of water in a tank using an ultrasonic sensor, you wish to turn on an LED forvarious levels of the water in the tank.
Let’s say we are looking at 10 levels. In our arduino code, we would have a variable that records the distance of the water from the ultrasonic sensor, with this distance; we can pick the levels we want.
To program the arduino to light LEDs at the various levels we have chosen, we can use the “if statement”. With the “if statement”, we tell the microcontroller to light an LED if the distance recorded by the ultrasonic sensor is such and such. Using an “if statement” will do a good job, but to make the program elegant, we use the “switch case” statement.
Using the “if statement” will require that we repeat the statement for every level we wish to execute. See image below:
At the other hand, a sketch using a switch case statement will look like this:
Line 1 code initiates the switch statement to begin checking for the conditions.
Line 3 code checks for the condition when the variable distance is 50.
Line 4 code carries out an action if the case 50 is true, i.e. if distance is equal to 50
Line 5 code breaks the loop for the case condition distance equals 50.
Line 6 code checks for case distance equal to 100.
The process repeats to check for all the conditions and breaks after the break function is called. A switch case statement makes a sketch look elegant and smart.
Continue can be used to jump steps in an iteration process. Let’s say you are counting numbers from 1 to 20 and at the same time printing them on the screen, you can use the continue statement to skip printing some numbers in the iteration. See code below.
Line 1 code iterates 1 to 20
Line 3 code creates figures to skip (5 to 8)
Line 5 code calls skip function
Line 7 code prints all numbers from 1 to 20 other than the ones that should be skipped
I hope this tutorial helps. With a sound knowledge of the various program control flow statements we can write different codes to execute different actions with ease.
In our next tutorial we shall discuss arduino libraries. Keep in touch.
Hi,
I really loved your exposition on the ways on controlling flow in Arduino.
Just one suggestion I would like to make; it would really be nice if you made the part that listed the types of control flow hyperlinks to their explanations in the page, for easier navigation.
Thanks!