【Control Structures in Arduino】What are they? + Types ▷ 2022

The control structures inside the will help you create the programs for your projects with . For this reason, 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 to carry out the sketch.

Also We will mention the other structures in the that you should 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 for when programming Arduino projects?

Control Structures in the Arduino Programming Environment are tools used to choose a sequence or alternative path within the software structure. This means that, They are instructions that allow breaking the methodical sequence of the codes, since there is a logical expression with which to decide if one route or another is chosen so that both can later reach the same end of the process.

To get one better administration and reading of the programming code, it is necessary to use tabulation correctly in each structure. This will help to find faster the errors or the sequences that you want to repeat.

Types of control structures in Arduino What are all that exist?

There are two types of control structure within Arduino programming, which are:

Decision structures

This type of structure refers to the path that 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 returns a value (true or false) to make the decision of which instructions to execute, based on the response.

In Arduino you will find the following conditional statements:

if

you shall use this tool when you want to know if a variable reached a certain condition.

Syour 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 elsethen what is sought is that, if you do not meet a conditionin this way the program must carry out a certain action.

See also  【 Android Shortcut Bubbles 】 Step by Step Guide ▷ 2022

else

With this control structure tool several conditions can be chosen that are mutually exclusive when a certain situation occurs.

It has to be used with the following structure:

if (condition1) { // do Thing A } else if (condition2) { // do Thing B } else { // do Thing C }

switch-case

It is possible to use this structure when you need the program to decide on a precise instruction when there is the possibility of comparing the value of the variable in relation to values ​​established in advance.

Its syntax is set 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 (as long as they are of the type int Y char). Y labels are the executed constants. It’s 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 They are tools that allow executing the instructions that the program has repeatedly.

Among the most used statements in Arduino are:

for

You can repeat the sequences of the codes that are between braces as many times as that you indicate in the Arduino IDE. For this, it will be analyzed that the conditions are fulfilled so that this series of control structures are executed again.

It is developed following these parameters:

for (initialization; condition; increment) { // statement(s); }

Where initialization means that it will be once and at the beginning of the structure. condition 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 it delivers the condition FALSE). In case the loop returns TRUEthen it will be executed through the increase.

while

With this tool you will be able to execute the loop continuously and as many times as necessary, as long as the condition established at the beginning of the loop between parentheses is met. If it returns a false value, it exits the expression and ends the action.

Its structure is:

while (condition) { // condition must be TRUE or FALSE // statement(s) }

do while

This loop is used in the same way as the aforementioned while statement. But in this case the condition for the structure to be repeated is at the end of the blockenclosed in parentheses.

See also  【 Cydia for iOS 】What is it? + What is it for? ▷ 2022

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 from the sensors } while (x < 100);

goto

This tool is very useful when the program needs to be transferred to a certain 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. Must be use with do, for or while.

An example code is:

int threshold = 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); }

continue

To skip the rest of the sequences included in the program, it is necessary to use this tool together 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 structures in Arduino programming that 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.

Watch below:

Sketch

The sketch, also known as, is the programming structure in which are included all the codes that allow the project to be carried out so that the panel performs the desired actions. It is made up of variables. operator-added functions, setup() and loop() tools, and comments made by the programmer.

Within In this programming environment it is possible to find a message area to show the errors or the correct operation of the program. The toolbar is also used to access the different commands. It is important to mention that the length of a sketch is .ino and for the sketch to work it is necessary that the directory has the same name as the sketch, although it is possible that the sketch is in another filebut it must belong to a directory with the same name.

See also  【Use Messenger Without Facebook】Step by Step Guide ▷ 2022

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 it repeatedly: }

arithmetic operators

The arithmetic operators are symbols that are used within programming of an Arduino software to include functions of Addition, subtraction, multiplication and division. Keep in mind that the returned values ​​are based on the data that has been 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 consider the following example:

int a = 5; int b = 10; int c = 0; c = a + b; // the variable c gets a value of 15 after this statement is executed

If for some reason the operands are of different types, program will take the most important operand and return a value based on that criteria. For example, if there are 2 data (one of type int and the other floatbeing greater) C++ will return a floating point result.

From this the example shown below follows:

float a = 5.5; float b = 6.6; int c = 0; c = a + b; // 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 include symbols that help better programmingbut taking into account a conditional variable.

Therefore, it is possible to find in this set the following symbols:

x == y (the variable x is equal to y). Keep in mind that double equals (==) is used, since a single = will represent another assignment. x! = y (variable x is not equal to y) x < y (variable x is less than y) x > y (variable x is greater than y) x < = y (variable x is less than or equal to y) x > = y (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

Loading Facebook Comments ...
Loading Disqus Comments ...