stickers_logo_text

Arduino Tutorial 06: While Loop

Hello!!!!!!!!!!! & welcome back guys. In our previous lesson 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 for loop, it is just a different way of looping. In most scenarios you could choose to loop with either for loop or a while loop. In this lesson we will look in more detail at how while loops work. We will continue to work with the below mention circuit, which you first built for our previous lesson. If you need help putting it together go back and see the previous lesson.

Let’s start

Requirement

  • 1 x Arduino Uno R3 Board
  • 1 x Laptop or pc
  • 1 x Bread board full size
  • 1 x Led(Red,5mm or 3mm)
  • 1 x Led(Green,5mm or 3mm)
  • 1 x 1KΩ Resistor
  • 1 x USB cable
  • Jumper wires

Connection Diagram

tutorial04_bb

 

Source Code

int redLEDPin=13; //Declare redLEDPin an int, and set to pin 9 
int greenLEDPin=11; //Declare greenLEDPin an int, and set to pin 10 
int redOnTime=250; //Declare redOnTime an int, and set to 250 mseconds 
int redOffTime=250; //Declare redOffTime an int, and set to 250 
int greenOnTime=250; //Declare greenOnTime an int, and set to 250
int greenOffTime=250; //Declare greenOffTime an int, and set to 250
int numgreenBlinks; //Number of times to blink green LED
int numRedBlinks; //Number of times to blink red LED
String redMessage="The Red LED is Blinking"; //Declaring a String Variable 
String greenMessage= "The Green LED is Blinking"; //Declaring a String Variable
void setup() {
 Serial.begin(9600); // Turn on the Serial Port
 pinMode(redLEDPin, OUTPUT); // Tell Arduino that redLEDPin is an output pin
 pinMode(greenLEDPin, OUTPUT); //Tell Arduino that greenLEDPin is an output pin
 
 Serial.println("How Many Times Do You Want the Red LED to Blink? "); //Prompt User for Input
 while (Serial.available()==0){ } //Wait for User Input
 numRedBlinks = Serial.parseInt(); //Read User Input
Serial.println("How Many Times Do You Want the Green LED to Blink? "); //Prompt User for Input
 while (Serial.available()==0){ } //Wait for Input
 numgreenBlinks = Serial.parseInt(); //Read User Input
}
void loop() {
Serial.println(redMessage);
 for (int j=1; j<=numRedBlinks; j=j+1) { // Start our for loop
 Serial.print(" You are on Blink #: ");
 Serial.println(j);
 digitalWrite(redLEDPin,HIGH); //Turn red LED on
 delay(redOnTime); //Leave on for redOnTime
 digitalWrite(redLEDPin,LOW); //Turn red LED off
 delay(redOffTime); //Leave off for redOffTime
}
 Serial.println(" ");
 Serial.println(greenMessage);
 for (int j=1; j<=numgreenBlinks; j=j+1) { // Start our for loop
 Serial.print(" You are on Blink #: ");
 Serial.println(j);
 digitalWrite(greenLEDPin,HIGH); //Turn yellow LED on
 delay(greenOnTime); //Leave on for greenOnTime
 digitalWrite(greenLEDPin,LOW); //Turn green LED off
 delay(greenOffTime); //Leave off for greenOffTime
}
Serial.println(" ");
}

You can see in the code above, we are prompting the user for data, then waiting, then entering the data for both numRedBlinks and numgreenBlinks. 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:

int j = 1 ; //declare the variable j to be an int and set value to 1
while (j<=10) { //start your while loop
Serial.println(j); // print the number j
j=j+1; // increment j by 1
} // end of the while loop

 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 green LED. Replace those two for loops with while loops. Lets look at the first for loop again:

 for (int j=1; j<=numRedBlinks; j=j+1) { // Start our for loop
 Serial.print(" You are on Blink #: ");
 Serial.println(j);
 digitalWrite(redLEDPin,HIGH); //Turn red LED on
 delay(redOnTime); //Leave on for redOnTime
 digitalWrite(redLEDPin,LOW); //Turn red LED off
 delay(redOffTime); //Leave off for redOffTime
}

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

int j=1; 
while (j<=numRedBlinks) { // Start our while loop
 Serial.print(" You are on Blink #: ");
 Serial.println(j);
 digitalWrite(redLEDPin,HIGH); //Turn red LED on
 delay(redOnTime); //Leave on for redOnTime
 digitalWrite(redLEDPin,LOW); //Turn red LED off
 delay(redOffTime); //Leave off for redOffTime
 j=j+1; //don't forget to increment your j;
} // this curly bracket ends the while loop

Source Code

int redLEDPin=13; //Declare redLEDPin an int, and set to pin 9 
int greenLEDPin=11; //Declare greenLEDPin an int, and set to pin 10 
int redOnTime=250; //Declare redOnTime an int, and set to 250 mseconds 
int redOffTime=250; //Declare redOffTime an int, and set to 250 
int greenOnTime=250; //Declare greenOnTime an int, and set to 250
int greenOffTime=250; //Declare greenOffTime an int, and set to 250
int numgreenBlinks; //Number of times to blink green LED
int numRedBlinks; //Number of times to blink red LED
String redMessage="The Red LED is Blinking"; //Declaring a String Variable 
String greenMessage= "The Green LED is Blinking"; //Declaring a String Variable
void setup() {
 Serial.begin(9600); // Turn on the Serial Port
 pinMode(redLEDPin, OUTPUT); // Tell Arduino that redLEDPin is an output pin
 pinMode(greenLEDPin, OUTPUT); //Tell Arduino that greenLEDPin is an output pin
 
 Serial.println("How Many Times Do You Want the Red LED to Blink? "); //Prompt User for Input
 while (Serial.available()==0){ } //Wait for User Input
 numRedBlinks = Serial.parseInt(); //Read User Input
Serial.println("How Many Times Do You Want the Green LED to Blink? "); //Prompt User for Input
 while (Serial.available()==0){ } //Wait for Input
 numgreenBlinks = Serial.parseInt(); //Read User Input
}
void loop() {
Serial.println(redMessage);
 int i=1; 
while (i<=numRedBlinks) { // Start our while loop
 Serial.print(" You are on Blink #: ");
 Serial.println(i);
 digitalWrite(redLEDPin,HIGH); //Turn red LED on
 delay(redOnTime); //Leave on for redOnTime
 digitalWrite(redLEDPin,LOW); //Turn red LED off
 delay(redOffTime); //Leave off for redOffTime
 i=i+1; //don't forget to increment your j;
} // this curly bracket ends the while loop
 Serial.println(" ");
 Serial.println(greenMessage);
 int j=1; 
while (j<=numgreenBlinks) { // Start our while loop
 Serial.print(" You are on Blink #: ");
 Serial.println(j);
 digitalWrite(greenLEDPin,HIGH); //Turn red LED on
 delay(greenOnTime); //Leave on for redOnTime
 digitalWrite(greenLEDPin,LOW); //Turn red LED off
 delay(greenOffTime); //Leave off for redOffTime
 j=j+1; //don't forget to increment your j;
} // this curly bracket ends the while loop
Serial.println(" ");
}

Now, burn the sketch in your arduino uno board and you will see the result on the breadboard and arduino serial monitor.

result

!!!!!!!!!!!!!!!!!!!!Congratulations Enjoy!!!!!!!!!!!!!!!!!! 

Hope you guys like our tutorial series. We will see you in the next tutorial with new programming and electronics concepts.

comments powered by Disqus

how can we help you?

Please leave your contact details, Our customer relationship manager will assist you.

get in contact
Be the Campus Ambassador of ELAN IIT Hyderabad!