Structure -- Variables
Here, you will be learning about the structure of Arduino code. Actually, this applies to most other codes as well. First, we look at variables...
To declare a variable, you do something like this...Say I want to declare x as a variable.
int x;
In this simple line, I declared x as a variable. Notice the int. You may be wondering what that is. int stands for an integer. So if we look at that line again, you know know that we are declaring x as an integer. Here's what declaring variables should look like...
(note: Usually, declaring variables are in the setup() of the program, or even before...
(variable type) (variable name);
There are many variable types. Here's a list:
*int - Integer value
*boolean - True/False value
char - String values
byte - 8-bit unsigned number, 0-255
long - Values for long numbers, from -2,147,483,648 to 2,147,483,647
float- Decimal values, such as 3.14159265358979323846...correct me if I'm wrong...
Of course, there are a lot more, but so you won't give you a headache, here are some of the basic ones. The variables with asterisks are most commonly used, especially for counters, decisions, etc. For arduino, float values are best for reading sensor values.
Now that we know how to declare variables, let's learn how to give these variables values. Say I declared x as an integer variable, and I want to assign the value of 5 to x. Here's what I'll do...
x=5;
Simple, isn't it? Same goes for boolean values, except that you assign true or false to the variable. Here's an example of assigning a boolean variable.
n=true;
This also goes for the other variable types as well. Now, let's look at a syntax error.
int x;
x=5;
x=true;
Notice how I assigned x to another value. Once you assign another value to x, the old value is erased and is replaced by the new value. However, something is still wrong here? It's this: you can't convert variable types. This code here is trying to turn an integer value into a boolean value. Simply can't be done. It's like converting from Buddhism into Communism! So no, this would not be good. However, this will...
int x;
x=5;
x=10;
Same as the piece above, except that I'm changing x from one integer value to another integer value. If you don't get it, think it this way...
In ancient India, you are born into doing the job your parents are doing for the rest of your life. For example, Bob is born into law. Bob will remain working in law for the rest of his life, whether he likes it or not. Same as variables, you can't change it's type.
Finally, something about number variables. Say I had an integer variable, x, given the value of 7. I want to add 3 to it to make 10. How do I do that? I'll show you!
int x;
x=7;
x=x+3;
I had just used arithmetic operators to give x a different value. Before moving on, take a look at line 3 of the code above.
x=x+3;
Seems odd, eh? Well, it isn't really. If you look closely, this translates as...
x=7+3;
Where did the 7 come from? That 7 came from the previous x value, which was 7. The x just stands for the new value for x.
Let's look at the arithmetic operators. I'm going to assume you know what these mean.
= assignment operator
- subtraction
+ addition
* multiplication
/ division
% modulo
Never saw a modulo? I'll explain. modulos are represented by a %. The modulo calculates the remainder of the quotient and the divisor. Here's the syntax:
(quotient)%(divisor)
Here are some examples:
5%5 = 0
5%6 = 6
7%6 = 1
To declare a variable, you do something like this...Say I want to declare x as a variable.
int x;
In this simple line, I declared x as a variable. Notice the int. You may be wondering what that is. int stands for an integer. So if we look at that line again, you know know that we are declaring x as an integer. Here's what declaring variables should look like...
(note: Usually, declaring variables are in the setup() of the program, or even before...
(variable type) (variable name);
There are many variable types. Here's a list:
*int - Integer value
*boolean - True/False value
char - String values
byte - 8-bit unsigned number, 0-255
long - Values for long numbers, from -2,147,483,648 to 2,147,483,647
float- Decimal values, such as 3.14159265358979323846...correct me if I'm wrong...
Of course, there are a lot more, but so you won't give you a headache, here are some of the basic ones. The variables with asterisks are most commonly used, especially for counters, decisions, etc. For arduino, float values are best for reading sensor values.
Now that we know how to declare variables, let's learn how to give these variables values. Say I declared x as an integer variable, and I want to assign the value of 5 to x. Here's what I'll do...
x=5;
Simple, isn't it? Same goes for boolean values, except that you assign true or false to the variable. Here's an example of assigning a boolean variable.
n=true;
This also goes for the other variable types as well. Now, let's look at a syntax error.
int x;
x=5;
x=true;
Notice how I assigned x to another value. Once you assign another value to x, the old value is erased and is replaced by the new value. However, something is still wrong here? It's this: you can't convert variable types. This code here is trying to turn an integer value into a boolean value. Simply can't be done. It's like converting from Buddhism into Communism! So no, this would not be good. However, this will...
int x;
x=5;
x=10;
Same as the piece above, except that I'm changing x from one integer value to another integer value. If you don't get it, think it this way...
In ancient India, you are born into doing the job your parents are doing for the rest of your life. For example, Bob is born into law. Bob will remain working in law for the rest of his life, whether he likes it or not. Same as variables, you can't change it's type.
Finally, something about number variables. Say I had an integer variable, x, given the value of 7. I want to add 3 to it to make 10. How do I do that? I'll show you!
int x;
x=7;
x=x+3;
I had just used arithmetic operators to give x a different value. Before moving on, take a look at line 3 of the code above.
x=x+3;
Seems odd, eh? Well, it isn't really. If you look closely, this translates as...
x=7+3;
Where did the 7 come from? That 7 came from the previous x value, which was 7. The x just stands for the new value for x.
Let's look at the arithmetic operators. I'm going to assume you know what these mean.
= assignment operator
- subtraction
+ addition
* multiplication
/ division
% modulo
Never saw a modulo? I'll explain. modulos are represented by a %. The modulo calculates the remainder of the quotient and the divisor. Here's the syntax:
(quotient)%(divisor)
Here are some examples:
5%5 = 0
5%6 = 6
7%6 = 1