Arduino Lesson 7: Using While Loops

In Lesson 3 we learned how to use for loops. For loops are very powerful and they are sufficient to take care of most of your looping needs. However, there is another type of loop called a ‘while loop’.  The while loop is not better than a for loop, it is just a different way of looping. In most scenarios you could choose to loop with either a for loop or a while loop. In lesson 6 we briefly introduced while loops, and used one to pause the program to wain on user input. In this lesson we will look in more detail at how while loops work. We will continue to work with this circuit, which you first built in lesson 3. If you need help putting it together go back and see Lesson 3.

LED Schematic
This circuit will allow you to independently control two Light Emitting Diodes from the arduino microcontroller

Also, we will continue to expand the code we developed in Lesson 6. At the end of Lesson 6, our code looked something like this:

You can see in the code above, we are prompting the user for data, then waiting, then entering the data for both numRedBlinks and numYellowBlinks. This allows the user to set the number of times the LED’s blink from the serial monitor. The way we actually do the blinking is with the two for loops. There is another way to loop, and that is with ‘while loops’. When you are writing code, often times you can choose a for loop or a while loop. Some people frequently use both types of loops and some have a preference for one or the other and almost always use their preferred type of loop. It is good to know how to use both types, so here we will explore the while loop.

Lets look at how one might work:

 Lets unpack this code and see how it works. First, notice that the while loop will have one or more commands that it executes. It will execute the commands that are between the curly brackets.  It will continue to execute the commands between the curly brackets as long as the ‘condition’ described in the parenthesis of the while loop is true. In the case above, it will continue to execute the commands between the curly brackets as long as j is less than or equal to ten. notice that inside the loop we are incrementing j by 1 each time through the loop. This ensures that it will eventually exit the loop. If we were to forget to increment j, the program would become hung in the loop. This would create what is called an infinite loop, and the program would become hung in the infinite loop.

So, for the code above, we start by making j=1, then each time through the loop we increment j by 1. The program will continue executing this loop until the condition is no longer true . . . for this case, at the point that j=11 the condition would no longer be true, the program would not enter the curly bracket, and would jump to the first line of code after the closing curly bracket.

So, for this lesson what we want to do is replace the for loops in the original code shown at the top of this lesson with while loops. There is one for loop that blinks the red LED and one for loop that blinks the yellow LED. Replace those two for loops with while loops. Lets look at the first for loop again:

Lets do the same function with a while loop. We could replace this code with: