
The control structures within the Arduino IDE They will help you create the programs for your projects with the arduino boardTherefore, it is important that you know the secrets of this programming language tool, which we will show you below.
The first thing we will do is analyze the different types of control structures in Arduino and understand why they are so important for carrying out the sketch.
Also We will mention the other structures in the arduino programming You need to know to develop your own projects from scratch. Pay attention to every detail to become a true electronics expert.
What are control structures and what are they used for in Arduino project programming?
Control structures in the Arduino programming environment These are tools used to choose an alternative sequence or path within the software structure. This means that, These are instructions that allow breaking the methodical sequence of the codes, since there is a logical expression with which a decision must be made. if one route or another is chosen so that both can eventually reach the same end of the process.
To get one For better management and readability of programming code, it is necessary to use tabulation correctly in each structure.This will help to find errors or sequences that need to be repeated more quickly.
Types of control structures in Arduino: What are all the existing ones?
There are two types of control structure within Arduino programming, which are:
Decision structures
This type of structure refers to the path the program must take when there is a variable that divides the sequence of the codes in two. To do this, a condition is evaluated and a value (true or false) is returned to decide which instructions to execute, depending on the answer.
In Arduino you will find the following conditional statements:
if
You need use this tool when you want to know if a variable reached a certain condition.
SThe syntax is:
if (condition) { // Where condition is an expression of TRUE or FALSE. //statement(s) }
For example:
if (x > 120) digitalWrite(LEDpin, HIGH); if (x > 120) digitalWrite(LEDpin, HIGH); if (x > 120) { digitalWrite(LEDpin, HIGH);} if (x > 120) { digitalWrite(LEDpin1, HIGH); digitalWrite(LEDpin2, HIGH); } // everything is correct
In case of adding elseSo what we're looking for is that, if it does not meet a conditionIn this way, the program must perform a specific action.
else
With this control structure tool Several conditions can be chosen that are mutually exclusive when a certain situation occurs.
It must be used with the following structure:
if (condition1) { // do Thing A } else if (condition2) { // do Thing B } else { // do Thing C }
switch-case
This structure can be used when the program needs to decide on a specific instruction. when there is the possibility of comparing the value of the variable in relation to values established beforehand.
Its syntax is established as follows:
switch (var) { case label1: // statements break; case label2: // statements break; default: // statements break; }
Where var is the variable that the program has to compare with other data (provided they are of the type int y tank). AND label These are the executed constants. It is used break to exit the switch and default to execute the code block when the requested conditions are not met.
Repetition structures
These loop structures These are tools that allow the program's instructions to be executed repeatedly.
Among the most commonly used statements in Arduino are:
for
You can repeat the code sequences found between braces as many times as you want. that you specify in the Arduino IDE. To do this, we will analyze whether the conditions are met for this series of control structures to be executed again.
It is developed following these parameters:
for (initialization; condition; increment) { // statement(s); }
Where initialization This means that it will be there once and at the beginning of the structure. Condition This is the statement that must occur in the program for it to enter the loop in question (keep in mind that the loop will end when the condition is met). FALSE). In case the loop returns TRUE CODEthen it will be executed by means of the increments.
while
With this tool you can run the loop continuously and as many times as necessary, provided that the condition established at the beginning of the loop in parentheses is met. If it returns a false value, it exits the expression and the action ends.
Its structure is:
while (condition) { // the condition must be TRUE or FALSE // statement(s) }
do-while
This loop is used in the same way as the previously mentioned "while" statement. But In this case, the condition for the structure to be repeated is found at the end of the block., enclosed in parentheses.
Its syntax is:
do { // statement block } while (condition);
An example of this is:
int x = 0; do { delay(50); // to wait for the sensor to stabilize x = readSensors(); // reading the sensors } while (x < 100);
goth
This tool is very useful when The program needs to be transferred to a specific point, according to the previously established label.
For example:
for (byte r = 0; r < 255; r++) { for (byte g = 255; g > 0; g--) { for (byte b = 0; b < 255; b++) { if (analogRead(0) > 250) { goto bailout; } // more program code declarations } } } bailout: // more declarations
break
When you need to exit the control structure, you will have to use this statement. It must be Use with do, for or while.
An example code is:
intthreshold = 40; for (int x = 0; x < 255; x++) { analogWrite(PWMpin, x); sens = analogRead(sensorPin); if (sens > threshold) { // rescue on sensor detection x = 0; break; } delay(50); }
keep on going
To skip the remaining sequences in the program, you need to use this tool along with do, for or while.
For example:
for (int x = 0; x <= 255; x ++) { if (x > 40 && x < 120) { // create jump in values continue; } analogWrite(PWMpin, x); delay(50); }
Other Arduino programming structures you should know to develop your own projects
In addition to the structures mentioned aboveThere are other tools that should also be taken into account when programming your Arduino board.
See below:
Sketch
The sketch, also known as Arduino sketch, It is the programming structure in which are included All the code that allows the project to proceed so that the board performs the desired actions. It is composed of variables, functions added by the operator, the setup() and loop() tools, and comments done by the programmer.
Within In this programming environment, it is possible to find a message area. to show errors or the correct functioning of the program. The toolbar is also used to access the different commands. It is important to mention that the extension of a sketch is .ino And for the sketch to work, the directory must have the same name as the sketch. although the sketch might be in another filebut it must belong to a directory with the same name.
The structure of the sketch is:
void setup() { // Add your setup code here to run once: } void loop() { // Enter your main code here to run repeatedly: }
Arithmetic operators
The Arithmetic operators are symbols used within programming of Arduino software to include functions of Addition, subtraction, multiplication and division. It is important to keep in mind that the returned values depend on the data defined in the operands, for example, int (in this case the results obtained will not have decimals).
Taking the above into account, it is possible to propose the following example:
int a = 5; int b = 10; int c = 0; c = a + b; // the variable c It returns a value of 15 after this statement is executed.
If for any reason the operands are of different types, The program will take the most important operand and return a value based on that criterion. For example, if there are two pieces of data (one of type int and the other float(if this is greater) C++ will return a floating-point result.
This leads to the example shown below:
float a = 5.5; float b = 6.6; int c = 0; c = a + b; // the variable 'c' stores a value of 12 only as opposed to the expected sum of 12.1
Comparison operators
Like the arithmetic operatorsIn this group, They include symbols that help with better programming.but taking into account a conditional variable.
Therefore, it is possible to find the following symbols in this set:
x == y (the variable x is equal to y). Keep in mind that the double equals sign (==) is used, since a single = will represent a different assignment. x ! = y (the variable x is not equal to y) x < y (the variable x is less than y) x > y (the variable x is greater than y) x <= y (the variable x is less than or equal to y) x >= y (the variable x is greater than or equal to y)
An example of a programming structure with comparison operators is:
if (x > 120) digitalWrite(LEDpin, HIGH); if (x > 120) digitalWrite(LEDpin, HIGH); if (x > 120) { digitalWrite(LEDpin, HIGH);} if (x > 120) { digitalWrite(LEDpin1, HIGH); digitalWrite(LEDpin2, HIGH); } // all conditionals are correct

















