In this section, you'll look at 'for' loops which you can use to control the flow of your program.

Transcript

Fall loops are a different type of loop again and offer more sophisticated control over the duration of the loop. A fall loop allows you to use or create a variable, define the condition of the variable that the loop should continue running for, and an action when the loop completes but before it starts to iterate again. A typical fall loop is used when you know how many times you want the loop to run and you want to use a variable to keep count. For example, if you wanted your loop to run 5 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 for int i. I is usually used as the counting variable, don't ask me why but it is.

So for int I and we create int I and it defaults to 0 because we haven't given it a value but we could give it a value here so we could say we want I to initialise it 5 for example. Now we want the loop to run while ever I is less than 5 and this is because int I starts at 0. So the first time it's a 0 index, the first time it runs through it will start at 0 which is the first time, then a 1, second time, 2, third time, 4, 3 the fourth time and 4 the fifth time. So when it's less than 5 we want it to run and as soon as it hits 5 we want it 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 I by 1 as our counter. Some 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 5 times perhaps but let's take a look at a bit more of a sophisticated example.

For 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 value back to our serial monitor so we can see what's happening. So we've got the code here and it's fairly straightforward. We start off with our pin definitions and I'm using pin 3 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. Ramp time is going to be the delay in between increments of the LED so it'll control how quickly or how slowly the LED ramps up and down between brightnesses. And then we're just setting up pin mode and our serial pin so we can print out the value of our counter to the serial monitor and see what's happening.

And you see here I've got 2 for loops here and this is a very common way to control the brightness of an LED if you want it to pulse. We're creating an integer variable I and we're going to make our for loop run while ever I is less than 256 and the reason for this is because the hardware PWM channels have a resolution of 8 bits or 255, 0 to 255. So we don't want I to go over 255 because it's not going to accomplish anything. So while it's less than 256, which is 255, we're going to increment I by 1 and we're going to write the value of I as an analog write to our LED which is going to start at 0 and ramp up increasing the brightness. Then we're going to wait in between for 2 milliseconds each time which is a really short amount of time but when you consider that that's 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 initialise I at 255 instead of 0 meaning it's already at full brightness and we want to drop it back down. Then while I is greater than 0, while it's still valid, we're going to decrement I. We're going to take it down one notch. So we'll start at 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 the serial monitor here. Problem uploading to the board so I actually uploaded it too quickly. Let's try again. Because the TX and RX pins which you use for the serial monitor are also used for uploading your program. So I opened the serial monitor as I was trying to send data down there and there was a conflict.

So let's open our serial 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 there on the screen. It's going up, back down, up and back down and that is the beauty of our for loop. If we were to change, let's 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. We'll 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 to that brightness and it's physically taking longer to fade, which is very, very cool. And that's how you can use for loops to control all kinds of things. Be it sequencing your LEDs or controlling the brightness using analog rack functionalities. They're so powerful and versatile. Next up, we're going to be taking a look at switch cases.

Feedback

Please continue if you would like to leave feedback for any of these topics:

  • Website features/issues
  • Content errors/improvements
  • Missing products/categories
  • Product assignments to categories
  • Search results relevance

For all other inquiries (orders status, stock levels, etc), please contact our support team for quick assistance.

Note: click continue and a draft email will be opened to edit. If you don't have an email client on your device, then send a message via the chat icon on the bottom left of our website.

Makers love reviews as much as you do, please follow this link to review the products you have purchased.