Paul McWhorter arduino lesson 8
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 gon na 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.
Paul McWhorter arduino lesson 8 Video
Paul McWhorter arduino lesson 8 news
Posted on Friday August 19, 2016Books You Should Read: Basic Electronics Hackaday … Continue Reading »
Paul McWhorter arduino lesson 8 Social