The For Loop

In this lesson you'll be learning about the "for" command. Full details on this command can be found on page 21 of the Arduino Programming guide. The "for" command is the next iteration of our repetition lessons. It will keep repeating what ever is in the brackets as long as the "condition" is true. You'll need to set up your circuit so that you have an LED (with appropriate resistors) plugged into pins digital pins 8-13.

  for (int ledPin = 13; ledPin >= 8; ledPin = ledPin-1)
    pinMode(ledPin, OUTPUT);
    digitalWrite(ledPin, HIGH);
    digitalWrite(ledPin, LOW);

The "for" process above will light up each pin starting at 13 and counting down to 8 in sequence, turning it off before it lights the next one. You'll notice " int ledPin=13" in the for statement. In this case ledPin only has meaning inside the for loop, this variable is not declared at the beginning of the program.

The basic structure of "for" is as follows:
 Initialize the Process
 Set the Condition
 int ledPin=13
 ledPin >= 8
 ledPin = ledPin - 1

Video Links: On Blip, on YouTube

All for loops will have an integer variable associated with them. I chose to use ledPin as the variable name since we'll be lighting an LED. The process runs as a loop. The condition is checked, if true the code in the brackets is run and then the expression is executed. Then it checks to see if the condition is still true. It functions just like our while loop. In this loop, is ledPin greater than or equal to 8? If true the code is run and Expression are completed. For our loop, the value of ledPin will be decreased by 1 each time through, which will cause the next LED to light up.

When ledPin is decreased to 7 the condition is no longer true and the process stops. Overall, this is very similar to how we used our while loop. You'll rapidly find that there are often many ways to complete the same task. Some of these ways will work better in certain conditions than others, but often it doesn't matter how you do it.

The full sketch is listed below:

 * Sequence Pins
 * This skecth will sequentially set pins 8-13 digital pins to high.
 * It will then set the pin to low and continue to the next pin.
 * It serves as an introduction to the for command.

void setup()                        // run once, when the sketch starts
{                                   // even though there's nothing in it 
}                                   // we still need this process

void loop()                         // run over and over again
  for (int ledPin = 13; ledPin >= 8; ledPin = ledPin-1)   // Declares ledPin,
 {                                  // tests if equal to or greater than 8
                                    // then does the following. It repeats
                                    // but decreases the value of ledPin by 1
    pinMode(ledPin, OUTPUT);        // sets the digital pin as output
    digitalWrite(ledPin, HIGH);     // sets the LED-ledPin on
    delay(100);                     // waits for a tenth of a second
    digitalWrite(ledPin, LOW);      // sets the LED-ledPin off

Additional Tasks:
  1. Once you've gotten this working see if you can add a few more leds.
  2. Make it go there and back again.
  3. Challenge: Light the LEDs at both ends and have them move to the center simultaneously.
One more thing, Incremental Operators
There is another, probably better way, to write the expression "ledPin = ledPin - 1". This is to use incremental operators.

 increases the value of x by 1
 decreases the value of x by 1
 x += y  also increases the value of x by y
 x -= y  also decreases the value of x by y
 x *= y  multiplies x by y
 x /= y  divides x by y

So, if  x=2, x+=5 would set x to be 7. Again if x=2, x*=5 would set x to be 10.

Additional Tasks:
  1. Change your program with one of these operators and see what happens.