Blinking in code
Section 9: From blocks to C++ codeLesson 9.2 · ⏱ 6 min
delay(500) = wait half a second. Four lines inside loop: on, wait, off, wait.
void setup() {
pinMode(13, OUTPUT);
}
void loop() {
digitalWrite(13, HIGH);
delay(500);
digitalWrite(13, LOW);
delay(500);
}

🖥️ What happens on the stage?
We ran this code in Robo Maker and the challenge passed: “Bravo! It works”.
📌 Another example: fast blinking
Waiting 150 instead of 500.
void setup() {
pinMode(13, OUTPUT);
}
void loop() {
digitalWrite(13, HIGH);
delay(150);
digitalWrite(13, LOW);
delay(150);
}

✍️ Practise
Make the LED blink.
✍️ Practise here in the lesson✖ Close the labor open the lab in a new page ↗
🎯 Challenge yourself
Easy
Slow blinking: a whole second.
👀 Show the solution🙈 Hide the solution
void setup() {
pinMode(13, OUTPUT);
}
void loop() {
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
delay(1000);
}

Result: it blinks slowly.
Medium
Two LEDs blinking in turns (13 and 12).
👀 Show the solution🙈 Hide the solution
void setup() {
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
}
void loop() {
digitalWrite(13, HIGH);
digitalWrite(12, LOW);
delay(400);
digitalWrite(13, LOW);
digitalWrite(12, HIGH);
delay(400);
}

Result: taking turns.
Hard
A traffic light in code: green (11), then yellow (12), then red (13).
👀 Show the solution🙈 Hide the solution
void setup() {
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
}
void loop() {
digitalWrite(11, HIGH);
delay(1200);
digitalWrite(11, LOW);
digitalWrite(12, HIGH);
delay(600);
digitalWrite(12, LOW);
digitalWrite(13, HIGH);
delay(1200);
digitalWrite(13, LOW);
}

Result: the three lights in order.

