
Show Notes
Discussion: 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: Memory Data Types Declaring a Variable Naming Conventions Initializing a Variable What is a variable? Variables are a programming tool that help us store and recall information in our programs. Memory 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
More from Learn Programming and Electronics with Arduino
Have you seen some really cool stuff being made with a thing called Arduino? What is Arduino anyway? It sounds like an Italian sandwich with extra cheese or something... Well - it's that and a lot more. I hope this video can help explain some the basic premise of the Arduino!
Have you ever wanted to change the font color in the Arduino IDE? Maybe it is hard for you to read the light grey comments in the Arduino IDE, or maybe you prefer something a little bolder. Whatever your reason, in this short video, I demonstrate a relatively easy way to change the font color of comments.
In many cases while using an Arduino, you will want to see the data being generated by the Arduino. One common method of doing this is using the Serial.print() function from the Serial library to display information to your computer's monitor.
In many cases while using an Arduino, you will want to see the data being generated by the Arduino. One common method of doing this is using the Serial.print() function from the Serial library to display information to your computer's monitor. In this week's episode, we will talk about the intricacies of the Serial.print() function. This is the first part, of a two part series on the Serial.print() function. Here are the specific topics we will cover in this lesson: Why would you want to use the Serial.print() function? A brief overview of the Serial library The basic use of the Serial.print() function