stickers_logo_text

Arduino Tutorial 04: For Loop

Hello!!!!!!! guys & welcome back to your 4th Arduino tutorial of arduino tutorial series. Hope, you guys are following our previous arduino blogs. In this blog we are going to discuss about “for” loop and see how for loop simplify your code make it shot and precise. But first we have to see, what is loop? & why we need “for loop”?

So, generally when you write a condition or statement in your program, compiler execute it line by line upto last statement of the code like, if we take an example of led blinking code

 digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
 delay(1000); // wait for a second
 digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
 delay(1000); // wait for a second

As you can see in the above mention code, compiler first compile the digitalWrite(led, HIGH); statement and turn on the led then it will come to the next statement and wait for 1000 ms and the execute the next statement and turn off the led and wait again 1000 ms and compiler repeat the whole process. But what happen if we want to perform this operation on the two led but slightly different way.

Now this time we have two leds and we want to glow these leds in a fix pattern. Consider, you have to blink first led for about 10 times and then you have to blink second led one time. Let see how we can do this

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)
  • 2 x 1KΩ Resistor
  • 1 x USB cable
  • Jumper wires

Connection Diagram

tutorial04_bb

 

Source Code

int redLED=13; //Red led is connect on 13 pin
int greenLED=11; //Green led is connected on 11 pin
int redON=250; //Red led ON for 250ms
int redOFF=250; //Red led OFF for 250ms 
int greenON=250; //green led ON for 250ms 
int greenOFF=250; //green led OFF for 250ms
void setup() {
 // put your setup code here, to run once:
 pinMode(redLED, OUTPUT);
 pinMode(greenLED, OUTPUT);
}
void loop() {
 // put your main code here, to run repeatedly:
digitalWrite(redLED,HIGH); //Turn red LED on
delay(redON); //Leave on for redOnTime
digitalWrite(redLED,LOW); //Turn red LED off
delay(redOFF); //Leave off for redOffTime
digitalWrite(redLED,HIGH); //Turn red LED on
delay(redON); //Leave on for redOnTime
digitalWrite(redLED,LOW); //Turn red LED off
delay(redOFF); //Leave off for redOffTime
digitalWrite(redLED,HIGH); //Turn red LED on
delay(redON); //Leave on for redOnTime
digitalWrite(redLED,LOW); //Turn red LED off
delay(redOFF); //Leave off for redOffTime
digitalWrite(redLED,HIGH); //Turn red LED on
delay(redON); //Leave on for redOnTime
digitalWrite(redLED,LOW); //Turn red LED off
delay(redOFF); //Leave off for redOffTime
digitalWrite(redLED,HIGH); //Turn red LED on
delay(redON); //Leave on for redOnTime
digitalWrite(redLED,LOW); //Turn red LED off
delay(redOFF); //Leave off for redOffTime
digitalWrite(redLED,HIGH); //Turn red LED on
delay(redON); //Leave on for redOnTime
digitalWrite(redLED,LOW); //Turn red LED off
delay(redOFF); //Leave off for redOffTime
digitalWrite(redLED,HIGH); //Turn red LED on
delay(redON); //Leave on for redOnTime
digitalWrite(redLED,LOW); //Turn red LED off
delay(redOFF); //Leave off for redOffTime
digitalWrite(redLED,HIGH); //Turn red LED on
delay(redON); //Leave on for redOnTime
digitalWrite(redLED,LOW); //Turn red LED off
delay(redOFF); //Leave off for redOffTime
digitalWrite(redLED,HIGH); //Turn red LED on
delay(redON); //Leave on for redOnTime
digitalWrite(redLED,LOW); //Turn red LED off
delay(redOFF); //Leave off for redOffTime
digitalWrite(redLED,HIGH); //Turn red LED on
delay(redON); //Leave on for redOnTime
digitalWrite(redLED,LOW); //Turn red LED off
delay(redOFF); //Leave off for redOffTime
digitalWrite(greenLED,HIGH); //Turn green LED on
delay(greenON); //Leave on for greenOnTime
digitalWrite(greenLED,LOW); //Turn green LED off
delay(greenOFF); //Leave off for greenOffTime

}

As you can see in the above mention code red led blink 10 times at a delay of 250ms and after that green led blink one time with the same time delay. Now, compile the code and upload the sketch into arduino uno board. If all goes well after uploading the program into adruino uno, you can see the result on the bread board that red led blink 10 times at a delay of 250ms and after that green led blink one time with the same time delay.

This is very basic example but what happen if you have to blink the red led for 20 times and green led for 10 time. Then this will create a hell lot of problems. In this case, if you follow the above mention way of code writing then you have to write a lengthy code and also it will take a lot of memory space of arduino uno board.

This is the point where you need the “for loop”. Basically, the idea of for loop is developed for such types of problems where you need to run a particular condition multiple number of time. The syntax of for loop is look like this.

  for(var_type var_initialize; condition; var_increment)
 {
    statement--------------;
    statement--------------;
    statement--------------;
 }
for (int j=1; j<=10; j=j++) {
 
//do something cool in this loop
    statement--------------;
    statement--------------;
    statement--------------;
}

As you can see the syntax of for loop, first you have to initialize the variable, then apply condition on it and in the end variable will increment. After that body will be execute and then the whole process will be repeat till the last condition of the loop.

So, let us see how we can solve this problem of led blinking with the help of “for loop”. First, we have to blink red led 20 times and  then green led 10 times. There is no change in the Circuit, here is the code

Source code

int redLED=13;          //Red led is connect on 13 pin
int greenLED=11;        //Green led is connected on 11 pin
int redON=250;          //Red led ON for 250ms
int redOFF=250;         //Red led OFF for 250ms 
int greenON=250;        //green led ON for 250ms 
int greenOFF=250;       //green led OFF for 250ms
void setup() {
 // put your setup code here, to run once:
 pinMode(redLED, OUTPUT);
 pinMode(greenLED, OUTPUT);
}
void loop() {
 for (int i=1; i<=20; i=i+1) {// Start our for loop
 digitalWrite(redLED,HIGH);   //Turn red LED on
 delay(redON);                //Leave on for redOnTime
 digitalWrite(redLED,LOW);    //Turn red LED off
 delay(redOFF);               //Leave off for redOffTime
}
 
 for(int j=1; j<=10; j=j+1){
 digitalWrite(greenLED,HIGH);        //Turn green LED on
 delay(greenON);                     //Leave on for greenOnTime
 digitalWrite(greenLED,LOW);         //Turn green LED off
 delay(greenOFF);                    //Leave off for greenOffTime
}
}

As you can see the above mention code. This code is short and precise and it will save your time and compile faster. Also, it will save your arduino uno memory as well.

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

!!!!!!!!!!!!!!!!!!!!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!