Arduino – While Loop

While there are a few choices for loops to do while coding in Arduino(Puns intended), you will find a lot of different situations to use each of these loops. We took a look at the for loop in this post, and discovered some of the uses for that. Now we’re going to take a look at the while loop, which is a little different, and I’ll explain to you why and when to use this loop.

while loops

What is it?

The while loop is a block of code that is run while the condition between the parenthesis is true. This is useful when you’re waiting for something, or you have to send a serial command somewhere repeatedly until the desired outcome is returned. It will stay in this loop until the pre-determined requirements are met. This is different from the for loop, because the for loop loops through and increments a variable until it hits a certain number. The while loop can also be written to do this, but that’s not what it’s designed to do.

Uses

One of our examples is going to be a listener, we’ll have the while loop wait until it sees that there is serial data available to read, then it will take action on the serial data that is received.

Another example is a temperature monitor. You can have it sit in the while loop and only do a temperature reading until it changes, then when it changes you can have it send a text message, write to a database, or do any other task imaginable(except split atoms, Arduino is not designed to perform that).

Usage

The following is they syntax from Arduino.cc:

while(condition){
  // statement(s)
}

Let’s break that down, it’s a very simple piece of code, right? In the first line we tell the compiler that we’re using a while statement, by stating “while”. Immediately after that we have a set of parenthesis, inside these parenthesis will be the condition that you are testing. If the condition immediately returns false, your code will never be run, otherwise it runs continuously until it returns false.

You can also use “break” to break out of the loop. This would effectively add a second condition to the while loop, if you use it inside an if statement. For example your while loop tests for sensor a to change, and your if statement tests for sensor b to change. When sensor a changes you have it return false to break out of the loop, when sensor b changes you have it return true to break out of the loop(because an if statement only reads the code inside the curly braces if the condition is true).

 

You can run forever loops by doing while(true). This is not recommended, but it can be done. An example of a forever loop is the “loop()” function that Arduino uses to loop the code you write. This is a loop that is always run, unless you use a break somewhere to remove it from the loop. You can get stuck in this loop indefinitely, so it’s not recommended to use forever loops. Just mentioning it today, so you do know that it’s possible, and that it’s also a risk.

Examples

A great example for this loop, is the post I did about serial communication via the serial monitor. The following is the code from that post:

String data;//initialize string variable

void setup() {
 Serial.begin(9600);//initialize serial
}

void loop() {
 while (Serial.available()){//loop while data is available
 Serial.print("You typed: ");
 data = Serial.readString();//read the data
 Serial.println(data);//print the data to serial monitor
 }
}

If you look through this code, you’ll see in the first line we set up a string named data. In the setup function we instantiate serial at 9600 baud rate. In this case the loop will not run until serial data is available, which would mean that something was sent from the serial monitor to the Arduino. To trigger this, you just have to type something in the serial monitor. Then it does the loop until no more serial data is available, and waits until next time it is available. You can learn more about this process in this post.

Join the newsletter

Subscribe

Subscribe to get our latest content by email.

We won't send you spam. Unsubscribe at any time. Powered by ConvertKit

Leave a Reply

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