Mar 26, 2017
This lesson covers one of the most fundamental of all programming concepts: Variables.
This is a really exciting topic because once you have a handle on how variables work, you have one of the biggest keys to understanding how programming works.
Specifically, we'll be discussing:
What is a variable? Variables are a programming tool that help us store and recall information in our programs.
A microcontroller, like the one the Arduino uses, as well as computers in general, have something called memory. Memory is really handy because it allows us to store information for use at a later time.
Let's say I'm working on a project that will monitor temperature during the day. I have a temperature sensor that reads the current temperature every 60 minutes from 1 am until midnight. I want the program to record the highest temperature and then display it at the end of the day on an LCD screen.
In order for the program to accomplish this, it needs to store two key pieces of information. It needs to store the value of the current temperature, and then it also needs to store the value of the highest temperature that it has encountered thus far.
But how do we actually save this information in a sketch? Furthermore, how do we recall it when we need to remember it? In order to do this, we need to use the memory.
For now, think of memory as a big wall of lockers, and you can use the lockers to store something. When you need that something again, you just go back to locker and grab it.
But how do you remember what locker you put it in? In order for us to do that, we first have to give the locker a name. That name you gave to the “locker”, the place where you’re storing your stuff, is called a variable.
Now technically speaking, a variable is the named address of a specific location of memory. However, I don't want to get caught up in all of the technical stuff. I want to dive into the practical use of variables. Therefore, let's go back to this temperature project.
As previously stated, we want to record the highest temperature during a 24 hour period. For our program, I need to store those two key pieces of information: the current temperature and the highest temperature.
That means I’ll have to name two lockers. I’ll call one locker "Current Temperature", and I'll name another locker "Highest Temperature".
Let's say my Arduino begins taking the first reading of the day. Maybe the temperature is 50 degrees. So I'll open the “Current Temperature” locker, and I'll put in the number 50.
At the same time, I'll also open up the “Highest Temperature” locker. Right now there's nothing in it, but 50 is larger than nothing. Therefore, I'll put the number 50 in the “Highest Temperature” locker, as well. So now both of our lockers have the number 50 in them.
After 60 minutes pass, I read the temperature again. Let's say it has raised two degrees to read 52 degrees Fahrenheit outside.
I open up my “Current Temperature” locker, and I put in 52. This means that 52 overwrites the number 50 so that now the “Current Temperature” locker has the number 52 in it.
I also peek inside the “Highest Temperature” locker. I see that 52 is hotter than 50. I go ahead and replace that also. I'm just going to repeat that process every hour.
I open up the “Current Temperature” locker, replace the old value with the new value, and then check to see if I need to replace the temperature in the “Highest Temperature” locker.
This reveals the first really important thing about variables and the most powerful thing about variables. The contents of a variable change. The name of the variable stays the same because the variable is just the container for the information.
We don't want to confuse it with the actual information itself. The variable is the locker. It's not the actual stuff inside the locker.
Let's recap what we’ve learned thus far. We need to store information, and we use memory to store it. We can think of memory like a wall of lockers. To use one of these lockers, we have to name it, and the name of the locker is called a variable.
Once we have it named, we can put stuff in it and refer back to that stuff at a later time whenever we need it again. Again, the name refers to the location of the locker, not the actual content of the locker.
There is another thing we have to do when we make a variable. We also have to say what type of thing we're going to put in it. This is called the Data Type.
A good analogy here is to imagine that you have to build a zoo. You have to figure out where each animal is going to go in your zoo, and you have to make sure that each animal is given enough space so it can do its thing.
For example, you'll need a bigger cage for a tiger than you will for an African frog display. If you're going to have an aquatic display, you'll need a bigger tank for a shark than you will for a goldfish.
Where am I going with this zoo analogy? Here's the deal. You can't take a monkey and put him in a cage designed for fish. Likewise, you can't take a fish and put it in a cage designed for a tiger. It just won’t work.
Variables are similar to this because you have to specify a data type for the variable. Maybe an example will clarify what I mean.
Let's assume I have a variable, and I say, "This variable can only hold whole numbers." I can put whole numbers in that variable all day long. However, if I try to put a number like a fraction into that variable, I'm going to get an error.
The reason is that I specified the data type for that variable to be whole numbers. I can't put a fraction into a variable that I've designated as a whole number variable.
Creating a variable is called "declaring" it. To declare a variable, you need two things: a name and a data type. The data type comes first, followed by its name.
One example of a common data type is an integer. An integer is a data type that can store a whole number from -32,768 to 32,766.
Of course, you don’t need to memorize that. Just understand that a whole number is like 1, -5, or 18. It doesn't have a decimal point after it, such as the number 1.0567.
In order to specify a variable as an integer data type, we use the abbreviation "int". The Arduino IDE recognizes all possible data types. Therefore, when you type “int”, it will conveniently change the color of the text for you automatically.
Following the data type, we need to key in the name. There has to be a space between the data type and the name.
You can name a variable almost anything you want, but there are some basic rules to follow. A variable name cannot contain any spaces or special characters, such as a pound sign, dollar sign, or percent.
You may use numbers in a variable name, as long as the name doesn’t start with a number. Also, you can't use a keyword as the name of a variable.
Keywords are special words that you will learn about later in the course. They're words that the Arduino IDE restricts for certain uses. Just like data types, the Arduino IDE will change the color of the text when you type a keyword.
Thankfully, this means you don't have to know all the keywords right now. If you happen to type one, it’ll change color and will help you realize that you can't use that as a variable name.
Using these rules, how should we name variables? Even though you can name them whatever you want, there are still some naming conventions that are best to follow.
The bottom line is that your variable names should be descriptive of what information they will hold.
For example, if we wanted to save the current temperature in a variable called "mom", we could. However, it really wouldn't make that much sense.
It'd be more logical to name that variable "currentTemperature". That way, if somebody else reads our sketch, they will have a basic idea of what information is actually in that variable.
Notice how I wrote the word "currentTemperature". I capitalized the T in the second word temperature. This is called camelCase. It's simply a way to help distinguish the words in a variable name.
You could also type "current_temperature" with an underscore between the two words. How you write the name of the variable is up to you.
The important concept here is that this is called the "naming convention". It is a way to keep things standardized to make the code easier to read and easier to understand.
Throughout the course you'll see me use a mix of camelCase and underscoring, depending on the context of the variable I create. Again, you're free to use whatever name you want, as long as you follow the rules we discussed.
As mentioned before, when we are declaring a variable, we need the data type followed by the name. Then, at the end of that statement, we want a semicolon.
Once we've declared a variable, it can now hold information. To put a value inside a variable, we use what is called the "assignment operator", which is simply just an equals sign.
The first (or initial) time we assign a value to a variable, it's called "initializing the variable". We can actually declare and initialize a variable on the same line of code. Here’s an example:
We have "int" for the data type and "currentTemperature"as the name of the variable. Next, there is the assignment operator, the equals sign.
After that is the value to which we are initializing that variable. In this case, the initial value we are putting in the variable is 40. Lastly, we finish the statement with the all-important semicolon.
If we want to change that value at some point later in the program, we just use the assignment operator again.
In other words, we would type the variable name "currentTemperature", use the equals sign as our assignment operator, type the new value, and then end it with a semicolon.
Let's recap this lesson. A variable is simply the name of a memory location. It's how we're able to store and recall information in our sketch.
In order to use a variable, we have to give it both a data type and a name. The data type determines what we can put in a variable. Although you can use almost anything for a variable name, we did go over a few helpful rules and naming conventions.
When we want to assign a value to a variable, we use an equals sign called the assignment operator. The first time we do this, it's called initializing that variable.
Throughout the program, if we want to change the value of a variable, we simply type its name, assignment operator, and new value.
That concludes this lesson. I know it's all kind of abstract right now, but these concepts will become more concrete as we move forward in the course. I look forward to next time where we'll dive more into data types and the like.