Arduino Uno R4 WiFi LESSON 21: Understanding While Loops

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 number 21 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 hopefully most of you guys already have your gear but if you don't take a look down in the description there is a link over to Amazon and you can hop on 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 do is I'm going to teach you about while Loops in Arduino and this is really going to be the homework assignment for the homework that I gave you in lesson number 20 so I should start by asking were you successful if you were successful leave a comment down below I Am Legend double just bump if you were not successful leave a comment down below I fold it up like a cheap Walmart lawnchair well anyway I hope you got it and what we're going to do is we're going to do this exercise and this homework to try to get you more familiar and really understanding the while Loops now we've learned that we can loop with the for Loop and then last week we talked a little bit about the while loop and then today we'll actually do something with the while loop and what the assignment was the assignment was to set up your RGB LED and to Blink it red green blue red green blue but the number of times you blink it you'll get that information from the user that will be user input and then you will have the red green blue red green blue you'll have that blink sequence not in a for Loop but to do do it with a while loop use the while loop to make sure that the Arduino blinks the led the number of times specified by the user sound good I hope it does so let's get out of your way and have a sip of coffee yes that is good coffee let's jump over to our overhead view and guys this is the circuit that we have had for the last couple of weeks notice that this is the RG B LED and the way that I have it plugged in this is the long leg so the long leg it's towards the bottom and that would then make this the red leg again that is the long leg that is ground and then this is the green leg and this is the blue leg so to hook this thing up the first thing we need to do is get it grounded and so this long leg this second leg in that same column as the long leg I've got this short wire going going to this ground rail I make the ground rail a ground rail by taking this black wire and jumping over to a gnd pin on the Arduino and so now everything along this entire row this entire uh bottom row or rightmost row is going to be a ground now okay now I need three 1,000 ohm current limiting resistors one for the red channel the green Channel and the blue Channel and so we take that uh red pin and then we put it uh in series with the 1 ohm resistor the 1 ohm resistor notice jumps over this trench and that way the things over here are not connected to the things over here this is the red Channel then I take a red wire and I go over to squiggly Wiggly pin nine the squiggly Wiggly pin nine because we might want to we might decide that we want to control the brightness and if we use one of those squiggly pins we will be able to use pwm to set the brightness okay go back and look at the earlier lessons if that doesn't make sense to you then this third pin is the green Channel we go through a 1000 Ohm resistor to a green wire which comes to squiggly pin 10 and then the last leg the fourth leg is the blue Channel again 1 ohm resistor we go to the blue LED and we go to the squiggly pin 11 so that's how we have this thing set up and let's come in here and let's start coding okay now what was your assignment again what your assignment was was to get input from the user how many times that he wants to Blink and if he says three times then what your LED should go as red green blue red green blue red green blue three blink sequences okay if he says four red green blue red green blue red green blue red green blue and then stop okay so that was the homework assignment and the homework assignment was to do it with while Loops not a for Loop but to do it with a while loop so that's what the homework assignment was let's go in and and get this thing set up first of all I have three pins so I need to set those pins up they're going to be integers and I have the red uh the red pin and that was equal to pin n don't forget the semicolon int and then we had the goren pen the green pen and that was set to pen 10 it was connected to pen 10 and then we have the blue pen and we had connected that to pin 11 like that now what else are we going to need to do well we're going to need to have some delays in there there's going to be uh there's going to be a short delay between the red the green and the blue and then there's going to be a long delay between the red green blue and the next red green blue so we're going to need an integer and this will be delay s for delay short let's make that 150 milliseconds that's pretty pretty fast and then let's go in integer delay L for delay long and this will be the delays between the blink blink blink blink blink blink blink blink blink so you see there's two different types of delays in there we'll make that a second which would be a th000 milliseconds like that now we're going to need to interact with the serial monitor so we need to to find a b rate so I'm going to say anbr is equal to 115200 like that okay uh now we're also going to down there we're going to need to counter as we're counting how many times it is Blink so I'm going to say the letter I and I am going to set that to the number one so the first time it's going to start out at the number one we've got it initialize there then we're going to get from the user how many times he wants to Blink so we're going to call that number link we're not going to set that we're just declaring it because the user will set the number of times that he or she wants to Blink now we need to ask him to tell us so we need we need a what a prompt and that's going to be a string and it's going to be my promp like that my promp and then that is going to be how how many how many blinks do you desire okay like that close it semicolon okay and I like to check up here yeah I got all my semicolons in I've been a good boy so now we'll come here come up and now if we're going to use that serial monitor we're going to need to turn it on so I'm going to do a serial. begin and what 115 to no no no no no you use your variable right BR and we set BR up there to uh 115,200 so we use the variable now we've got to do our pin mode so I've got pin mode which pin read pin and that is an output like that now I'm going to do a pin mode which one green pin and that is an [Music] output okay and and then I've got pin mode and then I've got a blue pin and that is an output and then put in my semicolon I am flying through this thing and I'm putting my semicolons in maybe this will run okay now we have got things set up now we need to come down to the void Loop now in the void Loop the first thing we're going to need to know is how many blinks does the user want so if I'm going to get input from the user what three things am I going to have to do ask wait and read ask him for what we need from him and then wait for him to put it in and then read it so how do we ask we do a Serial do print Ln and then what are we going to print we're going to print my prompt which we already set up up there now we've asked now what do we do we wait how do we wait we say while serial do aailable open close while that is equal equal zero uh if serial available is zero that means nothing has been put in and so what do we want to do we want to just sit and loop okay so this is the opening curly of the while clause and this is the Clos curly of the while Clause what do we want to do in the while Claus absolutely nothing we're just going to sit in loop loop loop loop loop waiting for for data when data is there then we will drop out of that while loop and we will land squarely squarely at this line of code and what do we need to do here now we need to read what the user put in we ask we wait we've done those two things now what do we need to do we need to read what are we going to read the num blink and that is going to be equal to serial do read int no no no you don't read an in you parse an INT hopefully you guys saw that parse int open close so now we have num blink okay now we have num blink and what do we want to do now now we want to Loop how many times num link times so what do I do I say while and while what while my counter my counter is I right while I is less than or equal to num BL link while I is less than or equal to num blink what am I going to do I'm going to keep looping between the open curly here and the closed curly everything in here I will do as many times as I Loop okay this curly closes the while claw this curly closes the void Loop okay and so we're keeping our Curly Straight here so now what do I put inside of this while I put the things that I want to do each time which is what blink red green blue okay red green blue so what I just need to do is I need to Blink red I need to Green blink green and I need to Blink blue and then each time through the while loop I will do that so how many times do I go red green blue red green blue red green blue that's defined by the while loop so what I've just got to do is get one good red green blue loop one good red green blue blink inside of this while loop and then the while loop will keep track of how many times you want to do that blink so what are we going to do we are going to do a digital right and then where we going to write the red pen and then what are we going to do we're going to take it high because we're going to turn it red now what do we want to do with the other ones digital right right okay and then green pen is going to be low right because whose turn is it it's Mr red pin's turn green pen wait your turn then I do a digital right and then what blue pin blue pin and then what low like that okay now this is kind of like the first I've got red on green off blue off now what do I need to do I need to delay because I want to keep that on for you to see it how long do I want to delay what do we call that short delay delay delay s d e l a y s like that so now it's going to stay on red is going to stay on for delay s now what do I want to do I want to turn everything off for delay s so let's take three these three lines of code let's copy them okay I'm doing a control C to copy and then I'm going to I hope I got four lines of code right because I want to I want to copy and paste that delay as well so I'm going to get the command to Red the command to Green the command to Blue and the delay I'm going to get those I copied them and now I'm going to paste them I didn't let me try that again so I'm going to get all four lines of code contrl C put my cursor her contrl B man okay there it is so now this turned red on and left it on for delay s now I want to turn everything off so now I will just say low like that okay so now I've got what the red blink now what do I want I want the green blink so what would be the easiest way to do that take all of this and copy it and then come here I'm going to skip one line and now this is the green blink so I'm going to take red this time it's going to be low whose turn is it it's Mr Green pin's turn to be high okay and he's going to be high for delay s and then he's going to be low for delay s and now what do we do we need to Blink Miss blue okay and so red you had your turn you go back low now and now blue pin it's your time to shine it's your time to shine okay and so now what do I have I have a red blink I've got a green blink and I've got a blue blink and now I just need that longer delay the delay and then this is delay long so you can kind of tell a space between the the blink sequence if that makes sense okay so now this closes my while loop this closes my void Loop I asked the user for input and then I am going to run this thing so let's go okay ah denied denied what is this nonsense oh s ah who did we forget Mr semicolon okay there we go we got Mr semicolon in there so now let's come up and let's run this thing I hate those ugly little orange complaints looking good looking good it's downloading and and boom okay now I've got to turn on my serial monitor now this is why I hate the new Ard the new ide2 it like really gets confused the first time you run the program and it sent that command to the serial monitor before we turn the serial monitor on so the serial monitor didn't get it so I'm going to click this little white button to just rerun the program that's already in the Arduino I click it how many links do you desire I desire three blinks okay three blinks now I need all eyes on the LED now here we go I'm going to press enter one two three boom got my three blinks I got my three blinks did you see that it asked me how many blinks do did I want and I said three and I got I uh it's still blinking it's still blinking somebody tell me why it's still blinking somebody tell me what it's still blinking it's not working you know you're going to say it's not working yeah it is what is it doing it's doing exactly what we told it to do and what did we tell it to do we said we are going to keep blinking as long as I as Le less than or equal to num blink but but we said we only wanted to to Blink three times ah what's going on here well let's see if we can figure out what's going on what if I just came in here and I said well let's just see serial.print what do I want to print I want to print I what's going on with I right I should go one two three and then and and then it should it should fall out of there so let's let's print that there and let's make it a print l in like that okay okay how many blinks do you desire I am going to say three now this time you're not going to be looking at the Led we want to see what that I is doing that we printed out so I'm going to say three okay one one one one ah in the for Loop the for Loop itself increments the index for you but in the while loop who has to in who has to increment the index you do and what did we not do we didn't we didn't increment it we just came here and said as long as I is less than num blink well we don't change I and so I is always going to be less than num blink what do we need to do down here at the very end we need to increment our index or our counter so what was I is going to become I + one now let's run it okay okay how many do I want I'm going to say three right I'm going to say three and it is going to go one and then it is going to go two and then it is going to go go three boom we got that thing got it boom okay now we can come in and we can take that print statement out we can take that print statement out right here we can take that print statement out and now we're going to run it [Music] again this time you're going to be watching the LED okay and how many times do I want to Blink I'm gonna say five and I want you to watch the LED this time okay so I'm going to click enter there's one two 3 4 five okay boom and it stopped it stopped success Yes we made it blink five times okay now I have to ask is everyone happy typically most of you guys will be happy but here is what the problem is and this is the fundamental failure of almost all programmers that I know and that is they don't fully test out their code they sort of tested it and it looked like it worked but what is the problem they didn't really shake it very hard they ran it one time and it worked but they didn't shake it to try to break it okay that's what you got to do with your code you got to shake it till you break it and then you got to figure out why it broke so I'm going to come up here and let's try it again instead of five what if I said four I'm going to put in a different number I'm going to say four and now I'm going to click [Music] enter and it didn't blink okay somebody tell me why did it like the number five but it didn't like the number four so when I asked for five blinks I got it but then it didn't work for four somebody tell me if you're in the live stream put a comment if you're already watching it later leave a comment down below so we knew it liked five but it didn't like the number four why did it not like the number four well let's see maybe maybe it just likes odd numbers so let's try three okay didn't like three either well how about two how about six how about seven tell me why this code only works for the number five tell me why this code only works for the number five so let's try that again five okay five now it doesn't even work for five okay well something's wrong it's not working what is it doing it's doing exactly what we told it to it's doing exactly what we told it to so let's go and and let's put that print statement in again okay let's put that let's figure out what's going on so right now it's not even going into it's not even going into the uh it's not even going into the while loop at all so what am I going to do I think I better come in and since it's not even going into the while loop I'm going to put my sir serial.print Ln out here I'm going to print I what is going on with i that the first time through it goes into the while loop and then the next time through it doesn't go into the while loop so let's see if we can figure out what is going on we will click like this okay so now it's asking me for a number let's say four and then it is is going to go 1 2 3 one 2 3 that's two and then that's three and then that's four it blinked four times okay it blinked four times now this is really strange it printed out one okay and did I put that in the right place okay let me let me start this again again let me clear this out and let's see if we can figure out what's going on okay how many blinks do you desire so I'm going to say three okay and then it's going to come down and it should print out what i is right here it should print out what i is right here so I'm going to say enter okay and then one okay now it asks me again again and I'm going to say three again H the first time through it said I was one and then the next time through it started out with I4 well why was I won I was my counter and I sent my counter to one and so when I got down here and went through this the first time I went through it the first time I was one as expected okay but what happened as I'm going through the while loop I becomes two three four and then it drops out and then it goes back to the top it gets my new number but who's sitting there at four I is so what did we forget to do we counted I up but we forgot what we got a res set it right we got to reset it so right here after I go through the while loop I need to set I I need to set my Loop counter to one again okay so now let's run it this time okay let's run it this [Music] time okay how many blinks do I want three and be watching the LED and let's see that's one 2 3 okay now it ask me again I want seven this time and I've got one 2 3 4 5 6 7 okay so what did we learn today what did we learn today with file Loops what's very very important with file Loops you have have to make sure that you increment your index that you increment your counter and just as important as that you have to make sure that after you've gone through the while loop and then you're coming back for the next time through you have to reinitialize your counter so you have to initialize it and you have to increment it and if you forget either one of those it's not going to work now you can imagine that I made those mistakes on purpose because there's a couple of things that I wanted you to learn yeah I wanted you to learn about while Loops but what I really wanted you to learn about is is that you have to check your code more than just running the program one time you've got to check it with odd numbers you got to check it with even numbers you got to check it with big numbers you got to check it with small numbers you got to check it in all of these different ways and just really shake that program as hard as you can because one of the things that as I've interacted with coders and programmers the worst problem that they have is they don't fully check their code they get it to work one time and then they pass it on to the next guy and then when it doesn't work you know what they do they blame the US user now I'm not trying to be hard on programmers but I'm saying that as we are going to develop code we have to do better than that we need to be the ones that Find the Errors because when our programs get really really big and we don't fully test every single part of it under every single condition it could be that the first time those little Snippets of code that are deeply inside of a 10,000 line program ever get e executed it's when the 747 is at 32742 FT altitude and it's nose is tipped up by 10 degrees and then under those conditions if it Banks left by a half of a degree that's when you get into that while loop that you didn't increment your your index and the airplane crashes and you know I'm only slightly slightly exaggerating here is is that when programs get complicated there can be certain conditions that would be very unlikely for you to end up in but when you end up in those if you haven't tested them then you can have disaster so as good Engineers you see I'm not a coder I'm an engineer and I'm not trying to get you to think like a coder I'm trying to get you to think like an engineer and as an engineer the most important thing that we're going to do is really really really test out our code code really shake it up really make sure it works and that's why when I taught students in in the classroom in real life I would always you know I would just sit there and these were poor little freshmen and you know they're in there learning their first codes and no matter what my goal when I graded their little project was to try to break it and so they're sitting there and they're typing in red green blue and then I would deliberately misspell red and then I would give them a bad great now I would do that just to get their attention of course when they fixed it I would pop the grade up and so I didn't deliberately try to try to ruin their lives and they would all end up with good grades but I was just trying to emphasize to them that that I was going to try to break their code and so they would be sitting there and they would try to think of every possible thing that I might do and then when they had thought of everything then I would always try to think of one thing that they hadn't thought of and so I was trying to instill in them the habit of really really uh debugging and testing and and and and putting that code through the pieces before you gave it to the next person so that's one of the important things I was trying to teach you today also with while Loops you know really while loops and for Loops anything that you can do with a for Loop you can do with a while loop and I kind of prefer the while Loops a little better than the for loops and I'll tell you why in a minute but but it's just the downside of a while loop you have to make sure that you initialize your counter and you have to make sure that you increment your counter and that's a little bit more taken care of for you with the for Loop and in the while loop you have to be managing that but if all I do is while Loops I get really really good at it now why do I like while Loops better than for Loops well with a for Loop you can say something like if you want to Blink five times you can say for I equal 1 and then keep going as long as I is less than or equal to non blink and then IAL I + 1 or I is equal IUS one or I is equal to 2 * I you see you all you're kind of forced to manage it right there inside of those parentheses where you're setting up the four Loop okay and so that's the nice thing about the for Loop but the nice thing about the while loop you don't have to when you go into it know the exact condition so let's say that I want to open the window or close the window and I have a while loop and the first thing I do is I measure the temperature and then I say while the temperature is greater than 100° leave the window window open okay so so you see I can do things like while the temperature is more than 100 turn the LED on so you see I can do things and that might Loop through a million times or it might Loop through one time but I can set my while loop up based on something that is changing to where the four Loop is more like count 1 to 10 now if I were doing things like averaging grades or inputting data or if I was doing just strictly coding or software types of things I would tend to use for Loops but anytime I'm going to be using embedded systems if I'm going to be using embedded systems where I'm controlling a circuit or a Servo or doing something you know that sensors and actuators then in that case I really like the while loop a little bit better okay guys I hope you are having as much fun taking this class as I am making them and uh as always I want to give a big shout out to you guys who are helping me out over at patreon on it is your encouragement and your support that keeps this great content coming really appreciate you guys you can also help me by giving me a thumbs up or leaving a comment down below if you have not already make sure you subscribe to the channel when you do ring that Bell so you'll 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 few are people sitting around watching silly cat videos Paul quarter with toptechboy do.com I will talk to you guys later
Info
Channel: Paul McWhorter
Views: 2,298
Rating: undefined out of 5
Keywords: STEM, LiveStream, TopTechBoy
Id: Cw_i7l_RFVE
Channel Id: undefined
Length: 34min 6sec (2046 seconds)
Published: Thu Jun 13 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.