Learn to code

Arduino Home

Lessons 1
Arduino - Getting Started
Lessons 2
Arduino - Basics
Lessons 3
Arduino - Serial Communication
Lessons 4
Arduino - Digital / Analog
Lessons 5
Arduino - Visual Output
Lessons 6
Arduino - Motor Control
Lessons 7
Arduino -LCD Displays
Lessons 8
Arduino -LCD Displays

Create a For Loop statement in Arduino

in Arduino Basics

Often you would like to iterate over a series of pins and do something to each one. The for statement is used to repeat a block of statements enclosed in curly brackets. Most of the time an increment counter is used to increment and terminate the loop. You can use the for statement for any repetitive task.

The example below we will write uses the for loop to simplify our code for blinking 3 LEDs with an Arduino Uno. First, we will look at what a for loop is.

For loop()

a for loop consists of three parts: initialization, conditional test, and iteration. Each part is separated by a semicolon.

A for loop could use an existing variable declared at the beginning of the sketch or it can create a variable that is used inside the loop. There are three things you need to do in the condition of a for loop:

  • initialize the variable that you’re using to count the number of times through the loop
  • set an ending condition
  • set an incrementing operation
for (initialization; condition; increment) {
//statement(s);
}
}

The for statement in Arduino allows you to execute code lines at a specified number of times repeatedly. The for loop is based on a counter and the counter itself can be used as a variable inside the for loop. What that means is that you can use the counting numbers as a variable inside the for loop.

The declaration part is only run once. The conditional statement is using a comparative operator and is tested each time through the loop. If the condition is true, the code inside the curly brackets is executed.

Let’s say we wanted to blink an LED five times quickly, we could use a for loop similar to the following:

Below is a version of a for loop with the variable used that is defined outside the for loop.

void setup() {

Serial.begin(9600); // begin serial communication
}

void loop() {
 
 int j;
 
 for(j=0; j < 4; j++ ) // for loop 
 {
 Serial.println(j);
 }

} // closing the void loop()

Since the initialization of the variable is already defined we do not nee dan datatype like int in the initialization part of the for loop. The output of this for loop is

0

1

2

3

With the conditional test, you can stop the for loop. The previous example test whether the variable j is less than 4 and will terminate the loop if the condition is false.

The following code will test if the variable is less than or equal to 4. And it will use a variable inside the for loop. A bit different than the code above. It will also print the numbers on the serial monitor.  However, instead of the numbers till 3, it will print the numbers 0 till 4.

Serial.println("for(int i=0; i <= 4; i++)");

for (int i = 0; i <= 4; i++)
{
  Serial.println(i);
}

The third part of the loop is the iterator statement that gets executed at the end of each pass through the for loop(). The following example increases the value of i by two on each pass. There is also a small delay added to the code.

The above expression will print the values of i with 0, 2, and 4.

Serial.println("for(int i=0; i < 4; i+=2)");
  for (int i = 0; i <= 4; i += 2) // increment with 2 in the for loop()
  {
    Serial.println(i);
    delay(1000);
  }

Example Blink Four Times

The example below will blink your LED three times and then stops blinking.

Breadboard Layout

Breadboard layout: you can see in the breadboard layout above that one LED is attached to pin 10. We choose the 220-ohm resistor for practical reasons, you can read more about this at resistance tutorial

Remember to connect the cathode side (longest leg op the LED) to the positive wire from the pin and the anode side tot he ground wire.

The Code

If you apply the above code on the sketch made in BLINK an LED the following sketch is derived.

/*
    Arduino Sketch that blinks 3 times with a for loop
*/

int pinLed = 10; // LED is attached to pin 10
int i = 0;

void setup()
{
  // set pin 10 as an output pin
  pinMode(pinLed, OUTPUT);

  // turn the LED off at beginning
  digitalWrite(pinLed, LOW);

  //start serial connection
  Serial.begin(9600);
}

void loop()
{

  for (i ; i < 3; i++ )
  {
    // turn the LED on
    digitalWrite(pinLed, HIGH);
    // wait for 1 second
    delay(1000);
    // turn the LED off
    digitalWrite(pinLed, LOW);
    // wait for 1 second
    delay(1000);

  } // closing the for loop()
} // closing the void loop()

Code Explanation

Since most of the code is explained in https://www.arduinoplatform.com/arduino-basics/blink-a-led-with-arduino/ we will focus on the code of the for loop().

The for loop below loops three times through the code. That is because we declared an integer variable called i. If i is less than three the code between the curly brackets is executed. Furthermore, if i is three the code is not executed and everything in the for loop() is ignored.

The variable is declared at the top of the sketch, in this way can access the variable in the loop and the counter will not reset.

The for loop() makes it easier to repeat some code, as you can see. In the code above, we declare an integer variable called I and assign the value 0 to it. This is done at the top of the sketch before the void setup(). The reason for this is the variable will not reset in the sketch.

Int i = 0;

Since most of the code is explained in https://www.arduinoplatform.com/arduino-basics/blink-a-led-with-arduino/ we will focus on the code of the for loop().

The for loop below loops three times through the code. That is because we declared an integer variable called i. If i is less than three the code between the curly brackets is executed. Furthermore, if i is three the code is not executed and everything in the for loop() is ignored.

 for (i ; i < 3; i++ )
  {
    // turn the LED on
    digitalWrite(pinLed, HIGH);
    // wait for 1 second
    delay(1000);
    // turn the LED off
    digitalWrite(pinLed, LOW);
    // wait for 1 second
    delay(1000);

  } // closing the for loop()

Each time through the loop, the conditional statement will check if the condition is true. As long as i is smaller than 3 the enclosed statement is executed.

Int i < 3;

Each time through the loop, after all statements between the curly brackets are executed, the variable I increases with 1.

i++;

We are using a compound operator i++ which is the same as i = i + 1.

The variable starts with 0 the first time in the loop, incrementing 3 times until the value is less than 3 before ending the for loop().

If you declared the variable inside the for loop()  as described below variable will be reseted every time the void loop() is started. So the for loop below will blink the LED continiously, that is not wat we want in this case. Remember, the code below has a variable inside the for loop(). This means that the variable i can only be accessed inside the for loop().

  for (int i = 0; i <= 3; i++ ) // incorrect for loop()
  {
    // turn the LED on
    digitalWrite(pinLed, HIGH);
    // wait for 1 second
    delay(1000);
    // turn the LED off
    digitalWrite(pinLed, LOW);
    // wait for 1 second
    delay(1000);
  } // closing the for loop()

The next lesson will give more details about arrays which are very useful in combination with a for loop().

Previous Post
Understanding Arduino operators
Next Post
Understanding If / Else statement in Arduino

1 Comment. Leave new

You must be logged in to post a comment.
Menu