Learn to code

ARDUINO & CODES

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

Create a For Loop statement in Arduino

in Arduino Basics

In this tutorial, we will learn how to use for loops in our sketch. We will use the same circuit as explained in the tutorial: 2.1 Blink a LED.

In our earlier tutorials, we build our first sketch as well as our first circuit. At this time you have learned how a breadboard works, what the digitalWrite commands do and how you can code a simple sketch. Now we are building a little bit more complex sketch. 

What you will need to know?

Before continuing with this guide, you may want to check out any topics from the list below that sound unfamiliar.

Parts you will need

Arduino Uno Rev3 Arduino Uno Rev3 × 1
Breadboard 400 point Breadboard 400 point × 1
220 ohm resistor 220 ohm resistor × 1
Dupont Wires Dupont Wires × 2
LED LED × 1

Arduinoplatform is a participant in several affiliate programs. This means that I will earn a commision if you buy a product from the affiliated websites through clicking on the links provided above.

BreadBoard Layout

The Code

// Sketch: Blinking the led with a for loop
int redPin = 10; // declare pin10 as ledpin
int numBlinks = 5; // variable to store the number of blinks
String LedOnMessage = "Red LED is turned on"; // this is a string with information
String LedOffMessage = "Red LEd is turned off"; // this is a string with information
void setup()
{
  pinMode(redPin, OUTPUT);  // set pin10 as output pin
  digitalWrite(redPin, LOW); // set the pin value on low at the begin
  Serial.begin(9600);
}
void loop()
{
  for (int counter = 1; counter <= numBlinks; counter++) {
    Serial.println(LedOnMessage);
    digitalWrite(10, HIGH);
    delay(1000);
    Serial.println(LedOffMessage);
    digitalWrite(10, LOW);
    delay(1000);
  }
  Serial.print("The Led has blinked:");
  Serial.print(numBlinks);
  Serial.println("times");
  Serial.println(" ");
}

Code Explanation

The above code has much more lines than our first sketch where we blinked only one LED.

If we want to blink the LED five times we can duplicate the code that tells the LED to blink 5 times. However, that will take a lot of coding as you can see below.

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

As you can see in the code above it will take a lot of coding for this simple task. We want to make the red LED blink 5 times the problem occurs that of counting occurs. It is almost impossible to keep track of how many times you wrote the correct line of code to meet the desired outcome.

The for loop can handle this more easily. Let’s go in detail how the for loops and then apply it to our circuit.

Generic Code for loop

for (initialization; condition; increment) {
  //statement(s);
}

“The initialization happens first and exactly once. Each time through the loop, the condition is tested; if it’s, true the statement block and the increment is executed, then the condition is tested again. When the condition becomes, false the loop ends.” Before we are going to apply the for loop on our two LED circuit diagram, we break down the code for an explanation. See the below example off the for loop.

  for (int counter = 1; counter <= numBlinks; counter++) {
    Serial.println(LedOnMessage);
    digitalWrite(10, HIGH);
    delay(1000);
    Serial.println(LedOffMessage);
    digitalWrite(10, LOW);
    delay(1000);
  }

Let us take a look at our new code. Firstly, look at the open-close brackets. This is the code that will be executed in the loop. You can put any code that you would like there. For the initialization part, we are making a local variable. We do not declare at on the top of the program as a global variable since we are only using it for the for loop. We give the local variable the name “counter”. The “int counter” declares that we are using the variable  “counter” as a local variable for the for loop. You can use any letter that you like. Since it is a loop we have to make an endpoint and a start point. That is why we declare “counter” to be 1. We do not start with 0 for counting issues.

Int counter = 1 tells the for loop to start with 1 and continue the loop until it reaches the condition counter =< 5. Remember, we assigned the value of 5 to numBlinks at the top of the program.

In other words, the for loop will repeat the commands until i is equal to 5and then stops. If you would decide to begin with i = 0, the loop will repeat itself for 6 times.

The for loop makes it a lot easier to code/handle things. For further learning, you can add a number to the condition, instead of a global variable, in the for loop. All you have to do change the variable that we used, numBlink, into the number that you prefer and delete the variable numBlink. However, it is wise not to do this but to use global variables.

So, if we want to blink the red LED ten times, it becomes very easy using the for loop. You can try it yourself. But be aware, the for loop is repeating itself. So, the Red LED will blink five times and then the loop starts over again. You can resolve this issue by adding another counter as described below.

Code with counter

The code below will blink the LED 5 times and then stops. We can achieve this by using three counters.

// Sketch: Blinking the led with a for loop and counters
int redPin = 10; // declare pin10 as ledpin
int numBlinks = 5; // variable to store the number of blinks
int counter = 1; // begin with 1 for counting issues
String LedOnMessage = "Red LED is turned on"; // this is a string with information
String LedOffMessage = "Red LEd is turned off"; // this is a string with information
void setup()
{
  pinMode(redPin, OUTPUT);  // set pin10 as output pin
  digitalWrite(redPin, LOW); // set the pin value on low at the begin
  Serial.begin(9600);
}
void loop()
{
  for (int counter2 = counter; counter <= numBlinks; counter++) {
    Serial.println(LedOnMessage);
    digitalWrite(10, HIGH);
    delay(1000);
    Serial.println(LedOffMessage);
    digitalWrite(10, LOW);
    delay(1000);
    Serial.println(counter);
  }
  
  for (int counter3 = counter; counter <= 6; counter++ ) {
  Serial.print("The Led has blinked:");
  Serial.print(numBlinks);
  Serial.println("times");
  Serial.println(" ");
   }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu