انتقل للمحتوى
Robo EgyptRobo Egypt
☰ Course contents
Robo Maker: home
0. Lab tour1
1. The start: the Arduino6
2. Wiring and the traffic light4
3. Inputs: the button4
4. Sound and movement5
5. Sensors and numbers5
6. Distance, the screen and colours5
7. Motors and the car4
8. The project: the city's smart gate3
9. From blocks to C++ code5
10. Sensors and screens in code5
11. Smart systems5
12. Bluetooth: control from the phone1

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.

⚙️ An LED blinking every half second
void setup() {
  pinMode(13, OUTPUT);
}

void loop() {
  digitalWrite(13, HIGH);
  delay(500);
  digitalWrite(13, LOW);
  delay(500);
}
The LED blinks
The LED blinks
🖥️ 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.

⚙️ Fast blinking
void setup() {
  pinMode(13, OUTPUT);
}

void loop() {
  digitalWrite(13, HIGH);
  delay(150);
  digitalWrite(13, LOW);
  delay(150);
}
It blinks fast
It blinks fast
✍️ Practise
Make the LED blink.
✍️ Practise here in the lessonor open the lab in a new page ↗

🎯 Challenge yourself

Easy

Slow blinking: a whole second.

👀 Show the solution
⚙️ Solution
void setup() {
  pinMode(13, OUTPUT);
}

void loop() {
  digitalWrite(13, HIGH);
  delay(1000);
  digitalWrite(13, LOW);
  delay(1000);
}
In the lab
In the lab

Result: it blinks slowly.

Medium

Two LEDs blinking in turns (13 and 12).

👀 Show the solution
⚙️ 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);
}
In the lab
In the lab

Result: taking turns.

Hard

A traffic light in code: green (11), then yellow (12), then red (13).

👀 Show the solution
⚙️ 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);
}
In the lab
In the lab

Result: the three lights in order.

Finished the lesson? Mark it so your progress counts.