Lesson 8 | Using Variables | Arduino Crash Course

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello I hope you're doing great in this lesson we're going to talk about one of the most fundamental of all programming concepts variables now this is a really exciting topic because once you have a handle on how variables work you basically have one of the big keys to start understanding how programming in general works specifically in this lesson we'll be talking about memory and variables declaring a variable some naming conventions for variables and initializing a variable so what is a variable so variables are a programming tool that help us store and recall information in our programs so a microcontroller like the one the Arduino uses and computers in general they have a thing called memory and memory well it's really handy memory allows us to store information for use at a later time so let's say I'm working on a project and this project is gonna monitor temperature during the day and I have a temperature sensor it reads the current temperature every 60 minutes one in the morning until midnight and I want the program to record the highest temperature and then display it at the end of the day on like an LCD screen so in order for the program to accomplish this it needs to store two key pieces information it needs to store the value of the current temperature and it also needs to store the value of the highest temperature that it's encountered thus far but how do we actually save this information in a sketch and then 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 so when you need that something again you just go back to the locker and you grab it but how do you remember what locker you put it in so in order for us to do that all we're gonna do is give the locker a name in the name of the locker the place where you actually put stuff to store it is called a variable now technically speaking a variable is the named address of a specific location in memory but I don't want to get caught up in all the technical stuff I want to dive in to the practical use of variables so let's go back to this temperature project and we'll try to make this clear so again what we want to do is record the highest temperature during a 24-hour period so for our program I'm gonna need to store those two key pieces of information the current temperature and the highest temperature so I'm gonna have to name two lockers one locker I'll call current temperature and another locker I'll name it highest temperature so let's say my Arduino the first reading of the day maybe the temperature is like 50 degrees so I'm going to take the number 50 I'm going to open up the current temperature locker and I'm going to put in the number 50 now at the same time I'm also gonna want to open up the highest temperature locker now right now there's nothing in it so 50 is larger than nothing so I'll go ahead and put the number 50 in the highest temperature locker also so now both of our lockers have the number 50 in them now 60 minutes later I read the temperature again and let's say it's raised to degrees and now it's 52 degrees Fahrenheit outside so I open up my current temperature locker and I put in 52 so 52 over writes the number 50 and so now the current temperature locker has the number 52 in it I also peak inside the highest temperature locker and I see that 52 is hotter than 50 I go ahead and replace that also and I'm just going to repeat that process every hour so I open up the locker the current temperature locker I place the old value with the new value and then I check to see if I need to replace the temperature in the highest Templar so this reveals the first really important thing about variables and the most powerful thing about variables and that's that the contents of a variable change now the name of the variable that stays the same because the variable is the container for the information we don't want to confuse it with the actual information itself the variables the locker it's not the actual stuff that's in the locker so let's recap what we just learned we need to store information and we use memory to store it we can think of a 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 variable once we have it named we can put stuff in it and we can refer back to that stuff at a later time whenever we need it again and again the name refers to the location of the locker not the actual content of the locker now there is another thing we have to do when we make a variable we also have to say what type of thing we are going to put in it so good analogy here is to imagine that you have to build a zoo now what you're going to have to do is figure out where each animal is going to go on your zoo and you have to make sure that each animal is given enough space so it can do its thing for example you're going to need a bigger cage for a tiger then you will for like an African frog display and if you're gonna have an aquatic display you're gonna need a bigger tank for a shark than you would for a goldfish so where am I going with the zoo analogy well 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's just not going to work now variables are kind of like this because a variable has to have a datatype specified for the variable so if I have a variable and I say this variable can only hold whole numbers and all day long I can put whole numbers in that variable but if I try to put a number like a fraction into that variable I'm gonna get an error and that's because I specified the datatype for that variable to be holding whole numbers I can't put a fraction into a variable that I've specified to be a whole number variable now creating a variable is called declaring it so to declare a variable you need two things a name and a data type the data type comes first followed by its name so one example of a common data type is an integer an integer is a data type that can store a whole number from negative 32,768 to 30 2766 so whole number is like 1 or 5 or 18 it doesn't have a decimal point after it like one point zero five six seven now to specify a variable is an integer integer datatype we use the abbreviation int now the arduino ide knows all of the datatypes and it conveniently changes the color of the text when you type int following the datatype we need to type the name so we have to have a space between the datatype and the name and we can pretty much name a variable or whatever we want but there are some basic rules so a variable name cannot have any spaces in it it can't have any special characters like pound sign dollar sign and % and you can use numbers in a variable name but you can't start the variable name with a number also you can't use a keyword as the name of a variable now keywords are special words that you're going to learn about later in the course but there was it the Arduino IDE restricts for certain uses and just like data types the Arduino IDE will change the color of the text when you type a keyword you don't have to know all the keywords because if you type it it'll change color and you'll realize hey I can't use that for a variable name but what are we supposed to actually name variables the truth is you can name them whatever you want but there are some good conventions to use when naming nothing's really agreed upon but the bottom line is your variable names should be descriptive of what they're gonna hold for example if we wanted to save the current temperature in a variable called mom we could but it really just wouldn't make that much sense it'd be more logical to name that variable current temperature that way if somebody else reads our sketch they might have a basic idea of what the value is that actually goes into that variable now notice also how I write the word current temperature I capitalize the T and temperature the second word and this is called camel case 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 now how you write the name of the variable it's all up to you but it's called the naming convention and it's meant to keep things standardized so that you can read the code that you've written and other people read the code that you've written so throughout the course you'll see me use a mix of camel case and underscoring depending on the context of the variable I create but again you're free to use whatever name you want as long as you follow those rules that we talked about before so again when we are declaring a variable we need the datatype followed by the name and then at the end of that statement we want a semicolon so once we've declared a variable we're gonna want to put something inside it to put a value inside a variable we use what is called the assignment operator which is just an equal sign now the first time we assign a value to a variable it's called initializing the variable now we can declare and initialize a variable on the same line of code for example here we've got int that's a data type current temperature that's the name of the variable we've got the assignment operator which is the equal sign and then we have the value that we are initializing that variable to which is the number 40 and then again we finish that statement with a semicolon later in the program if we want to change that value all we have to use is the assignment operator again so we would name the variable current temperature and then we would assign it using the assignment operator the equals sign to a new value and then we end it with a semicolon all right so let's recap what we've talked about so far I know it it's all kind of abstract but it will become more concrete as we move forward in the course so 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 a name we also need to give it a data type the data type determines what we can put in a variable when we want to assign a value to a variable we use the assignment operator the first time we put a value in a variable it's called initializing that variable throughout the program if we want to change the value of a variable we just have to name the variable use the assignment operator which is just an equal sign and then set it equal to that new value all right that's it for this lesson we're gonna dive more into data types and all stuff like that in the next videos alright see you then bye you
Info
Channel: Programming Electronics Academy
Views: 17,247
Rating: 4.9003558 out of 5
Keywords: Arduino, Arduino(Brand), Arduino Tutorial, Arduino Lesson, Open Source Hardware Group, Learning Arduino, Microcontrollers, Electronics, Arduino IDE, Arduino Sketch, Computer programming, C++
Id: 0yRzhojCjHM
Channel Id: undefined
Length: 11min 32sec (692 seconds)
Published: Fri Jan 29 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.