Книга: Arduino: Master The Arduino Basics
Назад: Chapter 2: The Functions of Arduino
Дальше: Chapter 3: Learning the Arduino Code

So, the first part of our code that we will take a look at are the variables and the data types. When talking about a programming language, the variables are like a label for your Arduino board because they are the parts in charge of associating the right values with their names. Before using your variable inside of a program, or a sketch as they are called in Arduino, you have to stop for a bit and then declare that variable. When you make that declaration, then that is when you start working with the data type.

Now, in programming, the data type is going to be the part that is in charge of telling the program what data types are going to be found inside of each variable type. Without a name or a specific data type present, the variables can't be declared in these sketches. 

There are quite a few types of data that we can work with in these sketches. But we are going to keep this pretty simple to help make it easier. Some of the most common data types that you will find inside of these sketches include:


Declaring your variables is not a process that is too difficult when you are working with the Arduino board. Let's take a look at an example of this. For this one, we will look at a variable that we will call ledpin and we will declare it. To start, we need to pick out a data type for this variable and give it a value of five. In order to declare this one, we would just use the following code:

int ledPin = 5;

And that is really all that you need to do in order to declare a variable in the Arduino board. Of course, there will be some times when it gets a bit more complicated, but it will still follow the same kind of idea no matter what you are working on. One thing that you should keep in mind though is that a semicolon needs to be added to the end of it so that it helps the IDE read through the whole thing properly. Other than that, you are good to go.


The next part of your code that you need to understand is the operators. These operators are a great way to add some more power to the code because they are often used to help manipulate the variable. In some of the programming languages that you choose, there will be quite a few operators to pick from, but Arduino will focus on just the four main ones to keep it pretty simple. Some of the operators that you are able to work with in the Arduino coding language include:

Boolean Operators: This is similar to what we discussed before with the data types. These operators will need to focus on data types that are either true or false or 0 or 1. The symbols that you would use for these operators would include (!) for or, the (&&) for and, and the (||) for not.

Logical Operators: If you are trying to work on a project that needs to do comparisons, you have to work with the logical operators. There are a few options to use such as the (==) symbol which means equal to, the (<=) for less than or equal to, and so on.

Mathematical Operators: These are the symbols you are familiar with any time you head to a math class. They are used in your sketch any time you need to do a math equation of any sort in the code. You will be able to use the signs such as (+), (-), (*), (/, and (%).

Assignment Operators: This one will usually just have one symbol, the equal (=) symbol because it is used to calculate what will be the final value of your data type. This final value is going to show up on the right-hand side of this equal sign. It can also show that the information that is on the right-hand side is equal to the information on the left side. 

You can also work with some operators that are in charge of increments and decrements. For example, if you use the symbol (++) after you name the variable, it means that you just want to add one to whatever the value of the variable is. The same can be true if you go the other way. If you use the (--), you are going to subtract one from the variable.


Next on the list is expressions. When we bring up expressions in the Arduino code, we are able to combine a few of the different data types that we listed above to make our expressions. When these different data types come together, you will be able to add a lot of control to the code that you are working on. 

One example of these expressions would be to create a mathematical expression. This could look as simple as something like a=4*2-6. The compiler, which is the part of your IDE that reads through the code and translates it, will see the equal sign and will take a look at the left-hand side to see what the variable is before going through and computing the answer of the numbers on the right side.

While we are here, we need to remember that when doing the mathematical equations, the compiler will use the MDAS rule to figure out what the answers are. This means that it will multiply everything first, do the division, the addition, and then finish with the subtraction. This is the standard in most mathematical equations so it shouldn't be anything new in this part. This means that the answer we would get from this equation would be a=2. 

You are able to write out expressions that aren't mathematical as well. For example, you can work with logical expressions, which will be the ones that use the Boolean and the logical operators. You will actually see these quite a bit when you start to write out your Arduino sketches. There are a few ways that you are able to do this, but let's look at a quick example. 

Let’s say that we have two pins that we are working with. Pin 1 is going to be HIGH and pin 2 is going to be HIGH. From here you will be able to create what is known as a conditional statement to make the board behave in the manner that you would like, but you would need the logical operator to help you get this finished.

To write out your conditional expression with the information that is provided, you can use the following: (pin1 == HIGH || pin2 == HIGH). Inside of this code, you can see the (||) symbols which are logical operators. This one means that if the first or the second condition is true (it doesn't matter which one or if both are true), then the whole expression will be true. If you would like to have both of your pins be at the HIGH setting before anything happens, you would need to change up the conditional statement that you are using. It would now look something like this: (pin1 == HIGH && pin2 == HIGH).

These three topics are just the beginning of what you are able to do with the code we are writing for the Arduino board, but they will help you to write a lot of the different codes that you would like for your projects. You can use the different data types that are available to create expressions, manipulate the data and do so much more. Before we get into some of the codes and projects that we will talk about in this guidebook, open up your IDE and experiment with some of the topics in this chapter so you can become familiar with how the coding works.

Назад: Chapter 2: The Functions of Arduino
Дальше: Chapter 3: Learning the Arduino Code