What is for loops in Arduino programing??

How for loop conditions works in programming & how to use for-loop works in code are some questions that may arise while programming an Arduino.

Hello guys! Welcome back to my blog today we are going to see about for loop in Arduino.

What are for loops in programming??

for loops is a conditional loop, use to execute some piece of code multiple times until the conditional satisfies. For example, if you want to print numbers 1 to 100 then instead of copy-pasting print statements 100 times, we can use for loop for printing numbers from 1 to 100. now we will see for loop in detail.


Arduino for statement

For loops are the combination of initiation, checking the condition, and increment or decrement alteration. Before knowing about for loop we will take some examples to get more clearance. Suppose you are using your phone and it needs to be charged so you connect the connector and put your phone on charging. Now you will see the battery getting charged in an increment manner example- 7%, 8%, 9%, and get charged until your phone reaches 100%. For loops also works as the same, you start charging from your actual battery percent, which is the initiation of your battery which is not 100% charged. So it continues to charge. For loop also increment iteration until it satisfies the condition.

For more clearance, we will take another example suppose you want to press your clothes. So you turn ON your Iron and wait until your iron gets hot. For that, you first set the desired temperature then the controller inside the iron every time checks & compares the current temperature with the desired temperature that we have set whether its matches or not. When the temperature does not match then it increases the temperature and again checks until the present temperature matches with the desired temperature that is set by you.

Here you can see you initialize your temperature, same as in for loop then the controller checks for temperature, same as in for loop we check for the condition. Then if the temperature which we set and the present temperature are not the same then the controller sends the command to increase the temperature and again compare until the temperature reaches the desired value, same as in for loop we use to increment or decrement iteration until the condition satisfies.

for loop

Now I hope you understand what for-loops does

for loop syntax

Here initiation means to start at what value we are going to start example – i=0, i=1, i=2, or j=0, i=2, etc.

The condition means to check the value whether it’s true or false, equal or not, greater or less than between values. If conditions are not satisfied then increment or decrement the iteration until conditions are matched.

Now here we are initiating value i=0 then we will check the condition if i is less than or equal to 255. If it is less than 255 then it will increment the value i, i will become i=1. Then it will again check & compare the condition value of I will is 1 that is less than 255 again value of i will increment and will become i=2 again it will check and compare if the value of i which is 2 now is less than 255 again it will increment i=3. It will compare & increment until the value of i will be equal to 255. When i=256 which is equal to 255 then for loop will break.

Multiple LED glowing in a sequence using a for loop in Arduino

for loop Arduino example

Arduino for loop LED

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);
 for (int i=0;i<=total_ledpin;i++)
 {
  pinMode(ledpins[i],OUTPUT);
  }

}

void loop() 
{
  
for(int i=0; i<=total_ledpin; i++)
{
Serial.println(ledpins[i]);
digitalWrite(ledpins[i],HIGH);
delay(1000);
}

for(int i=total_ledpin; i>=0; i--)
{
Serial.println(ledpins[i]);
digitalWrite(ledpins[i],LOW);
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);
 for (int i=0;i<=total_ledpin;i++)
 {
  pinMode(ledpins[i],OUTPUT);
  }
}

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 for loop to declare LED pins. We will discuss for loop in detail. In for loop, we initialize the value of i as 0 first and later increment till the value of i reaches the total length of LED which we have declared as 10 (i = 0 to i =10). As we know

ledpins[i],

 for i = 0 ledpins[0] = 2,

 for i = 1 ledpins[1] = 3,

 for i = 2 ledpins[2] = 4,

for i = 3 ledpins[3] = 5,

for i = 4 ledpins[4] = 6,

for i = 5 ledpins[5] = 7,

 for i = 6 ledpins[6] = 8,

for i = 7 ledpins[7] = 9,

 for i = 8 ledpins[8] = 10,

for i = 9 ledpins[9] = 11   

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

void loop() 
{
  
for(int i=0; i<=total_ledpin; i++)
{
Serial.println(ledpins[i]);
digitalWrite(ledpins[i],HIGH);
delay(1000);
}
for(int i=total_ledpin; i>=0; i--)
{
Serial.println(ledpins[i]);
digitalWrite(ledpins[i],LOW);
delay(1000);
}
}

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 i corresponds to the index number of elements inside the array (LED pins). To turn LED ON we will increment the value of i with keyword HIGH inside digitalWrite() and to turn LED OFF we will decrement the value of i with keyword LOW inside digitalWrite().

Conclusion –

Today we will learn about for-loops, for loops in Arduino, what is for loops, and how to use for loops in programming. we have seen an example of for-loops using multiple LEDs. for loops are 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