Tutorial 12: For Loop Iteration: Arduino Course for Absolute Beginners (ReM)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi I hope you're doing really good I'm glad you're here welcome to this tutorial on for loop iteration now if the last tutorial kind of blew your mind like you know what's going on here this one will surely melt your brain because for for loops are probably the most ubiquitous functions in programming languages in general not just Arduino but programming language programming languages in general for loops are everywhere and they're awesome but you really got to look at them a couple times before they make sense because at first you're scratching your head you know you're you're bashing your head against the wall and it just doesn't you're like what's going on here but I think we're going to work through this so by the end of this tutorial you should have a reasonable grasp on how to use for loops how to implement them so let's go ahead and jump right in so for this tutorial you are going to need an Arduino board now I've got the Arduino Uno here you can use any Arduino or Arduino clone I do however recommend using the original Arduino for these tutorials so we know that we're on the same page you will also need six 220 ohm resistors six LEDs doesn't matter what color one jumper wire and then you're going to need a solderless breadboard you're also going to need a small crystal figurine now I've got a mouse here but a monkey or maybe a chicken would also work fine okay so let's go ahead and set up this circuit so the first thing you're going to do is you take your Arduino board and set it right next to your breadboard and what you'll do is connect a resistor when you're 212 220 ohm resistors one to the number two pin and then you'll connect that to the breadboard and then you'll take the long leg of your light emitting diode and connect it on the breadboard to that other end of the 220 ohm resistor and then you'll connect that to the far ground to strip on the breadboard as you can see in this picture here and then you're going to repeat that from pin two all the way down to pin seven that same setup so you're going to have six resistors and six LEDs and then the final thing you do is take a jumper wire from the ground pin on the Arduino and run it to that ground strip on your breadboard and that's it with the circuit you can go ahead and plug in your Arduino all right with your Arduino IDE open go to file examples control for loop iteration let's take a look at the first block of code it's the comments it's always a good idea to read the comments when you're opening up a new sketch and it's always a good idea to write comments when you're writing a new sketch so this these comments it talks about you know briefly what the circuit does and what the sketch does and what we're doing here is we're going to light multiple LEDs in a sequence and then in Reverse and it's just talk shortly about how to set up the circuit it gives attribution to david melis and tom i go thank you gentlemen for making the sketch lets us know the sketch is in the public domain we can do what we want with it and then also points us to a really handy tutorial on the arduino website which i highly recommend you check out okay so that is the first block of code next block of code is where we declare and initialize variables so here we're declaring an integer it's been named timer and we set it equal to 100 so I wonder what timer variable might do well there's some comments that tell us what it says the higher the number the slower the timing so the point I'm trying to get here is that they have used a descriptive name for this variable so we basically just by reading it we kind of know what it's talking about timer and this is something that you should do with your variables I mean they could have named this variable T 0 1 4gb and maybe that meant something to you know the author who wrote it but it doesn't mean anything to me or you and so it's kind of useless so use descriptive names for your variables you'll thank yourself in the future okay so we move on to the next block of code and that is the setup block of code and this is where we run into our first four statement I'm going to throw in a quick disclaimer here about four statements four statements are probably or four loops are probably about the most complicated programming tool that we're going to talk about in this entire tutorial series okay now that being said we're going to walk through it step by step to make sure that you know we both kind of understand how this is working okay so you might be a little intimidated you might be a little confused but just keep coming back to it and I think you're going to get it okay so keep keep it in perspective if you're having a hard time learning this everybody has a hard time with the for loop the first time through so okay before we even jump into the code I want to talk about a really common analogy used to explain for loops and that is pretend you are a hipster DJ in Los Angeles and you're working at the hottest nightclub okay and you like spin records that's like your gig you like spin the records you play records and so when you're spinning your record you have and there's like let's say there's a couple tracks on the record and there are different points on the record player you know and if we're talking like old-school record player where you have a needle and you set it down on the record and then it plays through and then it stops well when you're doing this you need to know three things first you need to know well what track am I going to start at you know where do I start when I'm going to do this when I play this music am I going to start at one am I going to start at the fifth track I'm going to start at the third track where do I start and then you need to know well hate where am i going to stop what track do I stop at what is the condition I'm going to stop playing this music so the first one was well where do I start and then the next one is knowing a condition where to stop am I going to stop am I going to start at one and stop at five am I going to start at two and stop at four okay and then finally you need to know well which direction are you going to spin the record you're going to spin it forward or backward so maybe I'm going to start - but I'm going to spin it backwards and then I'm going to stop at one okay or maybe I'm going to start at five and I'm going to spin it backwards and I'm going to stop it - okay sounds kind of weird but that's our analogy so three things where do I start what condition do I need to stop at so where do I stop and then finally which direction am I going to play this record so now let's go ahead and go back to our our code here let's take a look at the code and so we've got that first word for and it is orange because it's a key word in Arduino and then we have in us a bunch of stuff inside some parentheses okay now there's three separate statements inside those parentheses and they're each separated by a semicolon the first statement should look kind of familiar it's actually just a declaration and initialization of an integer variable okay and this variable we've named it's been named this pin and it's set equal to two okay so I propose that this first statement is like you being the DJ saying well this is the track that we're going to start at so we're going to start this pin equal to two okay now the next statement is the condition where we're going to say well where do we stop this for loop and here it's kind of like it's kind of like the state the if statement in a it's kind of like the condition in an if statement okay it says this pin less than eight so that condition is where we're going to stop the for loop if that condition is not met the for loop does not get executed okay so here it's this pin well what does this pin here in this case it's two and we're going to say is it is two less than eight yeah it's less than eight so then we would go ahead and execute the code inside the for loop okay and then finally okay and actually so I want to say that this statement right here this is like the DJ saying hey this is the condition I stopped at okay so the first one the DJ says where do I start I started to the next one says where do I stop well I stop if it's not less than eight and then the final thing is which way am i running the record record okay and so here we've got this pin plus plus so what this does is this in ferments the this pin variable so that little plus plus all that means is add one to this pin and the little plus + sign it's not specific to four loops it's actually just a really little piece of handy short code that you can use to add one to a variable so what we could have wrote and writ written in this last statement we could have written this pin equals this pin plus one we could have wrote in that would have been perfectly fine but it's a lot easier just to write plus plus so again when you see plus plus all it means is add one to the variable so what we're doing is we're adding one to this pin conversely we could write this pin - - and what that means is subtract one from this pin so that would just mean this pin equals this pin minus one alright so if you're not confused yet you're like Doogie Howser okay so let's review the three statements again the declaration initialization of the counter variable which is this pin okay that's like where do I start the next statement is what's the condition we're going to stop the for loop at that's this this pin less than eight and then finally this pin plus plus we are increasing this pin so let's say you know we're spinning the record clockwise if it was - - we'd be spinning the record in the opposite direction so what actually happens in a for loop well let's go ahead and run through it let's pretend or the computer program and we're going to run through the first iteration of the for loop so on the computer program I come in I see oh we've got a for loop okay well what's the counter variable oh it's this pin we've set it equal to - okay well what's the condition the condition says this pin has to be less than eight is to less than eight yes two is less than eight so I'm going to go ahead and execute the code inside these curly brackets okay so what is that code well the code is our friendly function pinmode so it sets the mode of a pin or what pin oh hey it's the this pin variable this pin is equal to two so we're going to set the mode of pin two as an output and then I get to the end of my curly brackets well now I'm at the end so what happens at the end of a curly bracket on a four what we do is we go ahead we go back and we check well which way are we spinning the record you know are we adding are we subtracting in this case we're adding so what we do is where the computer and we're going to add one to the this pin variable so this pin it was two but we once we get to the end of that for loop we're going to add one so now it's three so now on the computer program now I go right back up to the top but I'm not worried about initializing and declaring the variable we already did that so I'm just going to go straight to the condition so now this pin what is it equal it's three is three less than 8 yeah 3 is less than 8 hey let's go ahead and execute the code so now I jump into those curly brackets pinmode this pin well what does this pin it's equal to 3 and we've set it as an output now we're at the end of the code and there we go to the curly bracket what do we do at the end of the curly bracket we have to increment the variable so now this pin is equal to 4 it was 3 now it's 4 now we go up to our condition this pin it's 4 is 4 less than 8 yes 4 is definitely less than 8 so now we go ahead and execute our our code again pinmode this pin what is this pin well it's 4 so now the fourth pin on the Arduino the mode is being set as an output and now we get to the end of our curly bracket and again what do we do we add one so now this pin is equal to 5 is 5 less than a it sure is okay so I think you get the gist here so we're constantly going through the for loop if the condition is met we execute the code inside the curly brackets we we do our incrementation okay in this case we're adding 1 and then we again we check the condition so now what happens let's say let's say we just set the pin as 7 so this pin pin mode so pin mode this pin is equal to 7 so we set the 7th pin on the Arduino as an output so now we get to the last one and now we add 1 to 7 so now it's 8 so now let's come up let's check the condition again where the computer so is 8 less than 8 hmm I do not think 8 is less than 8 so what with the for-loop all right see you later for loop we're going to continue on so that is the for loop whoo man I know that's kind of crazy isn't it so you might be thinking man that's a whole lot of work just to set you know the mode of pins like that's a really weird statement to do that but if you think about it we just set the mode of five pins and three lines of code if we wanted to do that just by writing pin mode and then hard coding instead of using a variable this pin if we wanted to do that pin mode to output pin mode three output pin mode four output all the way through pin mode seven output you could see that that would take quite a bit of code and what if what if there was like I don't know 40 things we wanted to do but we just wanted to keep increasing once that is a whole lot of typing and the more typing you do the more errors you're going to make so when we have that so you can set it I think you can start to see the power of a for loop it's allowing us to change to increment in over and over and over again just changing that one variable and doing all of that work in just a small itty little bit of code okay so that's the first for loop in this sketch so now let's move on to the next block of code and that is void loop okay so what's the first thing we run into in void loop oh my it is another for loop man okay so let's go ahead and take a look at this for loop oh wow you know what it looks exactly like the last for loop in fact it's the same thing but what's changed the only thing that's changed is what gets executed inside the for loop so let's just revisit this again okay so what's our counter variable it's this pin it's set equal it to what's the condition this pin less than eight and do we increment yes we increment okay well let's see what's inside the curly bracket so first it says turn the pin on so digital right this pin hi well what is this pin well matters what let's say we're the first time through the for-loop this pin would be equal to two so what does this say this is digital right pin too high so we're applying 5 volts to pin 2 and then what do we do we delay well how long do we delay well we delay for timer what is timer that was if you recall back at the beginning that was 100 so delay 100 milliseconds so digital right this pin that was - we write 5 volts to digital pin - we wait 100 milliseconds and then what do we do well we do a digital right and we say this pin well what is this pin it's - and when we write low voltage so zero volts so what we what have we done in here we who are doing is we blink an LED we digital right high voltage we wait 100 milliseconds and then we digital right low voltage so we've just blinked an LED and what Elly did did we blink well we delete or we blanked the this pin LED which was - so now we get to the end of that curly braket for that for loop and what do we do we add one to the counter variable so this pin it was - now we make it three so now we go back up is 3 less than 8 yes indeed 3 is less than a so now now what happens well now we execute the code digital right this pin well what is this pin well it's three now ok well let's write high voltage to pin 3 let's wait 100 milliseconds and then let's turn off pin 3 ok so we're at high voltage wait 100 milliseconds then right low voltage so now we just blinked pin 2 now we blink pin 3 and guess what happens when we get to the end that's right we increment it now it's going to be pin 4 and guess what's going to happen we're going to blink pin 4 then we're going to blink pin 5 and then we're going to blink 6 all the way through 7 and then what happens at 7 well once we get to 7 we have to add one to the counter variable that would make it 8 and then the condition would no longer be met for the for loop and so we're kicked out of the for loop do you get it so this is taking us all the way from 2 through 7 so digital pin 2 through digital pin 7 all get blinked in just these couple lines of code that's pretty awesome isn't it but hey we want to go back and forth so we went from two to seven now we want to go from seven down back to two so how do we do that let's look at another for loop oh my gosh so many for loops my brain is hurting here so this one is a little different this the DJ says let's start at pin seven okay so this pin is equal to seven and then let's make the condition this pin is greater than or equal to two and then let's spin the record in Reverse so this pin - - again all that means is subtract one from this pin okay so let's think about this okay so let's go the first time through the loop or the computer program so this pin equals seven then we check the condition well okay is seven greater than or equal to two seven is definitely greater than two so let's execute the code so what do we do digital right okay this pin what is this pin it's seven what do we do we write high voltage - pin 7 and what happens we wait a hundred milliseconds and then we write low voltage - pin 7 ok so what have we done we've blinked digital pin 7 okay and then we get to that last curly bracket and what do we do well we subtract one now from this pin so now what is this pin now it's 6 so is 6 greater than or equal to - yes 6 is greater than 2 so let's go ahead and execute the code so now digital write this pin it's 6 so now the LED at pin 6 is going to be written high voltage so it's going to turn on we're going to wait 100 milliseconds and then we're going to digital right pin 6 low okay I think you can see where this is going so now we're going to subtract 1 from 6 now we get to 5 we check the condition is 5 greater than equal to 2 yes it is we execute the code now we blink the LED of pin 5 so can you see what's going here in first for loop we started at pin two so two blanks three blanks for blinks five blinks six blinks and seven blinks and then we leave that for loop and now we come on to this next for loop and what happens guess what seven blinks again then what happens six blinks five links four blinks three blinks two blanks and after two blinks then pin number two blinks that is the condition is going to no longer be met because this pin would be equal to one and one is definitely not greater than two and it's not equal to two so we're going to jump out of that for loop and then once you jump out of that for loop you're at the end of the entire loop function so you would start right back at the top so what have we done we've blinked from two to seven from seven to two and that finishes the loop and then we'd go right back into the loop and we blink from two to seven and seven to two okay that is this program alright so let's take a look at it on here do we know all right let's go ahead verify the code and upload it and you can see it goes from five I'm sorry it goes from two it goes to seven and then it goes back down to two over and back and forth back and forth back and forth okay so I think you can see the power of the for loop here really awesome tool okay you need to master this it's very important in fact you need to master the if statement that we've talked about already and you also need to master this for loop if you really need to wrap your head around it so I highly recommend jumping in to the try it on your own okay challenge do the try it on your own challenge mess around with a lot really wrap your head around this because the sooner the more you understand this statement and the if statement the more power is going to be at your fingertips all right thank you so much for listening and I cannot wait to see you at the next tutorial have a great one you you
Info
Channel: Programming Electronics Academy
Views: 155,954
Rating: 4.9215684 out of 5
Keywords: For Loop, Iteration, Tutorial, Arduino (Computing Platform), Learn, Intro, Introduction, electronics, microcontrollers, arduino course for absolute beginners, challenegs, learning arduino, open source hardware group, serial port. serial monitor, leds, pinMode, digitalWrite, outputs, Arduino (Brand), Lesson, c++, C (Programming Language), Course (Degree), for statement, arduino code, Programming Language (Software Genre), Knight Rider (TV Program)
Id: bMsT8FeMQ-k
Channel Id: undefined
Length: 22min 34sec (1354 seconds)
Published: Thu Oct 17 2013
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.