In our recent PMRC RadioLabs session, we explored the basic Blink sketch, where an LED turned on and off at a fixed interval. Now, let’s take it further by experimenting with different timing patterns using the FOR loop.
In Arduino programming, loops help us repeat tasks, making our code more efficient. Instead of writing the same lines of code multiple times, we use a FOR loop to handle repetition automatically.
Understanding setup() and loop()
Every Arduino program consists of two main functions:
setup() – Runs Once
This function runs only once when the Arduino starts. It’s where we initialize settings, such as telling the Arduino which pins to use for inputs and outputs.
void setup() {
pinMode(13, OUTPUT); // Set pin 13 as an output
}
loop() – Runs Continuously
The loop() function repeats forever, allowing our Arduino to react and respond indefinitely.
void loop() {
// This is where we write code that repeats
}
Wiring the Circuit
Since we are using an external LED, we need to connect it properly to the Arduino.
Wiring Components:
Component | Arduino Pin |
---|---|
220Ω Resistor | Between Pin 13 and LED (long leg +) |
LED (long leg + / anode) | Connected to the resistor |
LED (short leg – / cathode) | Connected to GND (Ground) |
The 220Ω resistor limits the current flowing through the LED, preventing damage.
Understanding the FOR Loop
A FOR loop repeats a block of code a set number of times. Here’s how it’s structured:
for (initialization; condition; increment) {
// Code to repeat
}
Part | What It Does |
---|---|
initialization | Sets a starting value (e.g., int i = 0;). |
condition | The loop keeps running while this is true (e.g., i < 3). |
increment | Changes the variable each loop (i++ increases i by 1). |
Now, let’s use FOR loops to create different LED timing patterns.
Blinking “SOS” in Morse Code
One of the most famous timing patterns is SOS in Morse Code (… — …). Using a FOR loop, we can blink the LED in a structured and efficient way.
const int ledPin = 13;
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop() {
// Short blinks (S) - 3 times
for (int i = 0; i < 3; i++) {
digitalWrite(ledPin, HIGH); // LED ON
delay(200); // Short blink
digitalWrite(ledPin, LOW); // LED OFF
delay(200);
}
delay(600); // Pause between "S" and "O"
// Long blinks (O) - 3 times
for (int i = 0; i < 3; i++) {
digitalWrite(ledPin, HIGH);
delay(600); // Long blink
digitalWrite(ledPin, LOW);
delay(200);
}
delay(600); // Pause between "O" and "S"
// Short blinks (S) - 3 times again
for (int i = 0; i < 3; i++) {
digitalWrite(ledPin, HIGH);
delay(200);
digitalWrite(ledPin, LOW);
delay(200);
}
delay(2000); // Pause before repeating
}
Understanding the Code
pinMode(pin, mode) – Configures the LED pin
pinMode(ledPin, OUTPUT);
- This tells the Arduino that pin 13 is an output, meaning it will send power to the LED.
digitalWrite(pin, value) – Turns the LED On/Off
digitalWrite(ledPin, HIGH); // Turns LED ON
digitalWrite(ledPin, LOW); // Turns LED OFF
- HIGH means power is sent to the LED, turning it on.
- LOW means no power, turning the LED off.
delay(milliseconds) – Pauses the Code
delay(200); // Waits 200 milliseconds
- This pauses the program so the LED stays on/off for the given time.
FOR Loop – Repeating the Blink Pattern
The first FOR loop blinks the LED 3 times for the “S” in SOS:
for (int i = 0; i < 3; i++) {
- Starts at i = 0
- Runs while i < 3 (three times: i = 0, 1, 2)
- Increases i by 1 each loop (i++)
The same loop structure is used for the long “O” blinks, but with a longer delay.
Expanding the Idea – Challenge Yourself!
Now that we have the basic SOS blink pattern, here are a few challenges you can try on your own:
- Modify the pattern – Try blinking “HELLO” or your Callsign in Morse code!
- Speed Variation – Modify the delays inside the loop to gradually increase or decrease the blink speed.
- Double Blink Patterns – Instead of three blinks, try five short and five long for a new effect.
What’s Next?
Now that we’ve seen how FOR loops can be used for timing patterns, next time, we’ll explore potentiometers!