
Si Te gusta electronics and programming You have probably already taken your first steps in the Arduino platform, being today one of the most important tools in everyone to carry out the electronic project development that allow some kind of physical activityThis is how users can rely on different tools that will make it easier for them to use.
For this Arduino users can count on the control structures for learning to programall this with the aim of make work more flexible as you learn to use each of these structures. It's important to keep in mind that there are currently many of them available that will help you in the process of learning to program.
In accordance with all this, here we are going to teach you a little more about what it is. control structure y which are the best projects This type of flexibility is currently available, allowing you to start making your work on this platform more flexible. To do this, carefully follow everything we're about to show you.
What are control structures in Arduino programming and what are they used for?
In the case of Arduino structures of control They function very similarly to programming languages, which is why it's practically impossible to program Arduino without using these controls. And that's because these Control structures allow modification of the flow of execution of instructions of a program.
Thanks to these tools, the following aspects can be achieved:
- Depending on value of a variable, will execute one group or another of statements.
- According to the condition, one group or another of statements will be executed.
- It is capable of executing a group of statements as long as a condition is met. (Do – While)
- Executes a group of statements until a condition is met (Do-Until)
- Execute a group of statements a specified number of times (For-Next).
In the case of programming languages modern present similar control structures. It can be said that what varies between the control structures of different languages is their syntax, so each language has a specific syntax to express the structureTherefore, you can begin to do each one of your Arduino projects much more flexible when learning to use language control structures.
Keep in mind that to write code for your programs you need at least a basic understanding of C language. This is how Arduino uses a C programming language for what the compilation of the sketchesSome of the constructs are specific to the Arduino language, such as the I/O pin assignments. These structures will allow you to take actions based primarily on... certain conditions.
Therefore, the Arduino C language control structure includes the following:
- if
- if…else
- for
- switch case
- while
- do… while
- break
- keep on going
- return
- goth
This is all known as the decision structure and these elements are key to its operation; here we explain the most important ones:
- f: This is a statement used to test whether a specific condition has been met. It is typically used to test if an analog value is present. above a certain number and execute a series of statements that are written inside curly braces if it is trueIf the condition is false and not met, then the program will not execute the operation inside the key.
- If…else: This structure is implemented in response to the idea “If this doesn’t work, do the other thing.” If you want to test a digital input and do one thing if the input was high or do another thing if the input was low.
- For: This is used to repeat blocks of statements between its options. Therefore, an increment/decrement counter It is mostly used to increment/decrement and terminate the loop, mostly this sentence for It is used in combinations with matrices to operate on data/pin collections in Arduino.
- .case: Its purpose is to control the program's flow by specifying within the program the code that should be executed based on certain variables. Accordingly, the switch instruction It is compared with the value of a variable over the values specified in the case instructions.
- While: This is a While loop that executes continuously as long as the expression in parentheses at the top of the loop is true. In this case, the test variable must change to exit the loop. This entire situation can change depending on an expression within the loop. loop code or also by the change of a value in an esensor input.
- .while: It works the same way as the while loop, But unlike the previous method, the condition is tested at the end of the loop, so the loop will always run at least once.
- Break: Break: It is used in the instructions do, for, while Therefore, it allows you to exit the loop in a way different from the one indicated in the loop.
- Continue: It is used in the instructions do, for, while to skip the rest of the instructions that are between curly braces and go to the next execution of the loop, thus checking the conditional expression.
- goto: Transfers program flow to a point in the program that is labeled.
List of simple projects with control structures that you can do yourself to learn to program
Currently, several types of Arduino projects with control structures that you can do yourself at home to get started Learn to program now through this platformThat's why we're going to show you some projects you can consider if you like... programming and electronics.
To do this, carefully follow everything we are going to teach you below:
Threshold Alarm
To begin, you must do so from the sketch, which you must modify so that instead of turn on an LED When the threshold value is exceeded, simulate sending an alarm message through the Serial.printLn Each time an alarm is triggered and the threshold value is exceeded, but this will only happen the first time it is exceeded, not continuously. Now, when the threshold value falls below the threshold, an alert will be generated. recovery messageSimilarly, only when it goes down for the first time and not continuously.
Connection diagram
Here you must Run the sketch and check that it works.After this you must Add an alarm and recovery text when the value is below or above the threshold. Once you've verified that the text is being sent continuously, you should consider how to modify the sketch so that it's only sent when the threshold is exceeded for the first time or when it returns to its normal state for the first time. This also allows you to use the digitalwrite once instead of having to do it continuously.
Flowchart
Visualino solution
Here you should see in the serial plotter the graphic representation of what is happening.
This will vary depending on the color displayed; in this case, each color means the following:
- Blue: potentiometer reading.
- Green: State of emergency.
When selecting the area of “Print Values” It can be seen that it is only printed by alarm screen and recovered alarm, when the threshold is crossed, but not continuously.
Hysteresis
It consists of being able to check the effect of the program when you are right at the threshold where you will have continuous alarms and recoveriesTo solve this, it is important to add hysteresis.
Flowchart
Visualino solution
It exhibits a change in behavior with a average threshold of 400 and one hysteresis of 50. This way you can see how the threshold changes when going from an alarm state to a recovered state and vice versa.
Hysteresis with temperature probes
This is the same example as before. but this time with the TMP36 temperature probe with and without hysteresis.
Connection diagram
Repeat this same example. with the temperature probe and DHT11 humidity.
A satisfactory solution
To find the solution, you will need to access the following links, depending on the case:






















