Arduino Uno R4 WiFi LESSON 8: Using Variables in Arduino

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is Paul mcarter with toptechboy dcom and we're here today with episode number8 in our incredible new tutorial Series where you're learning how to think like an engineer using the Arduino Uno R4 WiFi what I will need you to do is pour yourself a nice tall glass of ice cold coffee that would be straight up black coffee pour it over ice no sugar no sweeteners none needed and as you're pouring your coffee as always I want to give a shout out to our friends over at sunfounder sunfounder is actually sponsoring this most excellent series of video lessons and in this class we will be using the sunfounder elite Explorer kit hopefully most of you guys already have your gear but if you don't to take a look down in the description below there is a link over to Amazon and you can hop over there and pick your kid up and believe me your life and my life are going to be a whole lot easier if we are working on identical Hardware but enough of this Shameless self-promotion let's jump in and talk about what I am going to teach you today and what I'm going to teach you is I'm going to teach you a new programming Concept in this class and that is the concept of using variables in your Arduino programs and the way I'm going to introduce it is we're going to go back and we're going to put together our little SOS our little Morse code blinking LED and then I'm going to show you how we can do it better by using variables instead of using constants sound good I hope it does so we're going to come over here I'm going to get out of your way and what you're going to need is you're going to need your red LED you're going to need your 1000 Ohm resistor and you're going to need two wires now now I never like to just jump in and start putting a circuit together we always sketch it out on paper first put it on paper first and then build it and if you do that you're going to get rid of a whole lot of problems and a whole lot of Errors now yes this is a simple circuit but what are we doing we're developing good engineering habits and what do an engineer do they always work it out on paper and then they go in and they build it or program it so if we're going to use that led what are we going to need we're going to need a voltage source okay that's our voltage source where are we going to get this positive voltage we're going to get it from pin 13 on the Arduino then what are we going to do we're going to go over and we're going to connect to our LED okay and remember this is the long leg the LEDs have to go in in the right direction ction the long leg always has to point back towards the voltage Supply then we don't want to burn out our Arduino or our LED so what do we need we need a current limiting resistor of 1,000 ohms okay now I've got it drawn on paper and now what do I need to do I need to build it on the circuit board so let's start with the led the led the long leg needs to do what it needs to point back towards the Arduino so I'll put the long leg in one column I'll skip a column and then I'll put the short leg in the other column and it's just I try not I try not to pack these things in too closely and so I try to kind of leave an empty column between the pins now I'm going to take that long leg of the LED so this red wire is being plugged into the same column as the long leg of the LED then where does that go that goes to our friend Mr pin 13 like that okay now what I'm going to do is I'm going to get the resistor and one leg of the resistor is going to connect to the short leg in the same column of the short leg of the LED then I'm going to jump over the trench okay why do I jump over the trench because I don't want this leg connected to this leg and the connections break at the trench so now I have gone to the Long Leg of the LED short leg of the LED to one leg of the resistor and then the other leg of the resistor so I'm going to put this wire in the same column as that other leg of the LED and then I bring it back home to ground G and D there are three ground pins one on this side and two on on this side they're all the same you can use whichever ground pin you want they're all connected together so I have what I have my circuit built now what we're going to want to do is we're going to want to go in and we're going to want to write that program to do the SOS again the did da da da did okay and we did this before so this should really be review for you so I'm going to come over here and we're going to be talking to pin 13 because we're going to be talking to pin 13 I need to do a what I need to do a pin mode and so where do we do our pin mode we want to do it one time so we do it in the void setup so I'm going to do pin mode capitalization is important which pin pin 13 and I'm sending a signal to it so it is an output like that okay so now that pin is ready to rock and roll right we've got it set up pin mode done boom now what do we want to do we want to come down to our void Loop and we want to Blink it da da da and so what am I going to do I'm going to do a digital right who am I going to write to pin 13 where am I going to put him I'm going to put him high now how long do I want to leave him high let's leave him high 50 milliseconds so I'll put in a delay of 50 milliseconds and then I am going to put my sem semicolon I turned it on in order to link now what do I need to do I need to turn it off digital write who am I going to write to digital I missed the a in there man digital right who am I going to write to pin 13 where am I going to put it I'm going to put it low to turn it off and now I'm going to put the semicolon now I want to leave it off for 50 milliseconds so I'll say delay and then 50 and now I have what I have one D I need how many d three so the easiest thing is for me to take these four lines of code copy them and I like to keep my code clean so I'm going to put a blank line in there and then I'm going to paste and I like to keep my indentation constant Arduino doesn't care about indentation the way python or micropython does but I still like to keep my code lined up and neat so I have what I have d d I need what another D so I go d d d all right now I need to work on my dos so I have d d d now I'm going to do my dos well the way I'm going to do my dos I'm going to just type in my I'm going to paste in my dis again only this time for the da I'm going to make it 500 and 500 now I have what I have d d d and I have one da I'm gonna copy that da and now I need how many more dos two so da da da like that and again I like to keep my code lined up so I'm going to take those extra tabs out okay what do I have I have d d d da da da what do I need I need three more dits so I think I'm just going to copy all three dits up here so you see this is the 50 millisecond so that's the dit that's dit d d I have my three dits I'll copy them and then I'll come down here and I'll paste them okay so now let's check all the code I've got dit d d da da do d d d now what I want is I want to delay before I start the d d DS again and so I'm just going to put an overall delay of let's say 1,000 milliseconds so that would be one second now the danger of copying and pasting is if we made a mistake I copied and pasted it like nine times okay so we got to hope that I didn't make a mistake in there okay so now what do we want to do we want to come up and remember we started with bare minimum and now I want to come up I want to make sure that on under the port I've selected the Port where the Arduino is so Comm 5 is right it sees the port there okay and then what I want to do I want to look under Tools I want to make sure that my board is an Arduino R4 WiFi that's the right board for me and so I think everything should be ready to go and so what we're going to do is we're going to come in and we're going to run it okay keep your eyes on the LED and it's kind of blinking around a little bit and then I've got a da da da da da did okay we got it we got it but what do we not like what do we not like well if I come in and let's download this again and as I download it I want you to look at the Led it's doing its own thing it's getting bright it's getting dim it's doing all this if you were looking for a morse code now you're you're confused because it's that's downloading pin 13 is blinking and the reason it's blinking is it sort of shows you pin 13 blinking so you can kind of see that your download is going but that means if you are using pin 13 you're going to get some confusing results out of it as the code downloads so 13 wasn't a very good choice I think we want to use pin8 now what is the problem the problem is I've got to come and change 13 to 8 13 13 to8 133 to8 13 to8 133 to8 and imagine if this was a thousand lines of code you'd spend you know two weeks trying to go in and make that correction what would be better what would be better would be if we used variables instead of numbers and so this is a very important engineering concept and a very important programming concept and that is you should never use numbers down in your program you should always use variables and Define your variables at the top and then when you find something like you should have used PIN 8 instead of pin 13 it would be very easy to change so how do we do this you have to declare your variables at the very top of the program and that is above the void setup because that makes it a global variable which means all parts of your program will recognize that variable okay all parts of your program will recognize that variable now if I declared my variable in the void setup if I declared my variable in the void setup then it would only be useful and it would only be known in the void setup okay it would only be known in the void setup so we do it at the top so you can use it any anywhere in the program now there are different types of variables there are ins which is short for integer integers are the round numbers 1 2 3 4 5 positive and negative so integers could be minus 3 Min -2 - one 0 1 2 3 but they're just the numbers they don't have a decimal point they're just those round numbers now the other type of number that you could have would be a float and the float are the numbers that are in between in math they're called the real numbers and so if I wanted to put Pi in my program and said Pi is equal to 3.14 that should be declared a what it should be declared a float so now for our pin we're going to make a variable for our pin should that be an INT or should that be a float well could you have a pen 13.5 no you're only going to have pen 13 12 11 so what type of variable should it be it should be an INT now I need to give a name to my variable and it's very important that we give it good clear names that if we come back later and look at our code it will make sense now could I name this variable kitty litter box yes I could but that would that be a very good description of what that variable is using or doing no it wouldn't what would be a better name I'm G to call it the red pen because it is the red LED pen you could call it red LED pen you could call it red pin you could call it LED pin but it needs to be something descriptive and now I'm going to give it a value we don't want 13 now we want it to be what eight okay and now who's your friend Mr semicolon all right so now I've defined red pin now what I'm going to do is I'm going to copy that name and down here everywhere that I use pin 13 I need to start here even everywhere that I use pin 13 instead of pen 13 I need to what call it red pen call it red pen red pen should have done this the first time right but I wanted to show you the wrong way to do it so that you could appreciate the right way to do it okay we'll get through through this get all of these 13s out of here we're almost there okay I think we're on those last dits one more okay there so now instead of putting in pin 13 I just Define red pin now I can make red pin 8 well if I'm going to make red pin8 I better come over here and I better move that wire to what I'm declaring which is pin 8 so I will come here and do you see that it's going in it's going in pin 8 so now I'm going to run the program come back over here where you can see it and this time it'll work just like before but I'm using what I'm using variables and so now I'm going to run it and it's looking pretty happy and look at that now as it's downloading what the LED is not misbehaving because I'm on pin eight and not pin 13 okay da da da did it da da da did it okay you see that's working but I used a variable so that's really good but what do you not like well the dits are too the dits are too short the dits need to be like 150 so what are we going to do the dits need to be 150 so what do I do I come in and I say 150 I say 150 no no because once you make it 150 I'm going to want it5 so what are you going to do you're going to come up here and then on this for the delays again it's not going to be you know 125.2 milliseconds it's going to be what it's going to be an INT and I'm going to call this dit and that's going to be what it's going to be 150 because I want it to be longer who's your friend Mr semicolon now I'm going to take this dit and everywhere I had a 50 or these I'm going to what I'm going to put a dit d d d d not not the 500 that's a what that's a DA so now I'm going to come in and fix these dits dit dit dit D okay and then d d and then there they all are okay we're done right no what do we need to do we need to do our dos so now I'm going to say int da and I kind of like the 500 on the Daws so now I'm going to take the variable da and then I'm going to put it down here so I'm going to put not dit those are all dits the 500 is going to be a da a da da a da a da a da okay so now I've taken all those numbers out so now everything is really good right are you happy are you happy are you happy tell me why you're not happy I used a number down here right when I'm looking at your guys homework I don't want to see any constants in your your program and what do I have here I have a constant and so I'm going to call this end weight like that if I'm going to use it down here I better do what up here I better declare it so int and it's going to be uh end weight like that and that's going to be a th000 like that okay let me make sure I did that right end weight end weight now when you look at my code what do you you see you see variables and you see no constants so let's see if this thing is going to run so we come here and then uh it's downloading watch the little LED and I have got a da da da da da da look at that how is that you know really I need to use pin eight for something else and so what we're going to do is we're going to move to pin three and guys I'm doing this because if you're working in engineering they're going to want you to change things so you better do your work where it is easy to change things so we're going to come in here and now red pen becomes what it becomes three we're going to look and make sure yes I have it in three and so now we are going to download it it's going down we're watching the LED it looks like it's compiling it's downloading and we've got a da da da da da did it it now what is the difference now now if your boss or your customer comes in and wants changes it's easy to change you just change the well-named variables at the top and the rest of the program is going to be okay and you could have a thousand lines of code and you don't want to be looking through every time that you set 50 milliseconds right you want to just always use variables and Define your variables up here at the top now I'll talk more about the other types of variables later but like if I wanted to put Pi in here I would make that a float why because it is 3.14 so I could say my Pi is equal to 3.141592 65 3.14 I got an extra one in there5 9265 okay semicolon at the end now we're not using my Pi but you see I can compile it and it will recognize now uh my the variable my Pi is float and so if I wanted to calculate the area of a circle I could say my Pi uh time R squar okay and I would set R up right so now I'm going to be using variables we're not going to use it today I'll just show you the other thing you can do there is also a Char and that means a character and it could be like my initial okay and then what is that it's let's say a p like that now when you give a value to a Char it needs to have the single quotes around single quotes but what if you wanted your full name well I could say then a Char is just one character if I want my full name then I have to use a string and the string is going to be my name and that's going to be what this time in double quotes Paul mcarter so I have what I have an INT that's a round number I have a float that is a decimal number I have have a Char or a character that's one letter single quotes and I have a string that is a string of letters and that's inside of Double quotes now it kind of annoys me that the word string doesn't change that uh happy little blue color but if we try to compile the code we'll see that it recognizes that string is a variable type it just doesn't turn it blue which bugs me a whole lot okay you see no errors it worked so what did you learn today what you learned is is that when you are doing a program you should never use constants or numbers down in the program you need to use variables you need to Define your variables at the top and you need to give the variable name something that makes sense now also notice how I like to use this bumpy font the red is all lowercase and then the first letter of the next word is uppercase same thing with end weight everything is lowercase except for the followon words the first letter make them uppercase and there's different conventions you can use of how to format your variables I like this bumpy font where everything is lowercase except the first letter of the follow on Words and that's the way I do it and that's probably the way I'll do it throughout this class you can do whatever nomenclature that you want but I like this one best some people will put underscores between them so they might put like redcore pin like that I don't like that very much some people would do like red Dash pin but I don't like dashes and underscores I really like that and so I just do it that way I think it's a great way if you guys want to follow along with me you might do it that way as well just because I think it is a pretty good way to do that okay what do we need to talk about we need to talk about your homework and what your homework is is to make a circuit with two LEDs a red LED and a green LED now what I want you to do is I want you to Blink the green LED five times fast 1 two 3 4 five on the green LED then the red LED I I want you to Blink 10 times slow so green LED five times fast 1 2 3 4 five and then the red LED blink 10 times slow now you can kind of decide what you want as fast and slow but then what you do is you make a video of your project really it just needs to be 30 seconds or a minute but you show me your code then you show me after you show me your code you show me your build and then what you do is you show your pro program running show that it works now when I look at your homework what am I going to look for when I look at your code I want to see what a whole bunch of numbers no I want to see you declare your variables and then I want to see you using variables and not constants in your code then what do I want to see on your build I want to see that it's neat and tidy and well put together then what do I want to see when you R your code I want to see that it did what I assigned and so then in the description of your video link back to this video leave a comment down below that points me over a link to your homework assignment and I will look at every single homework assignment and I'll comment on it okay because I really want to see are you guys understanding are you guys picking up what I'm throwing down you know what I mean I'm throwing it down are you picking it up or is it going right over your head okay and if I look at your homework I can better see if you're understanding it or not okay guys as always I want to give a shout out to you guys who are standing with me on patreon it's your help and support that's keeping this great content coming you can also help me with a thumbs up if you leave a comment down below that'll help me with the old YouTube juice if you haven't subscribed to the channel already make sure you subscribe when you do ring that Bell so you'll get notification when future lesson Dr lessons drop and as always share this video with other people because the world needs more people thinking like an engineer and fewer people sitting around watching silly cat videos Paul mcarter with toptechboy dcom I will talk to you guys later
Info
Channel: Paul McWhorter
Views: 4,414
Rating: undefined out of 5
Keywords: STEM, LiveStream, TopTechBoy
Id: Ucvwq5hFc3U
Channel Id: undefined
Length: 27min 36sec (1656 seconds)
Published: Thu Mar 14 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.