Arduino Tutorial 17: Understanding Arduino While Loops

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is Palma quarter from top tech boy comm and we are here today with lesson number 17 in our most excellent new and improved Arduino tutorials I need you to pour yourself a nice big mug of iced coffee that is strong black coffee poured over ice no sugar none needed straight black coffee over ice it is most refreshing if you become a little parched you're gonna need some of this to get through this lesson what are you also going to need you're gonna need your most excellent ie Lego super starter kit the Uno r3 project kit if you don't have one yet look in the description at the link hook a brother up click on it get one this has a boatload of components it's about $35 it's got the Arduino and the components and all of this series of lessons is based on this and if you get one we're working with the same components ok what are we going to talk about today in lesson number 17 we're gonna talk about while loops ok the last two lessons have been about for loops and anything that you can do with a for loop pretty much you can do it with a while loop and some people like while loops better some people like for loops better and I think it probably more people use the for loop just because they're kind of programmed that way but I find the while loop is I like the while loop a little better because anything you can do with a for loop you can do with a while loop but the while loop to me it works a little bit better when we're kind of working on an Arduino with a project with different things because if you think about the for loop it's sort of it's sort of centered around counting so you say things like start at this number and then go to this number and then increment the number this many times so it's very structured to where the while loop it allows a little more flexibility so you could say something well I want you to loop as long as the temperature is less than 50 and then when the temperature is more than 50 I want you to do something so the while loop to me works a little bit more naturally with the project based nature of the things that we are doing with the Arduino okay let's jump in and get started here and I need to switch over to a more suitable view for you how about this one I kind of like that one this is a new one for us I kind of like it let me get out of your way here and let's look at how we would do a while loop again I'll be using some constants which we normally don't want to do but it just helps me show you how a while loop works okay so let's come in and let's get us a counter so I'll say int and I'll say J and I'm just gonna leave it like that okay and then we are going to need our serial monitor so we are going to say serial dot begin and we are gonna set up baud rate at the trusty ol 9600 okay now here what we can do is we can create a loop here and let's just count to 10 so what I could say is wild ok it turns a happy little green so it recognizes it and then what is the condition that I want to loop under while J is less than or equal to 10 so this is just saying open curly starts the clause and then I hate it when it does this but you see it automatically put the closed curly in and then this curly ends the for loop so while J is less than or equal to 10 and let's just say I'll start here with J is equal to 1 because remember up here we didn't give J a value so here before we loop we got to give it a value remember in the for loop that starting point was kind of put inside the parameters of the for loop will here the only parameter for the while loop is hey man you keep looking as long as J is less than or equal to 10 and then here we would go serial dot l in and what are we gonna print we're gonna print J and then we're gonna go like that okay I want you to just think on your own and kind of tell me what this is gonna do it's is it's gonna it says while J is less than or equal to 10 print J do you have in your mind what it's gonna do okay I have got to switch the view here for you to see the serial monitor I do believe let me get this nonsense out of the way for you okay so let's download this boom that was a pretty easy program we did not get any errors so now let's look at the serial monitor and what is it going to do it just prints one one one one one one and it goes on forever why let's think about it we set J is equal to one we said loop as long as J is less than or equal to 10 and then serial print line J - J - J ever changed J never changed this program runs forever it's an infinite loop it will never stop so what do we need to do inside of here will we better down at the last step say j is equal to j plus 1 so you see now this is starting to look kind of like the for loop remember the for loop had this starting condition as a parameter and it had the increment as a parameter and it was called a for loop well if you just move those those parameters before the while loop and it's the last step of the while loop and then also here I'm just gonna do a serial dot print L print L in of nothing like that so as it goes out of the while loop it'll print a blank line so let's look and see what this does Oh what did I forgot I forgot you forgot to hold your breath right we got to hold our breath when we download the code and I forgot the semicolon but the real reason you didn't hold your breath so let's hold our breath when we compile boom you see it works when we hold our breath now let's come up and look at this and ooh also man you really need to put a delay in here so we're gonna say delay a 500 no no no you use variables so we're gonna come up here int deal a time equals 500 and then we're gonna come down here and put in the lady lay time delay time okay hold your breath boom-boom okay so now let's look at this so what's it going to do one two three four five seven eight nine ten skip a line one two this does like our for loop okay but it's just a little different format and that is a while loop and let me tell you I really like the file loop better because it's a little bit more in line with a little more flexibility I think in a little bit easier for me to think about and so what I want you to do is let me get rid of this nonsense and I'll leave that and I'll leave that okay let's come over and let's build a circuit and what I need you to do is get out your most that's the one you let go kit and then I need to come up with a view that you can see here let's try this one okay so what are you going to need you are going to need your a Lego Arduino Uno r3 you're gonna need a red LED if you prefer you can use the yellow but do not use the blue do not use the green we save those for special occasions okay so you can use the red or the yellow and there's going to be someone that uses the blue and then leaves the comment I use the blue anyway but I be careful I might ban you from the channel for using a blue LED when there is not a special occasion okay we're gonna have the red LED a current limiting resistor and we are going to have the potentiometer you guys we've been through this several times in the earlier lesson so I'm gonna go fast if you cannot keep up then go back and review some of our earlier lessons like using a breadboard and hooking up an LED okay so this we're going to put those two pins on the potentiometer I'm going to start like in column ten and then as I plug this in we straddle we straddle that Center loop okay the the left leg my left my left I'm going to put at pin at 5 volts okay so this we're just going to power up at 5 volts and then the right leg of the potentiometer the right leg of the potentiometer me and this pin doesn't want to go in the right leg of the potentiometer is going to go to GND all right now I want to read the value this lower center pin the lower center pin on the potentiometer I want to read that value so I'm going to go to a zero so now I'm set up to read off the potentiometer now I'm going to go between column 15 and column 20 with my resistor and then the long leg the long leg of the LED goes to the bottom leg of the resistor and now I need to take that to a pen that I'm going to control which is going to be pin 7 I'm just kind of making this up as I go along I am just winging it and then we need to take the short leg of the led the short leg of the LED and we need to take that to a ground all right so now we are set up to read from we're set up to read from the potentiometer and then decide to do something with the LED okay so what are we going to need to do over here at the code we need to get something that you can see what you need to see and I wonder what that would be said okay yeah I think that you can see things there pretty good all right so let's come over here to our code where I will keep I don't think that we need J here we're not going to need J but we're gonna be reading from the potentiometer so we need valve that's the value that we read from the potentiometer when we read from the potentiometer we will read we're applying to it five volts and so depending on the position of the potentiometer that center leg is going to be anywhere between zero and five volts and then that with an analog read will be represented by a number between zero and ten twenty three if we read a 0 that 0 votes volts if we read a 10 23 that's 5 volts and anywhere in between but pot Val will be between 0 and 10 23 all right I got a delay time there I'm gonna speed this up a little bit I'll make it a hundred we do serial dot begin 9600 just in case we need the serial monitor now we come down and make sure you get rid of all this stuff down here alright now what do I want to do I want to read from the potentiometer so I'm going to say hot ah I forget to do I hope you were yelling at me we need to do our pin modes pin mode and I forgot to even set up those variables for the pins ok so I'm going to have an int and I had a I'll call it pot pin because I'm reading from the potentiometer pot pin is equal to a zero and then I've got the led the red LED so I'll say red red pin the red pin is equal to seven and now I need to come down and do my pin mode since I need a pin mode for pot pin and that is going to be a putt pin it's going to be an input heavy little blue colors because we used all uppercase and then I'm going to do a pen mode and then I'm going to do red pin and that is an output in that is going to be good that is going to be good all right I think I've got all that set up so now what do I want to do in my main loop I want to read pot bell so pot Val I read it from I read it with what command analog read what is the one parameter on analog read it's where you read from which would be pot pin okay and then just for fun I will do a serial dot print L in and I will do pot Val I'll print that in case we need to look at it and also I will put a little delay here just because it doesn't so it won't go so fast that we can't see it so I'll say delay and then how long do i delay 500 no no no we use delay time okay now okay so I read the value what do I want to do now I'm gonna say wow while pot valve is greater than 1000 so if I get greater than 1000 I want to issue an error so while pot Val is greater than 1000 that's the condition then I'm gonna open my clause hit enter and then what you know I hate it put this one in for me so it ends that clause so here is my claws while pot Val is greater than 1000 what do I want to do did you toll right and then put pin red pen digital right red pen and high so I'm issuing a warning I'm issuing a warning if the pot Val is greater than a thousand it's like a warning all right now what do I need to do in here I've got to do another pot Val read because if I don't read pot foul again inside the loop it is just going to loop forever it will never come out of that loop because once it gets in it pot foul never changes so now I need to read pot vowel in here is equal to analog read of pot pin and then out of print it out again serial doc grant L in pot about bow and then I need to put a delay in here you see if I didn't put a delay in here when it got in here had run real fast because it wouldn't see this delay here so if it's not in the loop it's gonna print delay print delay if it gets in the loop it's gonna print delay print delay alright and now if I'm not looping if I'm not in that loop that means that pot val is not greater than a thousand what do I want to do I want to digit all right red pin comma low so you see if I'm just inside the loop that means pot val is are greater than a thousand i need to issue my warning light if it is not in the loop that means that pot val is less than a thousand and so I don't want my warning light on does this make sense okay guys I really need you all to hold your breath I need you to hold your breath so that this will compile correctly okay so here we go one two three somebody did not hold their breath okay and also this should have been red pin but the real problem was somebody did not hold their breath whoever it was whoever you were I need you to hold your breath this time hey man I did this more than once huh read ken alright every last one of you hold your breath this time boom ok finally everyone held their breath alright so let's open up the serial monitor and let's see how this looks all right so let me put this where you can see it I believe this will work okay so it's reading 16 okay it's reading 16 so let's see if it's changing yes it is changing and I need to show you also where you can see the circuit do I have a shot that will show the circuit and then need of a shot ah this one this one will work okay so here you can see the LED and you can see the serial monitor alright see see it's at zero the LED is off I will come in and begin to turn it on and you see how we're going up LED is staying off we are really hoping here I got to be careful to not go over a thousand right you don't want to go over a thousand I want to get real close mm what happened boom the red LED came on and it's staying on but that's only half the battle cuz the real trick is if I bring it back yeah the LED is off boom this works exactly like we wanted it are you beginning to see how the while loop is a little bit more natural when we're doing a circuit that has sensors and outputs now the output might be something a lot more interesting like a motor and the input might be something a lot more interesting like a thermometer but you could set up a while loop that says as long as the temperature is less than 70 degrees leave the fan off while the temperature is greater than 70 degrees turn the fan on see see all the sudden the while loop is a little bit more to me a little bit more natural structure to use when we start using like right now again I'm trying to teach you to program and I don't want us to get you know tied in a knot trying to make complicated circuits with a lot of sensors and more complicated actuators we're using these simple things so that you can learn to program and then after we learn to program then we'll come back and we will start looking at some of the more interesting components in the eel eggo kit okay guys this is palma quarter from top tech boy comm hopefully you will give us a thumbs up leave a comment down below man I love to hear back from you sometimes I wonder does anybody actually watch the videos or am I just sitting here talking to the wall okay think about giving us a thumbs up subscribe if you subscribe you need to ring the bell you need to hit that Bell and then you will get notifications when the new videos come up and think about sharing this with your friends and also think about having some coffee okay Palma quarter from top tech boy comm I will talk to you guys
Info
Channel: Paul McWhorter
Views: 95,542
Rating: undefined out of 5
Keywords: Arduino, Tutorial, While Loop, STEM
Id: CITZIv3SmvU
Channel Id: undefined
Length: 21min 5sec (1265 seconds)
Published: Tue Jul 23 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.