While Loop

There are other ways of harnessing the power of computers to complete repetitive tasks. 

while (ledPin>=9)
  pinMode (ledPin, OUTPUT);   digitalWrite (ledPin, HIGH);   delay(100);   digitalWrite (ledPin, LOW);   ledPin=ledPin-1; }

This block of code sits in the void loop. It will be executed as long as the conditions in the parenthesis is true. As the program progresses and we get to the while we ask the question, "Is ledPin greater than or equal to 9?" If yes, then we do the code in the curly brackets. Then we ask the question again, if true we execute the code again. This keeps repeating until the answer is "no". Then the program continues on. Note, whatever variable is being checked must be changed within the while otherwise once the while loop starts it will never end.

Video Links: On Blip on YouTube
This code expands on the power of repetition by introducing while
statements. You'll note there is no code in our setup process the
pins are declared as needed in the loop.

The circuit should have resistor-LEDs on pins 9-13.

created 6 September 2010
by Steve Dickie

int ledPin = 13;

void setup()

void loop()
  while (ledPin>=9)                      //Asks the question is ledPin >= 9 if true
                                                                      //execute the code in the braces.
  {    pinMode (ledPin, OUTPUT); //Set current pin to light-up as an output     digitalWrite (ledPin, HIGH); //Light up the current pin     delay(100); //Wait long enough for us to see it     digitalWrite (ledPin, LOW); //Turn the LED off     ledPin=ledPin-1;               //Advance to the next pin   }
  ledPin = 13;                     //Reset ledPin to start while over

This looks almost identical to the IF Loop we did in the last lesson. The "IF Loop" works as long as its the only thing you're doing in your void loop. However, what if you wanted to do something before or after you sequentially light your LEDs. In this case you could use a While statement. In fact you should be able to use this to make your LED sequencer go back and forth. This is not something you could do with your IF loop.

Comparison Operators:
As you might guess this involves comparing two values to each other. This might be useful when setting conditions for your while. Mostly this is straight forward but here it is anyway:

 x==y  x is equal to y
 x!=y  x is not equal to y
 x>y  x is greater than y
 x>=y  x is greater than or equal to y
 x<y  x is less than y
 x<=y  x is less than or equal to y

The operator that is most important to mention is "x is equal to y". The double equals sign is not a typo. A double equal sign is used to ask the question, does x equal y. If I used a single equals sign "x=y" I would be saying set the value of x to be the value of y. This difference is very important.

Additional Tasks:
    1. What would happen in the code above if I changed while (ledPin>=9) to while (ledPin!=9)? Try it and see.
    2. Make your LEDs go there and back again (hint, you'll need a second while).
    3. Challenge: Light the LEDs at both ends and have them move to the center simultaneously. (hint, this time only one while)