Arduino Workshop – Chapter Three – FOR Loops

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. Lets 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 dont. Ask me why, but it is so for in iron, we create into eyes, and it defaults to zero, because we havent, 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 its 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 lets 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. Lets. 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 lets 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 whats happening so weve got the Current here, its fairly straightforward, we start off with our pin definitions Im using pins three for the led because were 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 weve, just setting up pin mode and our serial pins, we can print out the value of our counter to the serum owner and see Whats happening – and you see here – Ive got two for loops here, and this is a very common way to control the brightness of melody.

If you want it to pulp, were creating an integer variable eyes and were 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 dont want to go over to 255 because its not going to accomplish anything. So our less than 256, which is 255, were going to increment I by 1 and were 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 were 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 thats more milliseconds, initially so its going to take approximately a bit over half a second in order to ramp up completely then were going to print. The value of I and were 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 its do well is were going to decriminalize it down one notch, so its 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 lets, take a look open up serial, monitor here, problem uploading to the board so actually uploaded it to quickly lets.

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 lets 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 its 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 its 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 its incrementing much slower, even though the numbers are still changing quite quickly, its going a lot slower to get that brightness and thats physically taking longer to save, which is very, very cool and thats.

Share.
Exit mobile version