Arduino Tutorial 15: Understanding Arduino For Loops

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is polemic order from top tech boy calm and we are here today with lesson number 15 in our new or improved Arduino tutorial series so I need you to pour yourself a nice big mug of iced coffee here that ice that's just straight strong black coffee over ice nothing else needed delicious and I need you to get out you're a lego super starter kit for the Uno r3 projects if you don't have this yet check the description down below click the link buy the kit hook a brother up 35 bucks this will keep us busy a long time this is the kit that we are using for this entire series and it will have a boatload of components that will give us all types of different projects that we can build what we're gonna learn today is a new programming command that is very very powerful in the arduino called the for loop okay so let's just jump right into this lesson i mean let me show you what you're gonna need for today's project out of your Lego kit you will need the arduino you will need the breadboard you will need a yellow LED in a red LED do not use the blue LED or the green LED those are for special occasions only for normal programming like today we will use the common the yellow and the red LED we save those other colors for special occasions and then you will need two 330 ohm resistors now i'm gonna give you a little assignment here and then i want you to pause the video and go hook it up yourself because you know enough to do this so this is your assignment i want you to hook up the yellow LED to pin six of course through the current current limiting resistor i want you to hook up the red LED to pin nine through another current limiting resistor and then i want you to blink the yellow LED three times and then the red LED five times so it should go yellow yellow yellow and then red red red red bread okay I'm not sure if I counted right there it's actually kind of hard to count yourself saying red if you've never tried it okay that's what your assignment is pause the video go do it and then when you come back I'll kind of do it with you all right guys I'm gonna go kind of fast because you really ought to already know how to do this and so we're just gonna we're gonna hook this circuit up kind of fast so we're gonna start with our current limiting resistor I'm going to put that between I'm going to put that between columns 10 and 15 and then I'm going to come in with my yellow LED my yellow LED knowing that the long leg needs to go towards the resistor because that's going towards the power supply so we're going to have that okay and then I need to connect the top of the resistor that column 15 I need to go over to pin 6 okay and then I need to come off the bottom leg of the diode the bottom leg of the diode the short leg of the diode and come over to G and D and I'm using the G and D that is by pin 13 so I should have one LED hooked up now the next LED I'm going to put the current limiting resistor between 20 and 20 and column 25 and again it doesn't matter exactly which one you use as long as you get the connections now I'm going to put the long leg of the LED in column 25 so now it is connected to the bottom leg of the current limiting resistor and now I need to bring my signal in so I'll go to column 20 that is the top leg of the LED I need to bring that to pin 9 and now I need to ground could complete the circuit by coming to the short leg of the led the red LED and now I'm going to use one of these ground pins if you look up here by it goes V in and then you got GND G and D you can use one of those all right kind of neaten things up here and now we are going to come over and we're gonna start writing our codes so I need to find a little bit better view let's see what this one is I think this weren't you can see everything that you need to see here if I use this view so let's come over here and let's start writing some code well how many pins do we have we're using two pins and so we need to set up our variables for that so I'm going to call it I'm going to start with yellow yellow pen and that's going to be equal to what ten six and then I'm gonna do an int four red pin and that's equal to what nine that's connected to pin nine now I need you know I'm gonna be blinking this LED and you know like on the yellow one in the red when I said that you want about a half second on half second off so it's blink blink like that so I need to have like yellow time and that is going to be five hundred because that would be five hundred milliseconds half a second then I'll need int I don't like to do it that way yellow time and then I try to be consistent in the way that I do capitalization like that everything's lowercase except the first letter of subsequent words and so that's also going to be 500 well you're saying if those are both 500 fine and you just use one variable well because maybe I would want to change them at some point so I'm leaving flexibility for the future all right so I've got yellow pen I've got red pen I've got yellow time I've got red Tom I think I am ready to go here so I am going to be using two pens so in the void setup I need to what do my pin modes and the first pin is going to be yellow yellow pin and that is a bought output turns blue happy little color and modes and then this is red pin it is a what output all right I always like to turn on the serial monitor at serial dot begin 9600 in case later I need to print something out I can already have my serial monitor running okay now I need to do some blinks let's start with the yellow if I want to blink the yellow what do I need to do first thing I need to do is turn it on so digit all right happy little orange color what am i turning on yellow pin and what do I want it to be high so I'm turning the yellow LED on now I need to delay how long yellow time let me just make sure I did that right yellow time okay and then that's one parameter yellow time now what do I need to do if I'm going to blink I need to turn it off digit all right yellow pin low and now the LEI yellow okay hopefully I haven't made any mistakes there if I have you're probably screaming at me and you're gonna scream at me even more because what's the quickest way now to blink yellow three times I'm going to take this I'm going to control C and then I'm going to come here so that blinks once now I'm going to control V that's going to blink twice and now I'm going to paste again control V okay control V so this should blink the yellow LED three times now I want to blink the red LED so I'm going to come and I'm gonna put pace this blink for yellow one more time and then I'm going to change this to red so this is going to be red red red red now I have three yellow blinks I've got one red blink I'll come in ctrl C to copy that and then control V that's two red blinks controlled three three red blinks control the four red blinks control the five red blinks okay so this should be three red blinks in five three yellow blinks and five red blinks you know the good thing about copying and pasting it goes really fast you know the bad thing if I made a mistake I just pasted it and pasted it and pasted it and you were probably if there was a mistake you were probably screaming at me but let's look and see what happens we come over here and we are going to download it now hold your breath you know I don't know if you guys do this but when I compile I hold my breath and I think that if I hold my breath the whole time it increases the chance that there's not an error okay here we go Oh what did I do wrong pin modes oh did I say pin modes instead of pin mode I said do my PIN modes and then I type in mode and I didn't look okay let's try it again I think a few of you are not holding your breath and that's why this didn't work everyone this time hold your breath boom you see everyone held their breath it worked now let's look over here oh man we have some action we have some action let's watch yellow LED one two three red LED one two three four five boom you know what the problem was who was it out there that didn't hold their breath that's why it didn't work the first time okay so we have this working and what you've gotta see is this works but it is a crazy wrong way of doing it there is a much better way of doing this and it is called a four loop so let me get rid of everything but one yellow blink and one red blink okay so I'm just taking out all this repetitive stuff what I want and only get these together [Music] so what is it I want to do this block of code three three times and then I want to do this block of code five times so what I want to do is I want to create a clause here starting there and ending here so now this little chunk of code is all in a clause and then I want to tell it to do this a certain number of times and it is called a for loop so I'm going to say for and now I've got to go define some more variables so to set it up between these parenthesis I put the parameters telling it how to loop but I've got to get some more variables so first thing that I need is I need to tell it how many times to blink the yellow LED so I'm going to say yellow blink yellow blink equals what yellow was going to be three okay now int when I have red blink because I'll have a second for loop for the red red blink is equal to what five okay now I just need a counter because you've got to have a counter if you want the loop to loop five times you have to have a counter and that's a variable typically the variables for counting and loops are either I or J the problem with I is I sometimes looks like one and if you're trying to debug your code and you've got nine one mixed up it can be really hard to find and so I like J cuz it's a character that looks very different also like the most horrendous problem to try to resolve is if you ever get a one in an L mixed up you see that's one that's L they look almost exactly the same and you will actually go insane and they will institutionalize you because you will never be able to find that problem if you do it so I try to not use ELLs and is variable so I'm gonna say int J I'm not going to put a value in J I'm just going to declare it because we will put a value in down here alright so now we need to come and so we've got this clause this clause in curly to start curly now we've got to put the parameters in here between the parentheses that tell it how to blink well what's my counter J where am I going to start well I'm going to start with J equal 1 so I'm counting from 1 and now I put the semicolon and now I'm saying I start with J equal 1 and now I want to loop as long as J is less than or equal to yellow blink and then each time through I want to make J is equal to J plus 1 so I increment by 1 so this keeps looping as long as J is less than or equal to yellow blinks so let's see the first time through J is equal to 1 1 is less than or equal to yellow blinks so it goes through first time when it gets through what does it do it makes J 2 so it comes up this time J is 2 2 is less than or equal to yellow blink so it goes through the loop a second time when it gets to the end it makes J equal J plus 1 now J is 3 it comes up here this time J is 3 well 3 is less than or equal to yellow blinks so it goes through a third time now J is for it comes up is J less than or equal to yellow blink no it is not it then jumps down here and then continues executing below if I just said J equal 1 J is less than or less than equal blink instead of less than or equal to it would go through 2 times not three times but if you say less than or equal to then it will go through the three times which yellow blink is and so if you just set things up like this it should work so it will do the things between the open curly and the closed curly it will do those things yellow blink tops which is three so now we need to make another for loop for red blink for J equal 1 to J is less than or equal to red a blink I don't use a comma that's not right semicolon that's one of those problems that's really hard to find if you haven't done a for loop in a while is it very easy to put a comma there so now red blink and now J equal J plus 1 and now open curly that's where the clause starts that's where the clause stock also look the four statements are like if statements you don't put a semicolon at the end of them okay is this going to work I sure hope so I need you to cooperate what do I need you to do I need you to hold your breath when we do this all of you because if you don't we're gonna get an error boom we got it okay let's see what happens over here we have some action let's watch yellow is gonna be O what is going on here houston we have a problem houston we have a problem it's blinking forever on red blink for J is equal to one ah I didn't put did you guys see this I didn't go to Yeats and J man I hope you guys were yelling at me someone didn't hold their breath someone didn't hold their breath okay let's try again okay and I'm just going to hit the reset button here yellow one two three red one two three four five boom this thing is working and you guys know how to do a four loop now do you see how powerful a for loop is also let me show you now imagine before if I come up and said okay I'm sorry I really wanted you to blink the yellow yellow LED five times in the red blink three times I'm sorry I messed up you'd have to go in and completely rewrite the code but here what do I do I just come up and say okay no problem it's cool we can fix it download it and boom two three four five and then one two three do you see how powerful this is man do you see how powerful this is so you guys have learned a lot already you know to how to do digital rights you know how to do analog rights you know how to do analog reads you know how to do if statements and you know how to do for loops and man that is a very very powerful combination very very powerful combination and so you can really start doing a lot of stuff with that what do we have coming up all right we're gonna there's a couple more things that we need to learn before we start doing a whole lot of different components right I know we've just kind of been piddling with potentiometers and LEDs and I know you're anxious to get to more projects get to some of the more exciting things in this kit but we've really got to get these basics down first and I like to not spend a lot of time having to hook the circuit up so simple LEDs and resistors allow us to do this learning really quick there is a lot more that you can do with four loops this was sort of an introduction to four loops and so I'm going to do another video to show you some of the some of the tricks some of the pro moves you can do in using your four loops there's really a lot of exciting things you can do as you start playing with these parameters I'll be looking at that then what I will do is there's another type of loop that it's called a while loop and almost anything that you can do with a for loop you can do with a while loop and sometimes it's easier to use a while loop sometimes a for loop so I'm going to show you how to do a while loop so we're going to do another for loop video then we're going to do a while loop video and then I'm going to show you how to get data from the serial monitor remember right now we're just printing to the serial monitor and the next in the in that follow unless I'm going to show you how to get data from the from the serial monitor okay guys this has been a really exciting video I hope you guys will think about giving us a thumbs up think about subscribing to the channel when you subscribe to the channel make sure you ring that bill click that bill and then you'll get notifications when I have new videos come out and then also think about sharing this with your friends if you have questions I love to hear comments below I like to see that there are people that are actually watching these videos and I'm not just sitting here talking to myself palma quarter from top tech boy calm I will talk to you guys later
Info
Channel: Paul McWhorter
Views: 107,305
Rating: undefined out of 5
Keywords: Arduino, Tutorial, For Loops
Id: OdHzRbR0xHQ
Channel Id: undefined
Length: 20min 40sec (1240 seconds)
Published: Wed Jul 17 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.