انتقل للمحتوى
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

The distance sensor in code

Section 10: Sensors and screens in codeLesson 10.2 · ⏱ 8 min

The sensor sends a sound we can't hear and catches its echo. pulseIn measures how long the echo takes to come back, and dividing by 58 gives the distance in centimetres.

⚙️ Print the distance
const int TRIG = 9;
const int ECHO = 10;

void setup() {
  Serial.begin(9600);
  pinMode(TRIG, OUTPUT);
  pinMode(ECHO, INPUT);
}

void loop() {
  digitalWrite(TRIG, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG, LOW);
  long t = pulseIn(ECHO, HIGH);
  int d = t / 58;
  Serial.println(d);
  delay(300);
}
We changed the distance in the sensor's properties and the number changed
We changed the distance in the sensor's properties and the number changed
🖥️ What happens on the stage?
We ran this code in Robo Maker and the challenge passed: “Bravo! It works”.

📌 Another example: near or far

We write a word instead of the number: “near” below 20 cm, otherwise “far”.

⚙️ Near or far
const int TRIG = 9;
const int ECHO = 10;

void setup() {
  Serial.begin(9600);
  pinMode(TRIG, OUTPUT);
  pinMode(ECHO, INPUT);
}

void loop() {
  digitalWrite(TRIG, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG, LOW);
  int d = pulseIn(ECHO, HIGH) / 58;
  if (d < 20) {
    Serial.println("near");
  } else {
    Serial.println("far");
  }
  delay(300);
}
🖥️ Serial Monitor
far
far
far
far
far
far
near
near
near
near
near
✍️ Practise
Print the distance.
✍️ Practise here in the lessonor open the lab in a new page ↗

🎯 Challenge yourself

Easy

Write the distance with “cm”.

👀 Show the solution
⚙️ Solution
const int TRIG = 9;
const int ECHO = 10;

void setup() {
  Serial.begin(9600);
  pinMode(TRIG, OUTPUT);
  pinMode(ECHO, INPUT);
}

void loop() {
  digitalWrite(TRIG, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG, LOW);
  int d = pulseIn(ECHO, HIGH) / 58;
  Serial.print(d);
  Serial.println(" cm");
  delay(300);
}
🖥️ Serial Monitor
37 cm
37 cm
35 cm
35 cm
35 cm
35 cm
35 cm
35 cm

Result: like 35 cm.

Medium

Turn it into metres: write d ÷ 100.0.

👀 Show the solution
⚙️ Solution
const int TRIG = 9;
const int ECHO = 10;

void setup() {
  Serial.begin(9600);
  pinMode(TRIG, OUTPUT);
  pinMode(ECHO, INPUT);
}

void loop() {
  digitalWrite(TRIG, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG, LOW);
  int d = pulseIn(ECHO, HIGH) / 58;
  Serial.print(d / 100.0);
  Serial.println(" m");
  delay(300);
}
🖥️ Serial Monitor
0.37 m
0.37 m
1.50 m
1.50 m
1.50 m
1.50 m
1.50 m

Result: like 1.50 m.

Hard

Three zones: “danger” below 10, “careful” below 30, and “safe”.

👀 Show the solution
⚙️ Solution
const int TRIG = 9;
const int ECHO = 10;

void setup() {
  Serial.begin(9600);
  pinMode(TRIG, OUTPUT);
  pinMode(ECHO, INPUT);
}

void loop() {
  digitalWrite(TRIG, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG, LOW);
  int d = pulseIn(ECHO, HIGH) / 58;
  if (d < 10) {
    Serial.println("danger");
  } else if (d < 30) {
    Serial.println("careful");
  } else {
    Serial.println("safe");
  }
  delay(300);
}
🖥️ Serial Monitor
safe
safe
safe
safe
safe
safe
careful
careful
careful
careful
danger
danger

Result: safe, then careful, then danger.

Finished the lesson? Mark it so your progress counts.