For Loop Iteration

Featured Video Play Icon

There are few functions so useful that you find them everywhere. The ‘for loop’ is an example of this type. A For Loop repeats an action for a specified number of iterations, reducing the lines of code that need to be written thus making the programmers life easier.

In this example we are setting out to make a row of LEDs light up somewhat similar to Kit in Knight Rider.

You Will Need

LED (6)
220 Ohm Resistor (6)
Solder-less Breadboard
Jumper Wires (1)
Red Canary (42)
Step-by-Step Instructions

Connect one side of a resistor into pin 2, connect the other side into a row on the breadboard.
From that row in the breadboard where you attached the resistor, connect the long leg of the LED.
Connect the short leg of the LED to one of the power strip columns on your breadboard.
Now connect a resistor to pin 3, and put the other leg in a row on the breadboard (a different one then your first LED.
Connect an LED in the same manor – make sure the short leg goes in the SAME power strip column as the previous LED.
Repeat this through pin 7.
Using a jumper wire, connect the common power strip to a GND pin on the Arduino.
Connect the Arduino to your computer.
Open up the Arduino IED.
Go to File, Examples, 05.Control, ForLoopIteration
Click the Verify button (Top Left). The button will turn orange and then blue once finished.
Click the Upload button. The button will turn orange and then blue when finished.
Watch in awe as your LEDs turn on and off in sequence.

The first executable code we find is the declaration of the ‘timer’ variable…

1
int timer = 100;           // The higher the number, the slower the timing.

This integer variable with a descriptive name will set the rate between one LED turning on and off. What is so important about variables like these is how easy they allow you to alter your code and how your circuit will run. If the programmer had written the number 100 in a bunch of functions through out this program we would have had the exact same effect, but as soon as we want to speed up the blinking of the LEDs, now we have to go through and change every 100 to a 500 – what a pain – especially when you miss a couple 100’s, and accidentally put a 500 where there shouldn’t have bee one. These variables are like controls into your program – imagine you are on the Star Ship Enterprise, all those buttons and touch screen levers – these are your control variables.

Our next friendly block of code is setup(). In this setup() function we run into our first For Loop.

1
2
3
4
5
6
7
8
9
void setup() {

   for (int thisPin = 2; thisPin LT 8; thisPin++)  {

       pinMode(thisPin, OUTPUT);

    }

}

Let’s consider just what is inside the parenthesis following the for loop..

1
for (int thisPin = 2; thisPin lt 8; thisPin++)

There are three separate statements in the parenthesis separated by a semicolon. The first statement is initialization of the counter variable used in the ‘for loop’ and it looks like any other variable declaration and initialization that you have seen.

1
int thisPin = 2;

The ‘thisPin’ variable is what will be used in the next block of code – or the test.

thisPin lt 8;

This is the test condition that will tell the loop to keep going or to stop. If this condition is TRUE, the code in the curly brackets of the ‘for loop; will be executed again, if the condition is FALSE, the program will stop executing the statement in the ‘for loop’ and move on in the program.

So when we first start this test, the ‘thisPin’ variable equals 2 and the test is..

2 lt 8

We know that 2 is less than 8, so we will execute the code in the curly brackets of the ‘for loop’. The final statement is …

1
thisPin++

I know, looks like a spin on Goggle+, but it’s not. The ‘++’ is a handy incrimination syntax. It means the same thing as “Add 1 to the value of variable thisPin” or, written another way…

thisPin = thisPin + 1 same as thisPin++

Leave a Reply

Your email address will not be published. Required fields are marked *