Arduino Uno R4 WiFi LESSON 17: Control Multiple LED from the Serial Monitor

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is Paul mcquarter with toptechboy dcom and we're here today with episode number 17 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 poured 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 now most of you guys probably already have your gear but if you don't look down in the description there there is a link over to Amazon you can hop on over there and pick your kit 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 do is I'm going to show you my solution to the homework assignment that I gave in lesson number 16 but I must first ask were you successful if if you were successful leave a comment down below I Am Legend double chest bump if you are not successful leave a comment down below I fold it up like a cheap Walmart lawn chair okay hopefully most of you guys were successful because I hope you see what I'm trying to do in the homework is give you a homework assignment that draws on those skills that we learned in earlier lesson so I'm trying to reinforce those things that we learned earlier on so they really become part of your toolkit you really understand the things that you're able to do on the Arduino so hopefully you guys were able to do it what was the assignment you were to make a circuit with three LEDs a red LED a yellow LED and a green LED and then you were supposed to turn the LEDs on or off based on input from the user so you would ask the user what color do you want if he says red you turn the red LED on if he ask for yellow you turn the yellow LED on so forth okay hopefully most of you guys were successful but let's jump on over here and let's get me out of your way and then let's take a look here what you're going to need are the three LED green yellow and red and you're going to need three 1,000 ohm current limiting resistors again on these resistors I use a little set of pliers to cut those long legs off so my build stays a little bit neater now the first thing that I'm going to do here is I'm going to plug my LED in and in all cases I'm going to put the long leg of the LED pointed upwards pointed towards the top of the breadboard and I always like to just kind of leave a blank row okay do you see how I leave a blank I guess that's a column A blank column between the pins just to give it a little bit more space now I'm going to come in same thing the long leg of the LED is going to be pointed up I have skipped a I have skipped a column between the pins do you see what I mean by skipping a column I don't try to jam them in closer than that and then finally we're going to come down to the red okay and so the red is there in all cases the long leg of the LED is paint pointed towards the top there okay so I've got my three LED what I want to do now is I don't want to run a line from each one down to ground so I'm just going to run that through the resistor but I'm going to start by creating my ground rail so I'm going to put my black wire in this bottom row and we need we know all of those holes in that bottom row all of those holes in that bottom row are connected and then I'm going to come over and I'm going to plug that into G and D and so now what do I know I know that this entire bottom row the entire bottom row is ground so now what I want to do is I want to go from the short leg of the LED down to ground and so I'm going to come over to the short leg of the LED it's hard for me to get this it's hard for me to get this where both you and I can easily see it at the same time so I'm making sure you can see it so that wire is going into the short leg maybe I should start down here at the ground okay so I got one leg at the ground and then I've got one leg going to the short leg of the green LED very nice short leg of the green LED going to the uh current limiting resistor which goes to ground and so hopefully this one will go a little easier so I'm going to go there ground rail up to the short leg of the yellow LED that's looking good and now I'm going to go from the ground to the short leg of the red LED like that okay I think that is very neat now I need to come over from those long legs and I need to connect them to uh I need to connect them to uh these digital pens and I'm just going to try to go in order now I didn't give you an assignment to control the brightness but I'm going to use three of the pwm pens just in case later on we wanted to set the brightness and so I'm going to start with the green and the other thing I should say that I like to do if I can at all I like to kind of color code my wires and so I'm going to start with the green LED and the long leg of the green LED I'm going to use a green wire and I am going to come over and I am going to connect that to pin 11 why pin 11 is a pwm and I've got three in a row 11 10 n so the green is going to go to 11 and I'm going to check that because yeah you see the green one's going to 11 and now what do I have the long leg of the yellow LED and with a yellow wire I will come over to pen 10 okay you see pen 10 and now what do I have I have the long leg of the red LED the long leg of the red LED and I'm going to come over to pin 9 okay so you see I've got 11 109 and then I come over let's see if we can get this a little bit neater and then through a yellow wire or through a red through a green wire a yellow wire and a red wire I come over and then I go through the current limiting resistors I go through the current limiting resistors down to uh down to ground and let's see if I can zoom in a little bit there okay so you can see that pretty good now let's come over to our code view I think we're ready to start coding this thing up so let's see what variables are we going to want to use here uh first of all I should say we're starting with bare minimum so you would you would fire up your IDE you would come over and you would say examples basic and you're going to go to bare minimum open that and then we want to make sure the board is connected so I'm going to come over and I'm going to go down to the port and you can see that my board is connected to Comm 5 you would connect to whatever Comm your board is connected to and it's very likely you might not be on five you might be on something else and let's make sure we have the right board selected the Arduino Uno R4 WiFi that's Arduino Uno R4 WiFi that all looks good so I think we're ready to start coding so we've got three we have got three LEDs we got three LEDs so let's set up three pins those are going to be integers so I'm going to say red or let's go in order green pin and that is going to be equal to what was that I started with 11 let's check that and make sure yeah it's on 11 now do you see when I had to go check do you see how nice it was that the green wire was going to the green so I could just look and I could see the green wire was going into pin 11 like we wanted so that is pin 11 then what do we have we have int and we've got yellow pin and that's going to be equal to pin 10 and then I've got int and then I have got red pin and that was equal to pin N I do believe okay so we have those set up now I'm going to get the user input so what I'm going to do is I'm going to read a string in so I'm going to need a string variable and then what I'm going to call call that is that's user color that's the color that the user wants I'm not going to set that anything cuz I will be reading that we're going to use the serial monitor so let's go ahead and set a b rate BR is going to be equal to 9600 you guys you can use 115200 if you want you can use one of the other ones you've just got to make sure the serial monitor is set to the same B that your program is set at very important since we're going to be each time reading a value I don't think that I need to put a delay in there so that's a pretty good set of that's a pretty good set of uh of uh variables to get started with now we're going to be using the serial monitor so we need to start it serial. begin what do we want to use what do we want to use 9600 no no no no no what do we do we use the variable that we set up BR okay for B rate put in our semicolon now we've got to do our pin mode so we're going to do pin mode and then what are we going to do green pin and that is going to be an output like that okay do you see how I like to do things in order do you see how I like to try to stay organized I've got green I've got yellow I've got red I've got a green wire I've got a yellow wire I've got a red wire when I do my variables I go green yellow red now I'm doing my pin modes I'm going to do green yellow red I try to keep things as logically organized as I can because it makes it a lot easier later on if we have to do some troubleshooting okay now let's just keep going here pin mode of what yellow pin okay and that's going to be an output also notice how we we use the same convention uh we use the same convention in how we name things so like I didn't call this yellow LED okay or I didn't call this you know I didn't call this uh I didn't call this uh you know I didn't call this yell light okay you can do whatever you want but once you kind of set it in if I'm going to say green pen then I'm going to say yellow pen and red pin if I was going to instead say green LED then I would say yellow LED and red LED be consistent in kind of how you're naming these variables and your life will be a whole lot easier so now we're going to go yellow pen like that okay now we're going to come down pin mode and then we're going to do what blue no not blue pin we know it's red pin Blues are only for special occasion red pin and then that's going to be an output so now we have the serial monitors started and we have our pin modes done we are doing really good now we need input from the user to get input from the user we have to do three things what three things do we have to do ask wait read and I think I'll be a good boy and make my prompt up here a string and I'm going to say the string is going to be my is going to be what were we going to call that uh string user promp user prompt like that and what that's going to be is please indicate uh let's see please enter color okay and then I'm going to say red prob even say it this way red I'm going to go green green yellow or red okay again I'm just trying to always go in the same in the same order okay and then put our semicolon in so that's my prompt now I'm going to come down here I want to get from the user what color he wants what do I have to do I have to ask and I'm going to do a serial.print Ln and what am I going to print I'm going to print user promp like that now what do I do I wait so while serial. of you know when I you know what happens when I say serial my iPad thinks I said Siri and then it starts listening and I'm finding that quite annoying I wish I could just make Siri go away okay so we've got serial. available okay serial. available open close while that is equal to zero right no no it's a conditional it's a what it's an equal equal anytime we're asking it is serial do available is it zero you have to do an equal equal if you want to set something you just use a one equal if you're asking what it is or it's condition in a conditional it's always the old double feel double equal okay so now I'm going to open my curly and then it closes the curly for me what do I do in this Loop absolutely nothing it just sits in loops and weights so I've got an open and I've got a close curly and then this curly closes the void Loop just want to make sure everything okay and I'm going to take that blank space out right so now I've asked I've waited now what do I do I read what did we call that serial that silly variable we called it user color so user color is going to be a serial. parse par parse float no no not a parse float we're reading a what we are reading a string so we're going to say serial. read string like that okay serial do read string and we've got to tell it where we are no we don't we don't have to tell it it just reads it from the from the serial monitor so user color is equals serial. readstring like that that looks good now what do we have to do we've got to figure out what we asked for we got to figure out what we ask for you know so let's see so let's say if and then we always put the conditional inside of the parentheses we put the conditional inside the parentheses so we say if user color equal equal what okay equal equal man give me a couple of equals here man equal equal red okay if that then what am I going to do then I am going to do a I am going to do a I'm going to do a digital WR digital WR and you know what let's keep going in order so I'm going to say if user color is green if user color is green then I'm going to digital write where green pin and it's going to be what hi like that okay now let me ask you right here tell me a problem with this uh if user color equal equal green well what if he puts an uppercase G or what if he makes everything uppercase so what did we learn last week that we really want to do we want to take that user color and we want to dot to lower case like that and that takes it however they put it in uppercase lowercase and it changes it to lowercase and then I just compare it to lowercase and that should work so that's the first one so then if user color equal equal yellow okay I don't have to set it lowercase again because I already did it I just do it once and then I see what it is what do I do I open my curly I come down and then what am I going to do I'm going to do a digital right and then which pen yellow pen and it's going to be a high high like that okay that looks good and now if user color equal equal what red then what am I going to do I'm going to open my curly come down and then I'm going to do a digital write and then I'm going to write red pin I'm going to write it high like that okay now I want you guys okay so let's first of all look and make sure so this closes the if that's right this closes the yellow if that's right this closes the green if that's right and then what does this bottom one do that bottom one closes the void Loop so it looks like it really looks like that I've got my uh curlies all balanced out that's good and so if I type in red whatever case it is it'll turn red if I type in yellow it'll turn yellow if I TA in green it'll turn green but you guys tell me kind of a logical error I have here what is something that isn't really good okay what's not really good is I'm never turning the LEDs off and so if I say what color do you want and I say red and then next time what color do you want and I say green what's going to happen the Red's still going to be on from the last time I'm going to have red and green so in each one of these if I ask for red I better also do what digital right what red pin and it needs to be low like that all right and then digital right yellow pin and it is going to be low like that okay I hate those popup things and so I've got green and really I like to keep things in order so I'm going to cut that one okay and then I am going to paste it there so green yellow red and it's going to be high low low well really I want to set so each one each time I want to be deliberate you say well what if you hadn't turned yellow on it's okay if it's off I can still turn it off all right so each time I want to set each one so I'm going to take those and I'm going to put them inside of the yellow okay inside of the yellow if statement only this time green is going to be what it is going to be low and yellow is going to be what yellow is going to be high and then uh red is going to be low now let's come into the red one and let's do the same thing I'm going to paste all three of them and then I'll just make them right so if it's red green is going to be low and yellow is going to be low and then red is going to be what high like that and then it comes back and it asks you again for what you want does that make sense okay so here we go I'm going to come in and I'm going to run this ah what is that ugly orange business ugly orange business what is that pin mode red put pen output what did we forget our friend Mr semicolon did you guys yell at me when I did that I'm sure I'm not the only one that does that okay let's run it ah what is it not like what is this orange BR does not name a type I thought I'd Define BR B ah what did I do here I said BR is 9600 but I didn't tell it it was an INT okay guys why do I make mistakes because you guys that are new sometimes you panic when it doesn't work and what you got to see is if if it doesn't work it's just you made a mistake go look find your mistake all right so now I really think it's going to work this time oh now that one okay so where was that pin mode serial. oh look at this look at this what is this this opens the void setup and this what closes the void setup I didn't what I didn't put these commands inside the void setup so what do I need to do I need to take that out okay and then I need to end the void setup after I need to end the void setup after I put those commands in okay like that now what I want you to see is look at the look at the strange error message it got it says okay it was if user color equal equal green okay it was down way down here that I had the problem and I've got another problem here too what did I not do I didn't put my semicolon there so I had several problems in there okay but this one was saying that it didn't have the uh it didn't have the semicolon after the token okay so you can sort of see what that was okay so now we're going to come in we're going to run this thing man what is this while it doesn't like my while loop oh look at that no semicolon okay so what I wanted to do there is I wanted to show you it's complaining about the while here and it exits okay but the error is not here the error was on the line before it so sometimes it'll say something's wrong with that while loop and you look and say ah it sure looks right it sure looks right well it might be the line before it that you had the error okay so let's run it uh it's going to run this time look at that look at that downloading it's looking good okay now we turn on the serial monitor up here okay and now it sent that prompt before the serial monitor was on so I'm going to have to click the little button to have it run the program again and ah now it is asking me and now I am going to say r e d and boom look at that we have the red LED turned on now let's try green okay what do I like the green LED turned on okay what also do I like I like the red turned off when I selected green now what do I want I want yellow okay and when I do that the yellow comes on I do like that and let me just look up here I did the Green green pen output yellow pen output red pen output that all looks good just wanted to make sure that all looks good now let's let's say that let let me try this watch this I'm going to put uppercase y uppercase L lowercase l o uppercase W and let's see I click enter and it goes yellow I guess I should do r e d like that crazy case and boom it works all right right guys so do you see how we're getting more and more comfortable with the Arduino what do we know how to do we know how to do digital rights we know how to do analog rights we know how to do delays we know how to get input from the user we know how to print to the serial monitor we know how to declare variables we know about integers floats and strings okay so we have learned quite a bit and what did we learn today we learned more about using if statement and we learned more about using conditionals okay so what we're going to do is so we've learned a little bit about if statements again what I like to do is I like to give you a homework okay so this time what I want you to do for your homework is leave this circuit leave this circuit hooked up the way it is but this time I want you to ask the user two questions what led do you want and then number two what brightness do you want and then you turn that led that brightness and then it goes back to the top and then it asks you again one at a time for which led and how much brightness and that is your homework when you do your homework make a short video of it can be just like one minute long post the video to YouTube showing your code and showing it working and then in the description of your homework solution in that video link back to this video right link back to this video and then in this video in the comments down below link over to your homework solution and I will hop on over there and take a look at your solution so that's your homework assignment is to expand this where you're not just turning an LED on and off but you're also setting its brightness if that makes sense okay so every lesson we like to learn a little bit of software a little bit of programming and then next week what we're going to learn is we're going to learn a new component we're going to get a different comp component out so we're going to be going back and forth between learning something new in programming learning something new in Hardware programming Hardware so we try to kind of keep it interesting and shake it up a little bit okay guys I hope you're having as much fun taking these classes as I am making them I want to always give a shout out to you guys who are standing with me on patreon it is your support and your encouragement that is keeping this great content coming you can also help me by giving me a thumbs up or leaving a comment down below if you have not already subscribed to the channel when you do make sure you ring that Bell so you get notifications When Future lessons drop and most importantly 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 tocom I will talk to you guys later
Info
Channel: Paul McWhorter
Views: 2,619
Rating: undefined out of 5
Keywords: STEM, LiveStream, TopTechBoy
Id: Ai7uqYHt_Yc
Channel Id: undefined
Length: 28min 27sec (1707 seconds)
Published: Thu May 16 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.