Arduino Tutorial 4: Understanding Arduino Variables

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is Paul McWhorter from top tech boy comm and we are here today with our new our improved our exciting Arduino tutorials and we are on tutorial number 4 what we're going to do today is we are gonna introduce the concept of using variables in our Arduino programming so I will need you to pour yourself a most excellent mug of iced coffee I will need you to get out you're a Lego super starter kit which if you don't have yet you can click on the link below in the description order your kit it's $35 we're gonna be doing this whole series on this kit so it will take you a long long long way into doing a whole whole whole lot of product projects that we're gonna be doing in these tutorials so order your kit get your coffee and then let's just jump right in okay so the concept that we're going to do today is we're going to learn about variables but to learn about variables we're gonna start by doing a project the old way so I need you to go back and get your project from lesson number three which was to build a blinking LED and then let's see if I can get this word you can see it this was my circuit that we built let's get the circuit diagram in the picture here I will back off you can see I do not have a producer Here I am doing the whole thing okay so this is the circuit that we're trying to build we were operating on pin 13 to provide the voltage we're going through a resistor of 330 ohms to the long leg of the LED the short leg of the LED connects to ground okay so that's what we hooked up here on our breadboard so I've got the red wire plugged into pin 13 the red wire comes over books to one leg the first leg of the resistor and then the second leg of the resistor connects to the long leg of the LED and in the short leg of the LED comes back to the GND pin the ground pin on the Arduino and the GND pin is right beside that 10:13 ok so what would be a neat little experiment for us to do with this circuit what I want you to do is I want you to create a little simple Morse code program to blink out on the LED SOS okay if you're not familiar with Morse code it's a series of dots and dashes that allow you to communicate a very low bandwidth way to communicate which used back before modern telecommunication and so if a ship was sinking the ship would send out an SOS and it sort of would say Stanford save our ships you need to know how to make the letter S and Morse code and you need to learn how to do the letter O in Morse code so the letter s if we come over here is a dot dot dot okay so if you were listening to it and go dit-dit-dit and then dash dash dash is an O and then dit-dit-dit so it goes dit dit dit Dada Dada dit dit dit so this is the letter S this is the letter O and then this is the letter s SOS so you want three quick blinks blink blink blink and then blink blink blink and then blink blink blink okay so we have the circuit set up so we should just need to do a little programming and then we should have this LED blink did it did it did it did you could do this on your own then you could follow along with me so you might run ahead pause the video and do this on your own and then come back and you can watch me do it okay here I go if I am going to turn that led on there's something I need to do one time and I need to do that in the void setup that is pinmode right anytime you use a pin you got to do a pin mode so I type in mode and then order the parameters the parameter is which pin am i working with pin 13 second parameter is is it an input or is it an output it is an output okay and then almost every command in ordered we know ends with a semicolon oh look this didn't turn the happy little orange color when I did pin mode what did I do wrong somebody tell me what did I do wrong I heard you I didn't use capitalization right the M should be capitalized pin mode 13 as an output okay now in the loop we want to do did it it did it did it and I'm gonna do some copying and pasting to try to make this go faster but I'm gonna do a digit tol right what pin pin 13 and then I'm gonna go high okay and with a semicolon now what delay because I want to leave that first dit on so I'm gonna delay what do you think 50 milliseconds okay so the light will stay on 50 milliseconds okay and then I want to do a digital right pin 13 what do I want to do now I want to turn it off so I'll say low end in a semicolon and then what deal a 50 okay so this turns it on wait 50 milliseconds off wait 50 milliseconds so that's the first blink that's the deer the first did well how many deals do I want three so I'm going to copy and paste so I'm going to come in here and take this I'm gonna copy it and then I'm gonna leave a blank line in my code just so it's easier to keep organized and paste okay so now I got D and then I got D and then if I look over here how many'd is did I want how many little dots one three so I need one more so I will do ctrl V again okay now that's the did did did now I need the da da da I need the three dashes so the easiest thing would be to just copy all of this because I have the three dis so I'm gonna copy all three of those little blinks and I'm gonna come down I'm gonna paste them but these I don't want did did they do these need to be longer so this is the first is second third did now this one I want longer so I'm gonna put it 500 500 and then I'm gonna put 500 ah sorry 500 500 500 okay so here I've got the d D D and then I got the da da da almost I need one more there okay now what do I need I need three more dis digit it da da da did it so I've got the digit da da da I need those three dots again so I can just this time just cut and paste the first three blinks 1 2 3 ctrl copy come down here and then ctrl paste and so this should do it right that was pretty quick wasn't it with my quick copying and pasting hopefully you were able to keep up so let's download this into the program we got the happy little green bar that's good and now let's look at it this isn't very good because it's running the digit it's running this all the way into this so after we do this one time we probably need to put a one-second delay in so these three don't run into that okay so we need to put a long delay at the end of like a second so we're gonna come back over here to the code and we're gonna say delay and then a thousand in that way I think we can see it better does that make sense all right learning as we go here so we will download it happy little green bar okay did it did did did it it did it it did it did it okay do you kind of see that oh darn the problem with that is that the short ones are too short the short ones are really too short and it's kind of hard to read do you see how it's not really working so we really Oh done the other thing I really wanted to use pin eight and then I need the little dot shorter okay what's the problem now that you see I'm gonna have to go in now I'm gonna make that pin 8 pin 8 and 8 10 8 pin 8 10 8 today I'm gonna have to change all those to 8 because I told you wrong I really wanted to do pen 8 oh and then this 50 is too short and so I really need to make that you know I really need to make that longer and so we got to go and we got to do a whole bunch of changing and guess what after you change it I'm gonna change my mind again and I'm gonna want to use pin 4 all right so do you see how hard it is to do things this way where you're just hardwiring in numbers here and this is gets to the concept of variables when you're writing a program you never want to use constants like this you want to set everything up with variables where do you set variables up you set them above the Boyd Boyd set up before the void set up we're going to show the variables that we want to use so you've got to tell it what type of variable it is you got to tell it what type of number it is and really there's kind of like - for the sake of Arduino there's sort of like two different types of numbers that we can think about one would be our counting numbers like 1 2 3 4 5 our round numbers we could have - 5 - 4 - 3 - 2 - 1 0 1 2 3 4 5 those are integers right if you've been in math class you know that those are integers and so one type of number we can have it's integer and then you can have another type of number which is 4 all the in-between numbers so like like if that would be numbers like 3.14 right that would be a floating number floating-point number meaning the in between numbers int or the round numbers floating point numbers or the in between numbers and so use an int anytime you can because it uses less variables so what would be one thing that we would want to set up at the top and then just use the variable rather than continuing to edit the specific numbers in the code how about what pin were connected to so I need to tell it that I'm going to set up a variable and it's gonna be what type of variable and integer and so therefore we type in int look it turned the happy little blue color it represent so int is telling it I'm going to create an integer what am I gonna call the pin well I'm gonna call it my pin or how about LED pin the LED or how about red pin you see any of these things would be good red LED I think I'm gonna call it red LED it doesn't matter what you call it but just always call it the same thing and then I'm going to give it a value it is going to be equal to right I didn't want pin 13 I want to set it to eight and then we end with a semicolon if we were gonna do PI like pi 3.14 we're not going to do it in this program but I'll just do it as an example 3.14 would that be an int no no no it would not be an int it would be a float and pi would be equal to 3.14 we can leave it in there even though we're not using it just to kind of show you the difference between a float what else oh my goodness all these numbers we might want to change it three or four times to get it working right right okay so what we want is we want that short number that's going to be an int because it's just gonna be the number of milliseconds and I'm gonna call it dit and that's the short number what should that be well instead of 50 let's make it a hundred okay and then I need the int for the da which is the longer number and we're gonna make that 500 okay so you see I've set up three variables that I'm going to use instead of going down and saying 8 8 8 8 8 8 for the pin number I am just going to call it red LED in anywhere I put in red LED Arduino will put in the number 8 then the advantages if you want to change the pin later you just change it one time up here so let's go in and put our red LED in and instead of topping it over and over I'm just gonna copy it and then let's look at where we need to put it everywhere there was a pin number 13 instead of 13 I'm gonna put red LED what is it gonna put in for red LED it's gonna put in the number 8 and same thing here same thing here here here so yeah it's a lot of work wouldn't it have been easier if I would have used variables to begin with I think it would have been okay and then here we have smore hopefully you're following along with me okay so now I've replaced the parameter of the pin number instead of a number I'm using the variable red LED and now I can set it to anything I want okay I can set it to anything I want now instead of using the 50 we want that to be the variable dit so everywhere there's a 50 I'm gonna put I'll copy that again because it's faster so I am going to put dit-dit-dit-dit-dit-dit do I want to put it here no that's gonna be a Doss I'll fix that in a minute and then I come back to my did dit dit dit okay so I got two more it and did okay now what do I want to do I want to do my diet 500 so I copied da and I come down here and where I have 500 I put da [Music] and then dit dit dit I think that's it you see the thing is really you want to get in the habit always use a variable don't use numbers down here always use a variable so also this between the digit it did did did it it we want that one-second delay and so we will kind of like maybe you will call that int it will call it long w-4 long wait and that's equal to 1000 all right so I got all my variables up top top here and then I come down here and I put long wait long wait okay now everything's really being controlled by these variables up here all right now I've said that I want red LED pen to be pen eight so I've got to come back over to my circuit and instead of having this wire in pin 13 let's see if I can make that bigger for you to see better okay so instead of having this red wire in 10 13 I'm gonna put it in pen 8 all right now I am coming back over here and I need to show you where I am so I need to switch back over here okay now let's download this okay ooh ooh our first error look at that it says long w was not declared in this scope it doesn't know what long W is my goodness what did I do wrong long W long w okay do you see it's telling me an error down here long W was not declared in the scope it doesn't recognize long W but I declared long w up here hmm why doesn't it like it somebody tell me I declared long w up here but yet when I get down here it doesn't recognize it what matters capitalization what did we do long W long w yeah I made that a capital L instead of a little L so little long capital W little long capital W and now we will download and we get the happy little green bar and then look over here boom 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 ok do you see that did it da da da did it it okay I still think that the hundred is a little bit too fast and so let's make this I think it would be easier if we made this 250 and we made this 700 I want to slow it down so you can see it and then I'm gonna make long mm all right so now let's look at that let's download that and watch over here okay boom watch this 1 2 3 1 2 3 1 2 3 1 2 3 dit dit dit da da da dit dit dit dit dit dit da da da dit dit dit so that's pretty good now since you're not a Morse code guy slowed it down so you can actually see it but you could see that if you were a real Maersk Morse code guy you could go a lot faster so let's go like let's say we're a real expert we'll make this 50 and we'll make this a hundred and 50 and so if you were a real ham radio operator you could recognize this and let's see how this looks yeah did it it did it did it did it it da da da did it okay you see that's going a lot faster and you could see it but also this got a little long then so let's say 1000 and then let's download it okay did it it Dada Dada did it did it it da Dada did it it did it it da Dada did it it okay do you see how now it's like really and fast ok do you see the power of using variables we can come in here and play with these parameters without having to go in and change all this and the longer and the more complicated your programs get the more important it is to use variables so from now on never use a number down in your program always set variables up at the top and then use those variables and then if you need to edit something you just come to the top of the program and you change the number there does this make sense is this brilliant oh wait a minute did I say pin eight I really meant pin four oh I'm sorry you've got to go rewrite your program you've got to write rewrite the whole program for pin four uh-huh you just come up here the red LED is now hooked to pin for you see it's not working hey we will download it and then BOOM did it it dah-dah-dah did it it did it it dah dah dah did it on pin four right so you could also play around with you know any of the timing parameters you want do you see the power of using variables now we didn't use it but there is one other type of variable and that is a string and so you could come here and say string and a string would be something like ah my name is and then here in quotes Paul McCourt er okay so I can have an integer which is an int an int variable which is the round numbers positive and negative I can have a float which is the in between numbers like pi 3.14 is a string of letters and then I could call that variable my name and my name is equal to quote Paul McCourt or in quote and so that when you set a value to a string you have to put it inside of quotes does that make sense now in future lessons we're gonna always do variables we're never going to put numbers in again we're always going to use variables and in future lessons we'll playing around more with float and start playing them around more with strings but for now you sort of see what with the power of this is okay so your assignment for this lesson is to now create three different led circuits a red a green and a blue I want you to blink the red LED five times fast I want you to blink the green LED ten times slow and then I want you to blink the blue LED 15 times real slow okay and I want you to do it don't put any pen don't put any numbers down in your code do it all with variables so what variables are you gonna need well you're gonna need a red LED variable for the pin that that's connected to a green LED variable and then the pin that's connected to in a blue LED and then what can that is connected to so you'll have three variables then you will have three pin modes based on those variables and then you need kind of like a a blue blink so you'll have that you'll need a red blink you'll have that you'll need a green blink you'll have that so all of those numbers you define up top and then you use the variable names down in your program okay guys this is polemic order from top tech boy comm I will talk to you guys later
Info
Channel: Paul McWhorter
Views: 589,179
Rating: undefined out of 5
Keywords:
Id: nPOKOi1jIK0
Channel Id: undefined
Length: 25min 56sec (1556 seconds)
Published: Tue Jun 11 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.