I had a lesson in mind, but when I put it together it ballooned out of control because of the underlying concepts that you would need to know. As such, I’ve decided to just teach the concepts on this go around, and we’ll put them together in a bit.
Fun words of the day:
Serial.print( );
Serial.println( );
These two commands are used with the Arduino’s serial monitor. It’s basically a viewable window in the Arduino program that you can use to print information on. It’s mostly used to monitor the activity of a program. The first will print whatever is in between the ( ), and the second will do the same and also print a line break (like hitting return for a new line). You can also print from data types like int (integers). NOTE, if printing plain text, IT MUST be inside quotation marks, if printing variable values, IT MUST NOT be in quotation marks. In the last lesson we had an integer from 0-1023 that came from an analog read of the potentiometer. You could print that out as part of the loop, and watch it change on the screen too. Another example might be to print messages to yourself from inside the code as it’s waiting on something or when it has completed a certain task.
NOTE, these commands can only be used if you put the following in the setup of your program.
Serial.begin(9600);
This is the command to open up communications between the arduino and your PC.
We’ve previous used int (integers), but there is another type you should know.
float value = 1.2;
In this case I have opened a variable called “value” and assigned it 1.2 You probably noticed it’s not an int, it’s a float. The difference is really just that a float is allowed to have decimals without having them chopped off to fit. If I tried to move the 1.2 in the example into an int, then it would drop the decimal part, and it would become just 1 with no partial number.
Now we start dealing with logic statements.
If( ) {
xxxxx; //insert code here.
}
What the if statement does is check a logic statement, usually just simple math, and if that statement is true, then it executes the code inside the brackets. If the statement is false, it skips everything in the brackets and carries on as if it never happened.
Example.
if(x=0) {
xxxxx; // do the imaginary code here.
xxxxx; // and this too.
}
As the code runs, it will check whatever the value of x is, and if it does equal 0, then the lines of code will be executed. If it’s anything else it will be skipped.
You can also not use the brackets, but it will assume you only mean the next line of code following the if statement.
if(x=0)
xxxxx; // do the imaginary code here.
xxxxx; // but this isn't part of the if statement
Note, the if statement does not get a semicolon ; but the lines after it do get a semicolon ;
for ( ; ; ) {
}
This is the for loop. It is one of the more versatile and powerful commands that you will use. In a nutshell, it runs in a loop using the code you write inside it, until the true-false test (that you give it) becomes false. Then it skips the rest of it and goes on with the next bit of programming. The first space inside the ( ) allows you to set a baseline variable. This will only ever run the first time. The second contains your logic test and is usually mathematical, ie, x=3. If x somehow becomes such that it fails that test, then the for loop aborts. The third space is a change that is made in every loop.
for (int x=0 ; x<2 ; x++ ) {
xxxxx; //any code here
}
In this example, it sets an integer called x to 0, then checks to see if it is less than 2, (this is currently true), so it executes the code inside the { }. Next it adds 1 to x, then it returns to the top.
Now, on the second pass, it checks x to see if it is less than 2. Because we added 1 to x on the first loop it is now (0 + 1 =1) and this is still less than 2, so it goes through the code again. Then adds 1 to x again and then it returns to the top.
On the third pass, it checks to see if x is less than 2. Because we added 1 to x on the second loop it is now (1 + 1 =2), and this is FALSE according to the logic x<2. Therefore, the for loop quits without going through the code and jumps to the next part of the program AFTER the for loop.
Notes about the for loop:
Counting can be tricky. Always work through it by hand to see if you get the exact number of loops you want. You will often find it’s one number less due to the way you set it up.
The code ++ means increase by 1. The same as - - means decrease by 1. You can also use simple math such as -4 or +3 or /5 or *2 to raise or lower the value.
You don’t have to use x, use anything that makes sense to you.
To be continued with an example code.