So, by the end of this lesson, you’re going to be able to understand basically what a variable is and how to use variables and also how to spell the word variable if we’re lucky. So what is a variable and how do I make them so variables? They’Re programming tools that cut that basically allow us to carry around information from one part of a program to another part of a program, so let’s go ahead and let’s start with a mental picture. So variable it’s, like a bucket, you can put things in a bucket and you can take things out of a bucket, but the bucket doesn’t change. What changes is what’s in the bucket okay, so let’s say that again you can put things in a variable and you can take things out of a variable, but the variable stays there. What changes is the contents of the variable. So this illustrates our first point, and that is the contents of a variable. Can change okay, so now variables, truthfully speaking, are more like specialty purpose buckets so imagine a bucket with a heavy duty of heavy duty handle and it’s blood it’s been designed to carry rocks. Okay or think about a bucket that’s got a pour spout on it and it’s been designed to carry liquids. So when you make a variable, you specify what type of stuff that you’re going to put into that variable or for our mental picture. What you’re going to put into that bucket so for programming, the stuff that you specify is called the data type.
So some examples of data types are integers characters and arrays, and once you’ve specified that datatype you’ve got to keep your promise and you can only put data types in to that type of data type that you buy into that variable that you name. So this means that if you make a variable – and you say hey, this variable is only going to hold rocks, then you can put all different types of rocks in that variable. But the second you try to pour beer into the rock bucket you’re going to get an error okay. So this leads us to the second point: a variable will only hold what data type that you specify. Okay, so let’s make this a little more concrete. All right! Let’S let’s show how we’re actually going to take the type this into the air. Do we know ite, so the process of making a variable is called declaring or declaration. So when you declare a variable there’s, two things that you need to tell the program first, what type of things you plan to put in the bucket that’s called the data type and then. Secondly, what is the name of the bucket? Because you’re going to want to refer back to the variable, so you need a name for it. So to declare a variable. You write the type of contents. It will hold so again that’s the data type, followed by the name and there’s a space in between so here’s a pretend declaration using our mental picture Rock and then my bucket of rocks and then here’s an actual variable declaration using an integer, so it’s int.
My number so again, we have got the data type and we also have the name, and then it ends with a semicolon. So the first thing you notice in the above declarations is that the data type is a different color and that’s because yard we know, knows variable data types and gives them a special color to reduce confusion, and this is done automatically for you. So what you should do is you should pause right here. You should open up your Arduino IDE type in the letters I and T, and they should turn the color orange or you could try typing in the word void. Vo ID or you know, type in, manage Amma and see if it changes. Colors. Okay, so let’s go ahead. Let’S shift here a little bit and let’s talk about naming variable variables, so the second part of the deck is giving the name giving the variable a name. So you can pretty much name a variable whatever you want, but there are certain restrictions. So, for example, a variable name cannot have spaces or special characters in it. Though an underscore is permitted. Upper and lowercase letters are fine, and numbers are okay too, but you can’t just have a number for the name of your variable. It can’t just be a number and your variable must start with a letter, not a number and finally just one other point. Variable names cannot be the name of our. Do we know keyword, so you know we already kind of touched on keywords, but there’s words it.
The Arduino IDE has said hey. These are keywords and they’re restricted for keyword use, so you cannot use them as variable names. Okay, now, in addition to these restrictions, there are a couple good conventions to follow. So when I say recommended I’m actually saying man, you should do this or you’re going to hate yourself later, so the variable name should be as descriptive of X function as possible. So, for example, the led pin variable could be the pin number that you put your LED on onto your duea board, so that kind of makes it clear. I mean you could name that you could name that variable mom. But what what does that mean to you? Five days from now I mean I guess it means it’s your mom but that’s weird, and what does it mean to somebody else? They don’t know so try to make the name of the variable descriptive of its function. Okay, now you should always start your variables with lowercase letters and then, if your variable name is two words connected together, then you should capitalize the first letter of the second word and then, if you have multiple words, just keep capitalizing the first letter of the of The words as you go: okay, so we’ve talked about the names, the conventions let’s go ahead and talk about the integer datatype for a second okay, now again there’s several types of data, types that you can declare. But again I just want to talk about the integer datatype, because that’s the first that we’re going to encounter in the next tutorial.
So you probably know that an integer is a whole number and a whole number is just a number with no decimals. So, like 510 1054, no decimal points so like 1.5. That would not be a whole number. So for Arduino, an integer is a number from negative 32768 to positive 32768. So if you try to put a number bigger than that into an integer variable, the value is actually going to roll over the opposite side like a game of pac man. So if you have, if you add the number 5 to 32767, you would get negative 30. 2763, so in and then, if you subtracted 5 from negative 32768 you’d actually get positive 30, 2762. So again, the reason this is is because when we specify datatypes, basically what we’re telling the compiler is to set aside so much space for this information. For this variable and it’s only going to allow so much space and it’s only let you use so much of that space. Okay, we’re not going to get too much into the details there, but that’s kind of the. Why? Behind the how or the how behind the? Why you know what I’m talking about so again? Integer is abbreviated, int and since integer is an Arduino datatype, it will change color to orange. So here is a declaration and a variable declaration. We have int for integer a space followed by LED, which is the name of the variable we’ve got here and then it’s finished with a semicolon.
So get let’s go ahead and recap variable declaration. The first thing you do is write the datatype. This is what the type of stuff you’re going to put in the variable, and then next, you name the variable, so try to use a name that makes sense, and this is the name that you’re going to use to refer to it over and then. Finally, you end it with a semicolon all right. So now let’s move on to initializing variables, so I mean it’s great. We can make variables right wonderful, but how do we put stuff in the variable’, so the process of putting something in a variable is called initialization or initialize so to initialize a variable. All you do is set it equal to something using an equal sign. That’S pretty easy, so first remember you always have to declare a variable. So here we have int LED followed by semicolon. So we’ve made our declaration, but now we want to initialize the LED variable to the value 13. So all we do is we type the name of the variable. We get an equal sign and we say: hey you’re equal to 13 and we finish it with a semicolon. That’S that’s easy. So now, essentially what we’ve done is we’ve put 13 in the LED bucket. Okay, so the LED variable is now going to represent the number 13 for us, okay, it’s, pretty easy and anytime. We want to refer back to the number 13.
All we have to do is type LED and the program’s going to know o LED holds 13. So we must be talking about the number 13 all right so now. What we can also do is we can initialize and declare a variable right at the same time, so that’s really easy too. All you do. Is you write your declaration so int LED, but instead of a semicolon, you just go ahead and set it equal to whatever value want to put it in there in this case it’s 13, and then you finish it with the semicolon. So now the variable LED has been initialized to the value 13. So now anytime, I refer to the LED variable. I will get the value 13 so that pretty much wraps it up, so you should kind of get what a variable is like you know, what is it and you should know how to declare one, and you should know how to initialize one, and you should also Be familiar with the integer, datatype and kind of have a fuzzy idea of what datatype means, and I assure you that, as we continue all of this variable stuff like why we use them and and how are they useful? It is all going to solidify. But right now I think you’ve got enough information to move on in the next tutorial. Where we write our first program. I’Ll see you then thanks so much for joining me.
arduino define Video
arduino define news
Posted on Tuesday July 16, 2019Arduino from the Command Line: Break Free from the GUI with Git and Vim! Linux Journal … Continue Reading »
Posted on Sunday August 11, 2019How to build a motion-controlled fan Popular Science … Continue Reading »
Posted on Sunday April 14, 2019Anet A8 Firmware: Which to Choose & How to Change It All 3DP … Continue Reading »
arduino define Social
🔬 Now that you’ve gotten your feet wet, dive into the kit and enjoy all nine experiments. Order yours today: http://bit.ly/2MnQ7fr