If you like electronics and programming surely you have 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 activity. Thus, for this, users can count on different tools that will facilitate their use.
For it Arduino users can count on the control structures to learn to program, all this with the purpose of make work more flexible while learning to use each of these structures. It is important to bear in mind that currently you can find a large number of them that will help you in the process of learning when programming.
In accordance with all this, here we are going to teach you a little more about what the control structure 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 structures of control They 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 is that said control structures allow modifying the execution flow of instructions of a program.
Thanks to these tools, the following aspects can be achieved:
- Depending on the value of a variable, will execute one group or another of sentences.
- In accordance with 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 programming languages modern present similar control structures. It can be said that what varies between the control structures of the different languages is their syntax, so each language has a proper syntax to express the structure. So you can start doing each of your Arduino projects much more flexible when learning to use language control structures.
Keep in mind that to be able to write code 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 a 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 whether a certain condition has been achieved. It is normally used to test if an analog value is found by above a certain number and execute a series of statements that are written within curly braces if trueIn 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 statements between your options. Thus, an increment / decrement counter is mostly used to increase / decrease and terminate the loop, mostly this for statement is used in combinations with matrices to operate on collections data / pins on Arduino.
- .case: Its objective is to control the flow of the program by specifying in the program the code to be executed based on certain variables. Agree with this, the switch statement it is compared to the value of a variable over the values specified in the case statements.
- While: It is a While loop that has a continuous execution as long as the expression enclosed in parentheses in the head of the loop is satisfied. 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 an einput from a sensor.
- .while: It works the same way as the while loop, but unlike the condition is tested at the end of the loop, so the loop will always execute at least once.
- Break: It is used in the instructions do, for, while so it allows you to exit the loop in a different way than that indicated in the loop.
- Continue: It is used in the instructions do, for, while to skip the rest of the instructions that are in braces and go to the next execution of the loop thus checking the conditional expression.
- Goto: Transfers 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 to start learn and program through this platform. That is why here we are going to show you some projects that you can take into account if you like the 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 turn on a led when it exceeds the threshold value, simulate the sending of an alarm message through the Serial.printLn every time there is an alarm 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 that a recovery message, in the same way only when you go 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 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 must 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 state.
When selecting the “Print Values” you can see that it only prints by alarm screen and alarm recovered, when the threshold is passed, but not continuously.
It consists of being able to check the effect of the program when you are just at the threshold where you will have continuous alarms and recoveries. To be able to solve this it is important to add hysteresis.
Has a change in behavior with a mean 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
This is the same example above but this time with the TMP36 temperature probe with and without hysteresis.
Perform this same example with temperature probe and the DHT11 moisture.
For the solution it will be necessary to access the following links as the case may be:
- DHT11: To do this, enter this link.
- TMP36: To do this, access this link.