Arduino Lesson 9 - For Loops

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi guys today we're going to be doing four loops which is a way of condensing code or it can also be used to fade up and down analog write values so I'm going to go over those two things today we're gonna start with the code condensing aspect of a for loop so I've got an LED right up to my Arduino on pin 13 and I want that led to flash a certain number of times so we're gonna start by by writing that in in the way we normally would so we're gonna do in LED and I've got a white pin 13 string up at 13 there go down to the void setup pin mode LED and obviously it's an output because we're writing to it and then down here in the void loop we're gonna do a number of digital rights so we want it to flash five times so this statement here is turning the LED on and off once so if we were using normal code to get it to flash on and off five times we'd have to copy this five times so two three four five so this this now would be the code we have to write if we wanted to flash our LED on and off five times and then at the end we're gonna do a long delay of five seconds so let's see what that looks like when we upload it to the to the Arduino so we can see that our LED is flashing five times and then there's a five-second delay and then it will loop back to the start the void loop and all over again this is a fairly long piece of code to do something very small so we can actually condense this using a for loop so we're going to start writing our for loop up here so you start off for you by saying for then you have brackets and then you have the square brackets and everything you want to do is contained within these square brackets so anything you want to loop a certain amount of times you put within here so in this instance we want to digital right the in high and then low so I'm gonna copy and paste that in here anything it up a bit and we can get rid of the other four copies and pastes of that code because we don't need that anymore because as you'll see our for loop is gonna run this piece of code five times which means it's the same as listening this code five times so as you can see it's saving a lot of space so the for loop works on what's pasted inside these brackets so I've switched a pen and paper because it's a little bit easier to explain this if I can use different colored pens so we've got our for loop in our code written like that then we open our brackets and we have some code in here which I haven't gone over yet that's what I'm about to go over then we close our brackets we open our square brackets and we close our square brackets down here so whatever's in between this square bracket and this square bracket so whatever we put here in our code is what we're going to loop a certain amount of times the code we put in here is specifying how many times we're running it so in this instance we've put the digitalwrite of iled high and then the digitalwrite of our LED low which would flush at once and here we're going to write code to turn that into five flashes by looping the code five times so first off when you see four loops written normally you will normally see the value or the character I and so you don't have to use the character I you can use you can use whatever you want as long as it's the same in each part of the for loop so for it can be split into three parts which we'll go over in a minute so as long as this is the same in each three parts it doesn't matter what you call it it's just an algebraic character or word there's used to track where the four loops are so for example if you've tracked if you've looped two times it's it's we save that to the value I which allows us to track how many times we've run it and how many times we've got left to run it so that might make more sense when I explain it in the code so I'm going to use the green pen to draw I so you can sort of see that it's included in all three parts of the for loop so let's start with the first part the Foley we're gonna say I is equal to zero and that there is the first part of the for loop so all that saying is we're starting the for loop at zero which is fairly logical really if you're going to start counting you don't we start at zero and there are some exceptions to that which we'll go over later then we do a state the second part of the for loop is a and by the way we disconnect each part of the for loop from each other by these semicolons so that's one part that's the first part and then here's the semicolon now this second part of the for loop here is a true or false statement if what we say here is true then we are going to run what's between these brackets so I are going to turn our LED on and off in our case if this statement is false then we skip from the end of the start the for loop to the end of the for loop and we continue doing what we were doing and we just ignore whatever is in here so we're gonna write a statement that says I is less than and we want to run the code five times so we're gonna say five there and then we're going to use our semicolon to end that middle section of our for loop so what we're saying here is if the algebraic number I is less than five then we're gonna run this code okay and the last part of the statement is I plus plus which just means every time we run the loop we add one this means add one what it means is if we've run through this a few times we start I equals zero is zero less than five yes it is so we this statements true which means we're gonna run the loop before we run the loop we add 1 to the value of I so now I is 1 we've run the loop once is 1 less than 5 yes it is so we add another one now I is 2 we run the loop is 2 less than 5 yes it is at 1 now 3 it's 3 less than 5 yes it is now I want for is for less than five years it is at one now five we come back round it's five less than five no it's not that statements now false we skip from here to here we ignore the code in here and then we just continue running the code below what we've done is we've written a statement that allows us to loop however many times we specify so the number here if we changed it to 20 then this would love loop 20 times if we changed it to 3 it would loop three times so now we can skip back to our code and fill out our for loop in here one thing I have to mention is we were using I as a kind of an algebraic tracking number if you will and it's important that we set up at the top that I is a number so int I means that we're telling the computer that I is an integer which is what int stands for if we didn't do this then we'd gain error that said I was not declared in the scope and the reason we'd get that is because the computer wouldn't know that I is a number so when we try and use it in the for loop to track how many times we've run the loop it wouldn't know what is so that's why we have to write in I up the top so now I know I've said that we can start writing our for loop so we say I equals zero and then semicolon because we want to start at zero then we say if I is less than and then we're going to say how many times we want the code to loop so we want it to look five times so I'm going to say if I less than five then semicolon and then we're gonna say five plus plus which means add 1 to the value of I every time we loop so now what's going to happen is let's just meet this up a bit so what's gonna happen is our codes gonna run down here we're gonna come to our for loop we're gonna set is 0 is 0 less than 5 yes it is so the statements true which means we're gonna run this code before we run the code we add 1 to the value of I so now I is 1 we run the code is 1 so when I say we've run the code we turn the LED on and off so we've done the LED on or off once so far is 1 less than 5 yes it is so now we add another one too the value of I so now I is equal to two we run the loop again is two less than five yes it is add one two now is three loop again is 3 less than five yes it is at one now it's four run again is four less than five at one yes it is sad one loop again is five less than five no it's not so now that statement is false so we skip from here to here we ignore the code in the middle and we just continue to run our code which in this case it's just a five second delay then we get to the end of the for loop sorry end of the avoid loop skip to the start the void loop and run it again so what we should see is exactly the same as we had before where the LED turns on and off five times because we specified five here and then there's a five-second delay and that should happen again and again because we're in the void loop so let's upload that and see if we see the same thing as we did before with the really long code so I've actually slowed the delay down to 500 milliseconds in the for loop just so we can see it happening more clearly so we've got one two three four five LED flashes then a five-second delay once the five-second delay is over we'll go back to the start of the void loop and we'll see our LED flash again five times and that is thanks to our for loop and because we've got I equals five here we're gonna live five times so if we change this number to let's say ten and got here and upload the code then we should say it one two three four five six seven eight nine ten and then we don't have that five-second delay and there you go so that's how you use a for loop to control how many times things loop in code and we can use that to greatly condense our code so as you saw at the start we had to list this five times whereas now we've just got one load of line of code that's running this ten times which is much more efficient next up we're going to use for loops and analog writes to try and create this fading led effect don't forget because we're using the post width modulation during the analog right we have to connect our signal line for the LED up to one of the pwm pins ie pins 3 5 6 9 10 or 11 on the Arduino Uno so I've got my LED hooked up to ground and pin 11 so the best way to do this is probably just to do it and talk you through it as I do it so we're gonna start by setting up the LED which remember is wide to one of the PWM pins on the Arduino so I've wired it to pin 11 myself in the pin mode the LED is an output because we're writing to it and now we're in the for loop so we're going to use a foot two for loops one to fade the led to its brightest at 5 volts and one to fade it from 5 volts back down to zero volts and other words off so we're gonna start by writing the for loop to fade it from off up to 5 volts so we're gonna say 4 oh before that before we do this sorry don't forget at the top we have to say in I otherwise it's gonna say I is not declared in this scope so we're gonna say for I equals 0 because we're gonna start at 0 then we're gonna say if I is less than 255 and the reason we're saying 255 is because that is the max analog right value for an analog right which you'll understand in a second because in the square brackets of this for loop we're going to use an analog right to create a voltage that goes to the LED which is what is effectively fading the brightness of the LED so I'm gonna say 255 there and then we're gonna say I plus plus we're then going to close the for loop and open its square brackets inside the square brackets the for loop we're gonna say now in the last one we were doing digital right in this one I'm going to do analog right and then I'm going to say we're gonna analog way to the LED and then now normally I'd put a number between 0 and 255 but in this case we're going to write I so it's gonna replace I with whichever value it's currently saved I as in the for loop so I'll run you through that and it will probably make more sense we're also going to add a small delay here so it doesn't happen too quickly so let's run through this and see see how it works so we start the for loop I is equal to 0 is 0 less than 255 yes it is so now add 1 to the value of I so now I is 1 then we do the we run the loop so analog right LED and remember I is equal to 1 at the moment so we analog writing it's the same as writing 1 there so now we add now we have the delay and we run background is 1 less than 255 yes it is so we add 1 now I is 2 we've run the loop so it's the same here as writing 2 we run the loop again is 2 less than 255 yes it is so we add one now is 3 and it's the same as writing 3 there so you can see how this is going to affect our analog right and effectively what it's doing is fading the value of analog right from 0 to 255 by putting the variable I in here so if you didn't quite understand that maybe take a look back at and a lot my video I did on analog rights but effectively all we're doing is passing the the algebraic number we're using to track the for loop into an analog right so it's a little bit of a clever thing to do but it's really useful and it allowed allows us to fade analog right to values so we can do things like fade LED brightnesses or we can turn motors on slowly instead of immediately on so and there's a lot of different things we can use it for and it's quite useful so now we've what this is doing is its fading from 0 to the max analog right value so to 5 volts so it's turning the LED from all the way off to all the way on now we want to fade from all the way on back to all the way off and this is one of the times where you wouldn't start the loop at 0 because obviously if we start the loop at zero then we're going to start by writing zero volts and if we want to fade from the max downwards we're not going to start by writing zero so let's start another for loop to fade back down again so gonna say for I equals and here we're gonna write the max value of an analog right so that's 255 because we want to fade from the max brightness to the minimum brightness so we're gonna say and there and now in the second part of the for loop the statement that has to be either true or false if it's true we run the loop if it's false we don't run the loop we're gonna say I is more than zero so now if I is bigger than zero we're gonna run the loop now we're gonna say I and they said of saying plus plus like we normally would we're gonna say - - and then we're gonna close our for loop and then do exactly the same as we did before we're gonna open our square brackets we're going to do an analog right to the LED and we're going to write the value of I semicolons with a lot of code and then we're gonna delay twenty milliseconds so we're going to run through this second for loop and see how it's working so we start at 255 is 255 greater than zero yes it is so that statement there is true which means we're going to run the for loop before we run the for loop we take away one from the value of I so minus minus means take away one same as plus plus means add one so now I is 254 so we're analog writing to the LED a value of 254 then we loop back round is 254 more than zero yes it is take away one now it's 253 analog right to the LED a value of 253 go again until eventually we get to one and then we go is one more than zero yes it is take away one so I is now equal to 0 we analog way to the LED a value of zero which is basically zero volts so we're turning the LED off completely the run background once more is zero less is zero more than zero no it's not so we've we've got statements now false so we skip to the end of the forum and then continue to run our odhh so if we run this code we'd see the LED fade from all the way off to all the way on and then from all the way on back to all the way off again [Music]
Info
Channel: Benduino
Views: 33,678
Rating: 4.8989601 out of 5
Keywords:
Id: u1riERgDYU0
Channel Id: undefined
Length: 18min 10sec (1090 seconds)
Published: Sun Dec 11 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.