With what we’ve seen so far, if we wanted to blink an LED five times, we probably wrote this:
// turn LED on and off
digitalWrite(13, HIGH); delay(200); digitalWrite(13, LOW); delay(200);
// turn LED on and off
digitalWrite(13, HIGH); delay(200); digitalWrite(13, LOW); delay(200);
// turn LED on and off
digitalWrite(13, HIGH); delay(200); digitalWrite(13, LOW); delay(200);
// ... and so on until five
It works, yes… But it’s a bit horrible.
- First, because it’s tiring to write (and read).
- Second, because if you suddenly want to change the speed, you have to change it in 20 different places.
- And third, what if you want it to blink 1000 times? Do we write it a thousand times?
Obviously, the answer is no. Precisely one of the great virtues of computers (and Arduino) is that they are excellent at repeating tasks.
To tell our board to repeat something many times, we use Loops. Today we are going to see the two fundamental tools for repeating things: for and while.
The for Loop
The for loop is used when we know exactly how many times we want to repeat something.
Repeat this 10 times Count from 1 to 100 Iterate through pins 2 to 6
The structure of for consists of three parts separated by semicolons:
for (start; condition; update) {
// Code to repeat
}
- Start: We create a counter variable (usually called
i) and give it an initial value. Ex:int i = 0;(We start at 0). - Condition: We ask if we should keep repeating. Ex:
i < 5;(Is i less than 5?). - Update: We change the counter at the end of each iteration. Ex:
i++(Add 1 to i).
Practical Example: Counting Blinks
In the previous example, where we wanted to blink an LED 5 times, we would do this.
for (int i = 0; i < 5; i++) {
digitalWrite(13, HIGH);
delay(200);
digitalWrite(13, LOW);
delay(200);
}
The Arduino will do this. First it creates i = 0. And then it starts repeating
- Is 0 less than 5? YES → Blink and add 1 to
i→iis 1 - Is 1 less than 5? YES → Blink and add 1
- Is 2 less than 5? YES → Blink and add 1
- Is 3 less than 5? YES → Blink and add 1
- Is 4 less than 5? YES → Blink and add 1
- Is 5 less than 5? NO. It exits the loop and continues with the rest of the program.
The while Loop
Sometimes we don’t know how many times we have to repeat something. We only know that we want to do it while a condition is true.
Keep walking while you don’t hit the wall Wait while the button is not pressed
The structure of while is simpler:
while (condition) {
// Code to repeat
}
In for, the variable updates itself (i++). In while, you are responsible for changing the condition at some point inside the loop.
If the condition is always true, the Arduino will get stuck there doing nothing until you reset it (it “hangs”).
Practical Example: Waiting for a Button
// While the button is NOT pressed... do nothing
while (digitalRead(2) == HIGH) {
// We wait...
delay(100);
}
// If we get here, it means someone pressed the button
igniteRocket();
We usually put a small delay because otherwise your Arduino goes crazy repeating the loop at full speed. Since you are already waiting, it’s good to give it a little break.
Practical Project: The Knight Rider Effect
To understand the power of for, let’s make the most famous light effect from the 80s: the front scanner of KITT, the Knight Rider car.
We have 5 LEDs in a row. We want the light to move from left to right and back, creating a trail.
Without loops, we would have to write dozens of digitalWrite. With loops, we’ll do it in a few lines.
The Setup
Connect 5 LEDs (with their resistors, always!) to consecutive digital pins: 2, 3, 4, 5, and 6. Align them on the breadboard so they look nice.
The Code
We will use the loop variable i not only to count, but to select the pin number.
- When
iis 2, we turn on pin 2. - When
iis 3, we turn on pin 3.
void setup() {
// Configure pins 2 to 6 as OUTPUT
// Instead of writing 5 pinMode lines, we use a loop
for (int i = 2; i <= 6; i++) {
pinMode(i, OUTPUT);
}
}
void loop() {
// --- FORWARD: From left to right (Pins 2 to 6) ---
for (int i = 2; i <= 6; i++) {
digitalWrite(i, HIGH); // Turn on the current LED
delay(100); // Wait a little
digitalWrite(i, LOW); // Turn off (to create a moving effect)
}
// --- BACKWARD: From right to left (Pins 5 to 3) ---
// Note: We don't start at 6 or end at 2 to avoid repeating the ends
for (int i = 5; i >= 3; i--) {
digitalWrite(i, HIGH);
delay(100);
digitalWrite(i, LOW);
}
}
for (int i = 2; i <= 6; i++)
- Starts at 2.
- Turns on LED 2, waits, turns off.
- Next iteration:
iis 3. Turns on LED 3… - And so on until 6.
for (int i = 5; i >= 3; i--)
- Here we go backwards. We start at 5.
- The condition is “while it is greater than or equal to 3”.
- The update is
i--(subtract 1). - The effect is that the light “bounces” back.
If you want the trail to be longer (so the previous ones stay on a bit), you can play with the timings or not turn off the LED immediately, but that would complicate the code. To start, this “moving dot” effect is perfect.
