Variables
A variable is a label that we give to a piece of information. This provides us with a simple way to save the information, change it, and access it. We need to tell the Arduino that we want to create a spot in its memory to store the information by declaring a variable. We can declare a variable in different parts of the sketch depending on where we want to use it. We also have to tell the Arduino what type of variable we want so it can reserve enough space and interpret it.
Computers see the world as a bunch of 0’s and 1’s. These are called bits. The more bits we use for a variable, the greater the range of values it can take on. The range of values we can store is 2 to the power of the number of bits used (2# of bits) For example, with 8 bits we can store up to 256 different values (e.g, 0, 1, 2, 3, … , 255). With 16 bits, we can store 65536 different values. In many cases we want to be able to store both positive and negative values, so we may use 16 bits to store values between -32,768 and 32,767. Different types of variables in Arduino use either 8, 16, or 32 bits. We only have a limited amount of memory for all of these bits, so we want to use the smallest number to get the job done. Some of the most common variable types are:
· byte: an 8-bit variable representing a number between 0 and 255.
· char: also 8-bits but Arduino interprets as a character like ‘a’ or ‘!’
· boolean: an 8-bit variable that can only hold the values true or false.
· int: a 16-bit integer. Integers are numbers without decimal places. An int can hold positive or negative values so the range is -32768 to 32767.
· long: a 32-bit integer. The extra bits allow us to store values between -2,147,483,648 to 2,147,483,647.
· float or double: these are 32-bit variables with decimal places with values like 3.14159. Some of the bits are used to tell where the decimal place goes. This leaves 6 to 7 digits of precision. Many programmers avoid using float variables since they require more complicated math.
To declare a variable, you tell Arduino what type of variable it is and its name. You can also give it an initial value (which can be changed later if you want). Some examples:
int start; //we’ll assign a value to this variable later
int count = 10;
long pastime = 2350000;
char firstLetter = ‘a’;
A few more notes on variables. In some cases you may need to store values outside the normal range of the long type. We don’t use them in our projects, but you have the option of using an unsigned long to store very large values. An unsigned long cannot hold negative numbers (unsigned means that the +/- sign isn’t used) but the range is from 0 to 4,294,967,295.
Also, variables are stored in volative memory which means that their values are lost when the Arno loses power. The Arno contains a small amount of non-volatile memory that retains its value even when the Arno loses power. We show you how to move values to non-volatile EEPROM in one of our projects.
Computers see the world as a bunch of 0’s and 1’s. These are called bits. The more bits we use for a variable, the greater the range of values it can take on. The range of values we can store is 2 to the power of the number of bits used (2# of bits) For example, with 8 bits we can store up to 256 different values (e.g, 0, 1, 2, 3, … , 255). With 16 bits, we can store 65536 different values. In many cases we want to be able to store both positive and negative values, so we may use 16 bits to store values between -32,768 and 32,767. Different types of variables in Arduino use either 8, 16, or 32 bits. We only have a limited amount of memory for all of these bits, so we want to use the smallest number to get the job done. Some of the most common variable types are:
· byte: an 8-bit variable representing a number between 0 and 255.
· char: also 8-bits but Arduino interprets as a character like ‘a’ or ‘!’
· boolean: an 8-bit variable that can only hold the values true or false.
· int: a 16-bit integer. Integers are numbers without decimal places. An int can hold positive or negative values so the range is -32768 to 32767.
· long: a 32-bit integer. The extra bits allow us to store values between -2,147,483,648 to 2,147,483,647.
· float or double: these are 32-bit variables with decimal places with values like 3.14159. Some of the bits are used to tell where the decimal place goes. This leaves 6 to 7 digits of precision. Many programmers avoid using float variables since they require more complicated math.
To declare a variable, you tell Arduino what type of variable it is and its name. You can also give it an initial value (which can be changed later if you want). Some examples:
int start; //we’ll assign a value to this variable later
int count = 10;
long pastime = 2350000;
char firstLetter = ‘a’;
A few more notes on variables. In some cases you may need to store values outside the normal range of the long type. We don’t use them in our projects, but you have the option of using an unsigned long to store very large values. An unsigned long cannot hold negative numbers (unsigned means that the +/- sign isn’t used) but the range is from 0 to 4,294,967,295.
Also, variables are stored in volative memory which means that their values are lost when the Arno loses power. The Arno contains a small amount of non-volatile memory that retains its value even when the Arno loses power. We show you how to move values to non-volatile EEPROM in one of our projects.
arrays
Each of the variable types can also be declared as arrays, which are groups of values of the same type. For example we can declare an int variable with one value:
int myValue = 1;
Or an array with multiple values:
int myValue[] = {5,3,2,7,8,10,155};
Here we declared an array with 7 values. Arduino creates 7 places in memory for these values. We can also tell Arduino to create the spots in memory and put the values in later:
int myValue[7];
To assign a value to the first spot we use a command like this:
myValue[0] = 155;
The number in the [] brackets is called the index. This is the spot we want to change or look at in the array. The first spot always has an index value of 0 and the last spot has an index value of 1 less than the length of the array. For example, an array of 7 values has index values between 0 and 6.
Arrays of char variables work a little differently:
char myMessage[] = “Hello World”;
This creates an array with 12 places. This might look wrong since there are only 11 characters in “Hello World”. Arduino creates an extra spot for a special character (called the null termination) that keeps track of where the array ends. This is helpful when we do things like sending the array to a computer screen.
Another special type of variable is a String. Like a char array, a String holds multiple characters (computer programmers often call groups of characters strings). A String variable has some special properties. In fact, a String is not really a variable at all, it’s an object. Here’s how we declare a String:
String myMessage = “Hello World”;
Notice that we didn’t use the [] brackets. One special property is that we can change the length of a String after we declare it:
String myMessage = “Part 1”;
myMessage = myMessage + “ and “ + “Part 2”;
Now myMessage has the value “Part 1 and Part 2”. We can check the length of the String object with:
int howLong = myMessage.length();
This command looks weird because we are giving the name of the variable, followed by a period and then asking for the length. This is an example of a method for an object of the String class. You don’t need to worry about this now, but you might see this type of syntax in other pieces of Arduino sketches. There are a lot of other useful methods for String objects. You can look at the Arduino reference on the web to see all the others. We also demonstrate String methods in Project 2.05.
int myValue = 1;
Or an array with multiple values:
int myValue[] = {5,3,2,7,8,10,155};
Here we declared an array with 7 values. Arduino creates 7 places in memory for these values. We can also tell Arduino to create the spots in memory and put the values in later:
int myValue[7];
To assign a value to the first spot we use a command like this:
myValue[0] = 155;
The number in the [] brackets is called the index. This is the spot we want to change or look at in the array. The first spot always has an index value of 0 and the last spot has an index value of 1 less than the length of the array. For example, an array of 7 values has index values between 0 and 6.
Arrays of char variables work a little differently:
char myMessage[] = “Hello World”;
This creates an array with 12 places. This might look wrong since there are only 11 characters in “Hello World”. Arduino creates an extra spot for a special character (called the null termination) that keeps track of where the array ends. This is helpful when we do things like sending the array to a computer screen.
Another special type of variable is a String. Like a char array, a String holds multiple characters (computer programmers often call groups of characters strings). A String variable has some special properties. In fact, a String is not really a variable at all, it’s an object. Here’s how we declare a String:
String myMessage = “Hello World”;
Notice that we didn’t use the [] brackets. One special property is that we can change the length of a String after we declare it:
String myMessage = “Part 1”;
myMessage = myMessage + “ and “ + “Part 2”;
Now myMessage has the value “Part 1 and Part 2”. We can check the length of the String object with:
int howLong = myMessage.length();
This command looks weird because we are giving the name of the variable, followed by a period and then asking for the length. This is an example of a method for an object of the String class. You don’t need to worry about this now, but you might see this type of syntax in other pieces of Arduino sketches. There are a lot of other useful methods for String objects. You can look at the Arduino reference on the web to see all the others. We also demonstrate String methods in Project 2.05.