Intermediate 7: While-Loops and Do While-Loops
Here we will learn about what loops are and how to write a while or do while-loop



A loop in programming is a set of code that repeats.  You should be familiar with a loop named void loop() .

Unlike void loop(), while-loops, do while-loops, and for-loops have conditional statements.  These loops only repeat for as long as the conditional statement remains true. In this lesson, we will learn when and how to use while and do while loops.

While Loops

The first loop we learn about is the while-loop.  A while-loop is a very straightforward control structure that repeats a set of code while a certain condition is true.

while() {}

while(condition) {

condition – This is a conditional statement, exactly like the condition of an if-statement.

statement – While the conditional statement is true, then the statement will repeat.  The condition will be checked again after each execution.



Conditional Statements

Conditional statements for while-loops and for-loops are exactly the same as the conditional statements of if-statements.


This statement will repeat as long as the variable x  is less than 10.

This statement will repeat as long as y * 3  is less than 15 and z  is less than 0.

All of these examples are valid and will compile.

While the conditional statement is true, then anything within the while-loop’s brackets will execute.


Example 1: While Loop Counters



x  is incremented by +1 each time the while loop is executed.  When x = 3 , the condition x < 3  is no longer true so the while loop exits and the program continues.

x = 0  is sent and void loop()  repeats.

Compare this to the next example.



What is Read by the Loop

During a loop, the only parts of the program being read are the conditional statement and anything within the loop itself.


Example 2: Bad While Loop Counters



This time we tried moving x = x + 1 outside of the while-loop.

Now the condition for the while loop x < 3  is always true, because x is never updating.   The only lines being read by the program are the condition line x < 3  and the statement line Serial.println(x) .


Using Sensors as Conditions

Since loops only read within the loop and the condition, we have to make sure to include the sensor update code within either the loop or the condition.

A code that uses a stored variable as a condition


will never update the variable bt1 unless explicitly told to within the loop


An alternative is to skip the variable entirely and perform the sensor check inside of the condition


Example 3:  Using a While Loop as an Initiator

For some of your programs (specifically autonomous code), you might not want your program to start running until you send a start signal.   We can use a while-loop to pause your code and wait for input.




An alternative way to do this is to use the variable bt1  instead of digitalRead(11)

It’s important to remember that the variable bt1  in itself is just a number.  It does not update itself.  To ensure that bt1  is continuously updated, the line bt1 = digitalRead(11)  must be included within the while-loop.



Do While – Loops

A Do While-Loop is the same as a while-loop, except that the do while-loop will always execute the statement at least once.

do { } while ();

do {
while (condition) ;

condition – This is a conditional statement, exactly like the condition of an if-statement.  The condition is checked after the statement.

statement – While the conditional statement is true, then the statement will repeat.


In a while-loop, the condition is checked before each statement.

This while-loop would not execute.  The final value for x  is  .


In a do while-loop, the condition is checked after each statement.

This do while-loop would execute once.  The final value for x  is 1 .

We won’t be using do while-loops that often in our programming. Very special cases can use a do while-loop to efficiently manage data space, but we don’t really need to worry about these cases.



While-Loops with CoDrone

Here are a few examples of ways to include while-loops into your CoDrone programs.

Example 4: Stay Code

Here’s an autonomous flight program that uses the sensor bt7  to begin a while-loop that acts as a “stay” command for the CoDrone.

When bt8  is triggered, the StartFlag  initiates a timed flight.  The CoDrone will throttle for 2 seconds, pitch forward for 3 seconds, then land.  

In this example, there is also a while-loop on sensor bt7 .  This while-loop acts as a pause command for the CoDrone, sending the ’empty’ command   CoDrone.Control() .

By sending only CoDrone.Control() , the CoDrone will hover in mid air while bt7  is covered.  After it is released, the CoDrone will resume its flight path.

This is a good way to stop your CoDrone from crashing into something while not stopping the code entirely.  You can pause your code for a few seconds, then release bt7 and see what the rest of your autonomous flight looks like.


If we had used an if-statement instead

Then the rest of the code would still read too, and we would end up sending the CoDrone multiple conflicting movement commands.  Each loop, you would be sending a zero command and a non-zero command.

PITCH = 70;


PITCH = 70;


The CoDrone would behave unpredictably in this case.


Example 5: Quick Color Mode

Another cool way to use while-loops is to create a “quick switch” mode.

You can almost think of a while-loop as a small bubble within your code that contains a whole new program.

This code acts like a normal controller flight program, but while holding the sensor bt8  it switches to a color change mode.

The color change mode uses the left joystick values and switches from either yellow to blue.  You can then release bt8  and resume normal flight.


Activity:  While We’re At It

Create an ‘quick switch’ mode that uses your joysticks to set certain options while covering sensor bt8.

When NOT covering bt8

  • Normal controller flight program
  • Kill switch on bt1

When covering bt8

  • Left joystick controls 4 different color modes.
    • Ex. Up makes arms yellow, Right makes arms blue, Down makes eyes green, Left makes all white
  • Right joystick left-right movement controls Sensitivity
    • To make flight controls more or less sensitive, you can multiply the joystick inputs by a variable.
    • Set your left-right movements to increase and decrease this variable
    • Make sure you set max and min sensitivity limits
    • Limit the max and min values for THROTTLE, PITCH, ROLL, and YAW to 100 and 0

Be creative and try to use include LEDs and Buzzers!