Arduino Workshop - Chapter Three - FOR Loops

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] full lips are a different type of lips again and also more sophisticated control over the duration of the loop a 4-leaf allows you to use or create a variable define the condition of the variable that the loop should continue running for and in action when the loop completes that before it starts the iterate again a typical for loop is used when you know how many times you want to live to run and you want to use a variable to keep count for example if you want to do lick to run five times and do something each time then you would structure your loop accordingly let's take a look so in here we can use for so much like if and when we use for then some brackets for our conditions then we can create a local variable here 4 into 5 is usually used as the chatting variable don't ask me why but it is so for in iron we create into eyes and it defaults to zero because we haven't given it a value but we could give it a value here we could say we want I to initialize it 5 for example now we want the loop to run while ever I is less than 5 and this is because into I start 0 the first time is zero index the first time it runs through it will start at zero which is the first time then the one second time to third time for 3 the fourth time and for the fifth time so when it's less than 5 we want to run as soon as it hits 5 we want to break out of the loop because it will have iterated 5 times then what are we going to do every time the loop iterates every time it gets to the end well let's increment is by one of our counter nice curly brackets to put our code in and we can put whatever we want in we could set an LED to turn on or off five times back let's take a look at a bit more of a sophisticated example four loops are really powerful tools in creating logic structures and decision-making programs so let's take a look at how we can use for loops to control the brightness of an LED and print that values back to our serial monitor see what's happening so we've got the current here it's fairly straightforward we start off with our pin definitions I'm using pins three for the led because we're going to be using the analog write function to use the hardware PWM to control the brightness of our LED Rev time is going to be the delay in between increments of the LED sort of control how quickly or how slowly the LED ramps up and down between brightnesses and then we've just setting up pin mode and our serial pins we can print out the value of our counter to the serum owner and see what's happening and you see here I've got two for loops here and this is a very common way to control the brightness of melody if you want it to pulp we're creating an integer variable eyes and we're going to make our full loop run while ever I is less than 256 and the reason to this is because the hardware PWM channels have a resolution of 8-bit or 255 0 to 255 so we don't want to go over to 255 because it's not going to accomplish anything so our less than 256 which is 255 we're going to increment I by 1 and we're going to write the value of I and an analog right to our LED which is going to start at 0 and ramp up increasing the brightness then we're going to wait in between 2 milliseconds each time which is a really short amount of time when you consider that that 2 times 255 that's more milliseconds initially so it's going to take approximately a bit over half a second in order to ramp up completely then we're going to print the value of I and we're doing the same thing in our second for loop but in Reverse we initialize a at 255 instead of 0 meaning a thirtieth full brightness and we want to drop it back down then while I is greater than 0 while it's do well is we're going to decriminalize it down one notch so it's either 255 and work its way down and same thing we write the value of I using analog write delay and then print that to our serial monitor so let's take a look open up serial monitor here problem uploading to the board so actually uploaded it to quickly let's try again because the TX and rx end which used to the serial monitor also use for uploading a program so I open this to room on it as it was and data down there and though the conflict so let's open up the real monitor back up now fantastic so I can see the LED is ramping up and down up and down very cool and you can see the value of I happening in there on the screen it's going up back down up and back down and that is the beauty of that for loop if we were to change a change ramp time to 10 milliseconds so it's going to take five times as long to get to full brightness and five times as long to get to minimum brightness well open our terminal here so you can see that it's incrementing much slower even though the numbers are still changing quite quickly it's going a lot slower to get that brightness and that's physically taking longer to save which is very very cool and that's how you can use foliage to control all kinds of things there the sequencing and LEDs or controlling the brightness using analog wrap functionalities that's so powerful and versatile next up we're going to be taking a look at switch cases
Info
Channel: Core Electronics
Views: 48,915
Rating: 4.9137931 out of 5
Keywords: arduino for loop, how to use for loop arduino, arduino how to use for loop
Id: azBmL9qMK-8
Channel Id: undefined
Length: 5min 8sec (308 seconds)
Published: Tue Mar 07 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.