If you like electronics and programming, you have surely already taken your first steps on the Arduino platform , being today one of the most important tools in the world to carry out the development of electronic projects that allow you to carry out some type of physical activity . This is how users can count on different tools to facilitate their use.
For this , Arduino users can count on the control structures to learn to program, all this in order to make the work more flexible while learning to use each of these structures. It is important to note that currently you can find a large number of them that will help you in the learning process when programming .
In accordance with all this, here we are going to teach you a little more about what the control structure is and what are the best projects of this type that you can currently find and thus be able to begin to make your work more flexible on this platform. To do this, follow in detail everything that we are going to teach you below.
What are control structures in Arduino programming and what are they for?
In the case of Arduino, the control structures work in a very similar way to programming languages, which is why when programming Arduino it is practically impossible not to use these controls. And it is that these control structures allow modifying the flow of execution of the instructions of a program.
Thanks to these tools, the following aspects can be achieved:
- Depending on the value of a variable, it will execute one group or another of statements.
- According to the condition, one group or another of sentences will be executed.
- It is capable of executing a statement group as long as a condition is met (Do – While)
- Execute a statement group until a condition is met (Do-Until)
- Execute a statement group a specified number of times (For-Next).
In the case of the programming languages modern exhibit structures similar control. It can be said that what varies between the control structures of the different languages is their syntax, so each language has its own syntax to express the structure . Therefore, you can start to make each of your Arduino projects much more flexible when learning to use the language control structures.
Keep in mind that to be able to write codes for your programs you need at least a minimum knowledge of the C language. This is how Arduino uses a C programming language for what is the compilation of scketchs . Some of the constructs are particular to the Arduino language like the writing of the I / O pins. In the case of these structures, they will allow you to take actions based mainly on certain conditions .
That is why the Arduino C language control structure includes the following:
- if … else
- switch case
- do … while
All this is known as the decision structure and they are key to operation, this is how we explain the most important of them here:
- if: It is a statement that is used to test if a certain condition has been achieved. Normally it is used to test if an analog value is above a certain number and execute a series of statements that are written in braces if it is true, in the case that it is false and the condition is not met, then the program will not execute the operation that is inside the key.
- If … else: This structure is executed in response to the idea “If this does not comply, do the other” if you want to test a digital input and do one thing if the input was high or do something else if the input was low.
- For: This is used to repeat block of statements between its options. Therefore, an increment / decrement counter is mostly used to increment / decrement and terminate the loop, mostly this for statement is used in combination with arrays to operate on Arduino data / pin collections.
- .case: It aims to control the flow of the program by specifying in the program the code to be executed based on certain variables. Accordingly, the switch statement is compared to the value of a variable over the values specified in the case statements.
- While: This is a While loop that has a continuous execution as long as the expression placed between parentheses in the loop header is fulfilled. In this case the test variable will have to change to get out of the loop. This whole situation may change at the expense of an expression within the loop code or also by the change of a value in a sensor input.
- .while: Works the same way as the while loop, but unlike the condition being tested at the end of the loop, so the loop will always execute at least once.
- Break: It is used in the do, for, while instructions so it allows you to exit the loop in a different way than the one indicated in the loop.
- Continue: It is used in the do, for, while instructions to skip the rest of the instructions that are between braces and go to the next execution of the loop thus checking the conditional expression.
- Goto: Transfer the 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 you can find several types of Arduino projects with control structures that you can do yourself at home in order to start learning and programming through this platform . That is why here we are going to show you some projects that you can take into account if you like programming and electronics .
To do this, follow in detail everything that we are going to teach you below:
To start you must do it from the sketch, which you must modify so that instead of turning on a led when it exceeds the threshold value, simulate the sending of an alarm message through Serial. printLn every time an alarm exists and the threshold value is exceeded, but this will only happen when it is exceeded the first time and not continuously. Now when the threshold value is below a recovery message is generated , in the same way only when it goes down for the first time and not continuously.
Here you must run the sketch and check its operation . After this you must add an alarm and recovery text for when the value is below or exceeds the threshold. When it is already verified that the text is sent continuously, you should think about how to modify the sketch so that it is only sent when it is overcome for the first time or when it has returned to its normal state for the first time. This also allows you to use the digitalwrite once instead of having to do it continuously.
Here you should see in the serial plotter the graphic representation of what is happening.
This will vary depending on the color shown, in this case each color means the following:
- Blue: potentiometer reading.
- Green: Alarm status.
By selecting the “Print Values” area, it can be seen that only the alarm and recovered alarm are printed on the screen, when the threshold is passed, but not continuously.
It consists of being able to check the effect of the program when it is just at the threshold where there will be continuous alarms and recoveries . In order to solve this it is important to add hysteresis.
It presents a change in behavior with an average threshold of 400 and a hysteresis of 50. In 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
It is the same previous example but this time with the TMP36 temperature probe with and without hysteresis.
Carry out this same example with the DHT11 temperature and humidity probe.
For the solution it will be necessary to access the following links as the case may be: