LESSON 7 Using While Loops with Arduino

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys i'm palma quarter and I am here with Arduino lesson number seven and in this lesson we're going to be looking at a new type of loop we're going to be looking at while loops and we've done looping before we've done looping with for loops and the while loop is not better than a for loop it's just a little bit different the truth is you could do almost anything that you ever wanted to do with a for loop but you do need to understand a while loop because there might be a few unique circumstances that a while loop would be better than a for loop and also you might be looking at someone else's code and you might see this and think well gee what's that about and so a while loop is just another way to loop okay some people prefer for loops some people prefer while loops 99% of the time a job you could do with either a for loop or a while loop just to kind of catch you up where we are we are still working on this - LED circuit the two LED circuit if you need help hooking this up we really went through it in quite a bit of detail in lesson number three and see the link down in the description of this video I will give you you can go there and go to lesson three and you can see how to hook this circuit up and then also if you look at our resources for lesson number seven which we're on now you can get this code that we've been building in this series of lessons okay you can kind of see what I'm doing here every lesson I just build a little bit on on the previous lesson and so far we haven't changed the circuit a lot because basically it's hard to learn circuits and programming at the same time so for right now in these lessons we're kind of working with this circuit and we're learning a lot of programming very quickly though we're going to have these core programming skills and then we're going to start expanding what we're doing on the circuit side so as you go through this series of lessons we're going to be over here learning about programming and then over here learning about circuits and always our circuits and our programs working working together but for right now we're going to do a couple more lessons still with this - with this - LED circuit so again go to lesson three and if you need help getting that hooked up go to lesson seven and you can get this code so you can catch up without with where we are if you have not not been working with us what we did in the last lesson is basically we've got a lot of programming tools under our belt already and it's starting to get pretty pretty fancy what we can do basically if we look at this code up here at the top we declare our variables then in our void setup we put the things in the void setup that we want to do one time the void setup like all clauses they're sort of like a paragraph it starts with the open curly bracket and it ends with the close curly bracket so everything between those curly brackets is the void setup what we do is we turn the serial port on we do our pin modes right any time you're going to use an art app in an Arduino you need to tell it whether it's going to be an output or an input then we prompt the user for information we print out to the user serial monitor how many times do you want the red LED to blink then we wait for him to answer and then we read his answer into the variable num red blinks we do the same thing for the yellow blinks so we find out how many times he wants to blink and then we blink the red LED and then we blink the yellow LED in this particular version of the program we prompt the user for the number of blinks and the void setup so once he enters it then it just goes it blinks the red one then it blinks the yellow one it blinks the red one it blinks the yellow if we wanted to change that every time these inputs are these prompts we would go ahead and put in the void loop instead of the void setup in that time that way every time through the loop it would ask the user for input but the point is that at this point we can go out and we can get information from the user we can do something based on that information and then we can send information back out to the user so all of a sudden we're getting a lot of these puzzle pieces put together let's just take a quick look at this program see what it's doing now download the program we interact with the user right now through the serial monitor whenever we always turn the serial monitor on if we're going to use it and we turn the serial monitor on with a serial again and we can call up that serial monitor and then it asks me how many times do you want the red LED to blink I think five don't want to play favorites here so the yellow one we're going to blink five times as well okay now you notice the red is going to blink five and then the yellow blinks five and in synchronize with that blinking over here we're printing out to the serial monitor we're printing out which LED is blinking and which blink we're on so you see we're getting a lot a lot of these little pieces a lot of these little pieces working together now well the truth is is that there's another type of loop that you can do you can see here the way we did this was we did it with a for loop the for loop here is blinking the red and then the for loop here is blinking the yellow and the way the for loop works is we just say four and then we set some integer we call it J it could have been I it could have been Z it could have been your name it could have been anything but this counter we set it we tell it to start it one we tell it to keep going through this loop as long as that variable J is less than or equal to this number and then we tell it every time that it goes through we want it to increment J by one and so this is the condition it is going to keep looping as long as this condition is true okay the loop starts with the open curly bracket and the loop ends with the close curly bracket okay that's what we've sort of been calling a clause it's like a little group of that's inside those curly brackets that all sort of odd happens happens together happens in sequence okay so that's that's how a for loop works and that's that's pretty neat but there's another type of loop that we can do and it's called a while loop so let's take a look at a while loop I'll just open up a new window here and we'll just write a really minimal program to kind of show you how a while loop works we're not going to be doing any blinking of the LEDs with this we don't need to go in and do any ten modes or declare a lot of variables the one thing we do need to do though is we need to open our serial monitor so we do a serial dot begin okay and the serial this is capital let's go 115200 hmm and then ending in a colon semicolon okay that's about all we probably need to do right now in the void setup let's come down here to the void loop this is how you do a while loop if you're going to do a while loop you probably need a counter and so we need to have some sort of counter usually we call a counter either I or J or C and T let's go ahead and call it J so I'm going to declare a counter J and it's an int and so I've declared my variable J as an int I created the bucket I've labeled the bucket J and I've told it that that bucket is going to contain hints I'm going to go ahead and put a number in the bucket and want to set it equal to one okay now you could declare that variable up at the very top before the void set up if you did it would be a global variable in all the programs would be working with that same value of J a lot of times it's good to not use global variables but to use local variables and if the only thing that really needs to know about this J is this while loop I'm fixing to make then maybe it makes more sense to make it a local variable and declare it here inside the void loop so we now have a counter and that counter variable we have called J and we have initialize that counter to one so let's be good boys and girls and let's put our comment in here we're going to declare J is an int and set it equal to one okay now this is where we're going to do our while loop and the way a while loop is I'm going to put a white line there just to make it a little easy to see what I'm doing we're going to say while we're going to turn the code off sorry okay and we are going to get that off sweet okay we are going to sorry about that e we're going to start our while loop with the word while and then we will put some condition we will come back and put some condition between these parenthesis and this loop will continue to execute as long as that condition is true so now what I'm going to do is I'm going to come in and I'm going to start my while loop it starts with a curly bracket and it's going to end with a closing curly bracket and anytime I make a loop whether it's a while loop or a for loop or any type of Clause I put that ending curly bracket in right when I create the loop okay because if you don't you're going to come in here you're going to write a lot of code and then you're gonna not you're going to forget to put your curly bracket there and then you're going to see this and say oh I got a curly bracket problem is this curly bracket goes with this one this one opens the void loop this one closes the void loop this one opens the while loop and this one closes the while loop so anytime I put a loop and I go ahead and I put my starting bracket in my ending my starting curly bracket in my ending curly bracket and so now this Clause is going to continue to execute as long as the conditional that you put in here is true okay so let's put a conditional in here we're going to say keep looping as long as J our counter is Lin or equal to let's say ten okay so this is going to keep looping as long as J is less than or equal to ten and then I'm going to come down here and I'm just going to do a print serial dot print and I want it to increment to the next line each time so I'm going to say print L in like that and then what am I going to print I'm going to print the variable J I'm the Jade is not going quotes because J is a variable I don't want it to print the character J I want it to print the variable J you remember the J bucket what's in the J bucket that's what I want to see so I just say print line J and in all of our commands we in with a colon all right now I want you to look at this and think for just a second what would happen if we ran this right now just look at it and ponder it for a second well we come up J is equal to 1 so it's J less than or equal to 10 yes it prints J and then it comes back as J less than equal to 10 yes it prints J at Prince J it prints Jade's print shape there's no way for it to break out of this loop because J is staying at the value 1 and so what we've done here is we created an infinite loop and if we ran it it would just sit and print out the number one forever so what you've got to do when you write a while loop is you've got to make sure you're doing something that lets you break out of the loop in what you typically do is you increment your counter J is equal to J plus 1 so the first time through J is 1 when you get to the bottom loop J is equal to 1 plus 1 which becomes 2 and then the next time through 3 4 5 6 7 8 9 and then 10 Willis J less than or equal to 10 yes it goes through the tenth time it adds 1 2 J when it comes back up here at that point J is 11 it does not enter the loop it comes down here and it just does the next line after the end of the loop so it keeps looping as long as J is less than or equal to 10 and each time J is equal to J plus 2 okay so let's go ahead and run this is ooh ooh forgot my colon why didn't somebody say you forgot your colon semicolon all right now let's see looks like things are happy let's look at our serial monitor and look at that printing a lot of numbers but it's printing so fast we can't really see it so one of the things you see is computers can run a lot faster than we can keep track of them and so if we want to be looking at these numbers we need to put something in there to slow it down and so let's slow it down let's put a delay in this loop of about 250 so that it we'll kind of pause about a quarter of a second remember these delays are in milliseconds it'll pause at about a quarter of a second and so we can see the numbers come in by a little better all right download the program it's green everybody's happy now let's look at our serial monitor and look at that one two three four five six seven eight nine ten one two three four five six seven eight nine ten and so it is looping through there why does it repeat well it repeats because after it does it comes down and we're in the void loop so it comes back it runs the void loop again and then it loops through the while loop here and so it's looping through the while loop and the void loop and that's why the numbers 1 through 10 keep repeating one of the things is I think once you do something usually as far as formatting your output once you do something inside of that while loop when you leave it's good to put like print a blank line so you can see which group of code or which group of printout came from inside that loop and so a lot of times what I will do is I will just after it do a serial dot print print line and then just print a blank just an empty line and that makes it a little easier to read the code as it goes through what did it not print that's not even close red line PR int Ln all right looking green looking happy okay there we go let's look at this and so now we count to 10 and then we get a blank line in there and see it makes it a little easier to see those groups of 10 and we've we've slowed it down so it sort of makes it it sort of makes it a little bit nicer a little bit easier to read ok now let's practice a little bit with our format formatting of our output instead of just printing J if a person I just came and saw this they might not really know what those numbers are and so let's put it in conjunction with a string that says you are on loop number okay so when I come here I'm going to add another text print out a text string so I'm going to say cereal print wine if I want to print a string just plug a string in here I need to put it in the double quotes you are on loop loop number and then close the quotes close the parentheses add the semicolon so let's see what this is going to do now we're just practicing stuff here that we've learned before the while loop is new but we're just practicing some of this formatting business okay so here we go you are on loop number one you are on loop number two three okay let's stop that from scrolling do you see how this is not I mean you normally don't write sentences that what you would really like the eight to be here on the same line as that and that way it would be easier to read so how would I get this this line this print command in this print command to print on the same line well don't advance the line on the first print so we just say serial print instead of serial print L in and it'll print this stay on that line and then it will print this and I bet this looks a little bit neater if we do this okay let's take a look at it alright you are on loop three four five okay now what's wrong with this okay and this happens quite a bit that is you see I put that and then the one and there's no space there's there's no space there because I didn't tell it to put a space there I just ended right with that R and then the J comes in right after that so we should put a space right there let's try it again we've got to download it alright okay so let's take a look at it now you are on loop you see and there it is the number 1 2 3 4 5 6 7 8 9 so you see this is the same every time and then because we just printed out a string we just plug the string in and here we're printing out a variable and every time through j is equal to something different okay now you can get creative in with these with these while loops you can do different things depending on your application and so let's say I wanted to know all of the even numbers between 1 and 20 ok so I start with J is equal to J here let's start with J equal 2 so you see we don't have to we don't have to start with 1 we can start with 2 and we don't have to increment by one we can increment by 2 okay so it's still going to have the same condition it is going to print it's going to go through here as long as J is less than or equal to 10 let's go ahead and make that 20 this time so as long as J is less than or equal to 20 it's going to go through here but this time it's going to increment J by 2 instead of by 1 and it's going to start it too so let's take a look at that this is just showing you don't always have to start at 1 and end at a specific number and you don't always have to increment by most different things that you can do here okay look at that so 2 4 6 8 10 12 14 16 18 20 2 4 6 8 10 12 14 16 tween you see we're counting by twos this time because we're incrementing by toud well let's let's count by 5's okay let's count by 5's let's start at 0 and let's count by 5's and let's go all the way up to 100 okay download it okay I have 10 15 20 look at that counting by 5's trying to scroll on 5 10 15 20 over there I can hardly go that fast but you see if you look there I'm now counting by 5's so you have a lot of flexibility in what you do inside of this for loop I mean inside of this while loop but basically you put the condition here as long as this condition is true it will continue to execute the Claus Claus starts with the beginning curly bracket and it ends with the ending curly bracket okay so this is a B this is some pretty neat stuff here I'm trying to think if there's anything else I need to show you with the while loop I think you can kind of figure it out from here let me see if I can go back to that original program and this was the program as it sort of was at the end of lesson 6 remember this is our blinking LED program let's run that let's call up our serial monitor okay how many times you want to blink the red LED let's say five how many times you want to blink the yellow LED let's say five and then let's send it and then when we come over here it's going to bring blink the red one five in the yellow one five okay so this is what we did in lesson six but in lesson six we did it with four loops well this time let's do it with a while loop okay so I think there are a couple of things that you need to you need to pay attention to here and so I can keep the clause the same pretty much because my my for loop Clause began here and ended here well I'm going to leave that that that Clause there but then here I'm going to replace the for loop with a while loop so I'm going to say Wow and then I put some condition in there and then it's going to go from here to here well while what well let's say while J is less than or equal to I'm not going to put in 10 because remember the number of links that I want was no red blinks so I'm going to come here and I'm going to say num red blinks and then we should say here this is just a comment but let's keep our comments good so we're going to say start our while with this time okay start our while loop okay now what let's just see I'm going to run this and see what happens while J is less than or equal to num blinks let's see what happens if we try to run this oh it doesn't like this I got an error and it says J was not just declared in this scope okay I can't just come in and start using J if I'm going to use J I've got to declare it and I did not declare it up here at the top so the first time the Arduino ever heard about J was right here and it says what's this J business I don't know J nobody ever told me about J what's he doing in here okay so if we're going to use J we've got to declare it we couldn't declare it up here but again it would make more sense to make it a local variable because we're not really needing to pass it around between things and so we're going to start right here and since it hasn't seen it we've got to declare it J isn't in it yeah J and since we're going to use J here we better give it a value right and so let's give it a value of equal to one and then let's send in a colon so here we say it J is equal to one and so now it is going to be happy right well mmm maybe not okay maybe not let's just see what happens I hope I don't get this thing hung or I can't stop it okay so let's come up here and see how many times you want to blink red I want to blink it 5 how many times you want to blink yellow and I want to bring people up five and then you are on blink one one one one whoa what's going on one one one and look at that that's more than five blinks it just keeps blinking blinking blinking blinking blinking blinking yeah so we have a problem here and what the problem is I said J is equal to one and I said keep blinking as long as J is less than or equal to num blinks and then I just blink it what did I not - I didn't increment J so J stayed at one and so if we went back and looked at the serial monitor is just staying at one and this red LED over here is going to blink forever because there's way for it to get out of this loop if I'm going to do a while loop with a conditional like that I have to be responsible to increment J J is equal to J plus 1 okay and with the semicolon and now let's put that in there it looks like things are going to be happy and then let's come up we're going to say five for the red blinks we're going to say five for the yellow blinks one two one two three four five one two three four five so it looks like it's working now and I have got a little bit of a glitch in there and I am not exactly sure why it's doing that one of the problems that I have is and I mentioned this before that to have the Arduino over here and my computer is over there I've got a really long USB cable and I think maybe that I have got a little glitch coming from that long USB cable but you can see that it's working now that basically it's on me which blink I'm on and it's blinking the right number of times and so I took that same function that I was doing in a for loop but now I've done it with a while loop so what your assignment is your assignment B is to come in and fix this one where it is working with a while loop as well and so you got both of these programs working with a while loop but one of the things I've got to warn you that you've got to be careful of when you come out of this J is going to be equal to what will the last time it on the tenth time through the loop it J is 10 and then at the bottom J goes from 10 to 11 it comes back up here is it that says it's J less than or equal to I mean num blinks let's just say it was 10 no it's 11 now so it's going to jump down here and it's going to be right here and then what you've got to remember is you've got to remember to go back and set J equal to 1 because it's J is equal to 1 here but this is going to ramp it all the way up to 10 or whatever you set number plus one more for the last time through the loop and then you're going to come through here and this one's never going to execute unless you go back and set J equal to one here so always right before you start a loop if you're doing a counter like this make sure that you initialize your counter and a lot of times when I'm helping students with their code on a problem like this they would forget to come down here and I think we forget to come down here and reinitialize there J the only reason I tell you that is I don't want you to get stuck in them if you're not here in my classroom I would not be there to help you figure out what was going on and so I warn you always before you initialize your while loop before you start your while to make sure that you initialize your variable whatever the counter is going to be okay again thank you this is less than seven I look forward to seeing seeing you guys very shortly unless an eighth hope you guys are enjoying this stick with it we're going to get to some more interesting stuff here really soon
Info
Channel: Paul McWhorter
Views: 182,830
Rating: 4.9118237 out of 5
Keywords: arduino, while loops, STEM, High School Science, Lessons, Programming, Engineering
Id: Y64Ev-pWqFs
Channel Id: undefined
Length: 26min 56sec (1616 seconds)
Published: Fri Jun 27 2014
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.