while loop and do-while loop in Arduino programing

What do a while-loop and do-while loop do in programming or how to use a while-loop and do-while loop do in programming are some questions that may arise while programming an Arduino.

Hello guys! Welcome back to my blog today we are going to see about the while & do-while loop in Arduino programming.

while loop Arduino

While-loop is the same as the for-loop, the result and the operation are similar to the for-loop but the difference is the length of the lines.

While loop

int i                       

i=0;                      => initialization                             

while (i<5)           => checking conditions                            

{

Print (i);                 => task execution                          

i++;                       => Incrementing / decrementing                            

}

In the while loop first, we initialize the value of i and assign a value to it (i=0), inside the while loop we check the condition if the value of i is less than 5, if the condition satisfies then we will print the value of i and we will increment it, previously the value of i was 0 after incrementing it will change to 1. now again it will check in if condition whether value if i is less than 5 if condition satisfies then we will print the value of i and increment it. this process is repeated until the condition is not satisfied. if the condition is not satisfied then the code will jump out of the while loop and the remaining code will be executed.

We will take a small example of a while loop by glowing an led

Arduino while loop LED

Controlling LEDs with while loops


LED controlling using while loop


Connect the negative terminal of all LEDs to the GND pin of the Arduino
Connect positive pin of LED 1 to PIN no 2 of Arduino
Connect positive pin of LED 2 to PIN no 3 of Arduino
Connect positive pin of LED 3 to PIN no 4 of Arduino
Connect positive pin of LED 4 to PIN no 5 of Arduino
Connect the positive pin of LED 5 to PIN no 6 of Arduino
Connect positive pin of LED 6 to PIN no 7 of Arduino
Connect positive pin of LED 7 to PIN no 8 of Arduino
Connect positive pin of LED 8 to PIN no 9 of Arduino
Connect positive pin of LED 9 to PIN no 10 of Arduino
Connect the positive pin of LED 10 to PIN no 11 of Arduino

Code - 

int ledpins[] = {2,3,4,5,6,7,8,9,10,11};
int total_ledpin = 10;

void setup() 
{
   Serial.begin(115200);
   int k=0;
    while (k<total_ledpin)                                   
    {
    pinMode(ledpins[k],OUTPUT);                                      
    k++;                      
    }
}

void loop() {
  int i=0;                                               
while (i<=10)                                   
{
digitalWrite(ledpins[i],HIGH);                                         
i++;   
delay(1000);                    
}

int j=10;
while (j>=0)                                   
{
digitalWrite(ledpins[j],LOW);                                        
j--;   
delay(1000);                    
}

}

Code Explanation - 

int ledpins[] = {2,3,4,5,6,7,8,9,10,11};
int total_ledpin = 10;

First, we have created an array that consists of pins of Arduino pins from pin no 1 to 10. On each pin, we have connected a led. So in total, we have 10 LEDs. Here we have created an array and assigned a name of the array as ledpins. Here we are using a total of 10 pins of Arduino to connect 10 LEDs. We are connecting LED to Arduino pin numbers 2, 3, 4, 5, 6, 7, 8, 9, 10, 11. We have declared the total LED pin count as 10 as in this example we are using 10 LED.

void setup() 
{
   Serial.begin(115200);
   int k=0;
    while (k<total_ledpin)                                   
    {
    pinMode(ledpins[k],OUTPUT);                                      
    k++;                      
    }
}

In the void setup, we have configured the baud rate to 115200. We configure pins as INPUT or OUTPUT. We have to declare the LED pin as output. Here we have 10 LEDs so instead of declaring each 10 LED pin as output. We will use a while loop to declare LED pins. In the while loop, we initialize the value of k as 0 first and later increment till the value of k reaches the total length of LED which we have declared as 10 (i = 0 to i =10). As we know

ledpins[i],

 for k = 0 ledpins[0] = 2,

 for k = 1 ledpins[1] = 3,

 for k= 2 ledpins[2] = 4,

for k = 3 ledpins[3] = 5,

for k = 4 ledpins[4] = 6,

for k = 5 ledpins[5] = 7,

 for k = 6 ledpins[6] = 8,

for k = 7 ledpins[7] = 9,

 for k = 8 ledpins[8] = 10,

for k = 9 ledpins[9] = 11   

So using while loop we have declared each LED pin of Arduino as OUTPUT

In the void loop, we continuously turn all LEDs ON in a sequential manner and turn OFF LEDs in opposite direction. As above in void setup, as we use for loop to declare all LEDS pins as OUTPUT, the same we will do in the void loop to turn LED ON and turn LED OFF using the digitalWrite() function. The value of k corresponds to the index number of elements inside the array (LED pins). To turn LED ON we will increment the value of k with keyword HIGH inside digitalWrite() and to turn LED OFF we will decrement the value of k with keyword LOW inside digitalWrite().

do-while

In the do-while loop here the first operation is executed then the condition is checked at the end, but the output result of the while-loop and do-while loop is the same.

int i;

int i=5;           =>  initialization                             

do

{

Print(i);         =>  task execution                          

i++;              =>  Incrementing / decrementing                            

}

while (i<5);    =>  checking conditions                

In the do-while loop first, we initialize the value of i and assign a value to it (i=0), inside the do condition we will print the value of i that is 0 then we will increment the value of i. we will increment it, previously the value of i was 0 after incrementing it will change to 1 and again it will print the value of i (i=1). it will continue to increment the value of i till condition i less than 5 is satisfied given in the while loop. from this example, we are clear that in the do-while loop first operation is performed and then the condition is checked.         

Led blinking example using do-while loop

int ledpins[] = {2,3,4,5,6,7,8,9,10,11};
int total_ledpin = 10;

void setup() 
{
   Serial.begin(115200);
   int k=0;
    while (k<total_ledpin)                                   
    {
    pinMode(ledpins[k],OUTPUT);                                      
    k++;                      
    }
}

void loop() 
{
int x = 0;
do {
  delay(1000);          
  digitalWrite(ledpins[x],HIGH);
  Serial.print(x);
  x++;
} while (x <= 10);

int y = 10;
do {
  delay(1000);          
  digitalWrite(ledpins[y],LOW);
  Serial.print(y);
  y--;
} while (y >=0);

}

First, we have created an array that consists of pins of Arduino pins from pin no 1 to 10. On each pin, we have connected a led. So in total, we have 10 LEDs. Here we have created an array and assigned a name of the array as ledpins. Here we are using a total of 10 pins of Arduino to connect 10 LEDs. We are connecting LED to Arduino pin numbers 2, 3, 4, 5, 6, 7, 8, 9, 10, 11. We have declared the total LED pin count as 10 as in this example we are using 10 LED.

In the void setup, we have configured the baud rate to 115200. We configure pins as INPUT or OUTPUT. We have to declare the LED pin as output. Here we have 10 LEDs so instead of declaring each 10 LED pin as output. We will use a while loop to declare LED pins. In the while loop, we initialize the value of k as 0 first and later increment till the value of k reaches the total length of LED which we have declared as 10 (i = 0 to i =10). As we know

ledpins[i],

In the void loop, we continuously turn all LEDs ON in a sequential manner and turn OFF LEDs in opposite direction. As above in void setup, as we use for loop to declare all LEDS pins as OUTPUT, the same we will do in the void loop to turn LED ON and turn LED OFF using the digitalWrite() function. this example is the same as the while loop but the only difference is in do while loop first we will execute the operation then we will check the condition means LEDs will turn ON first, then increment the value, and later condition is checked. similarly, for turning LED OFF we only decrement the value.

While-loop is an entry control loop whereas do-while is an exit control loop.

Conclusion –

Today we will learn about While-loop, While-loop in Arduino, what is While-loop, and how to use While-loop in programming. we have seen an example of a While-loop using multiple LEDs. While-loop is most commonly used in each and every programing language.

"I hope you find this IoT blog very helpful to you. In the upcoming lesson, we will see about for loop till then bye. See you all in my next blog."  

Close Menu