انتقل للمحتوى
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 car's parking sensor

Section 11: Smart systemsLesson 11.2 · ⏱ 8 min

If the distance is below 20 cm the buzzer rings, otherwise it is silent. tone(8, 1000) is a sound, and noTone(8) is silence.

⚙️ A bell when something comes close
const int TRIG = 9;
const int ECHO = 10;

void setup() {
  pinMode(TRIG, OUTPUT);
  pinMode(ECHO, INPUT);
  pinMode(8, OUTPUT);
}

void loop() {
  digitalWrite(TRIG, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG, LOW);
  int d = pulseIn(ECHO, HIGH) / 58;
  if (d < 20) {
    tone(8, 1000);
  } else {
    noTone(8);
  }
  delay(100);
}
Far: the buzzer is silent
Far: the buzzer is silent
Close: the buzzer rings
Close: the buzzer rings
🖥️ What happens on the stage?
We tested it in Robo Maker: the buzzer rang at 10 cm and went quiet when we moved away to 50 cm. The challenge passed.

📌 Another example: faster beeping the closer you get

The waiting time comes from map: close = a short wait.

⚙️ Faster beeping the closer you get
const int TRIG = 9;
const int ECHO = 10;

void setup() {
  pinMode(TRIG, OUTPUT);
  pinMode(ECHO, INPUT);
  pinMode(8, OUTPUT);
}

void loop() {
  digitalWrite(TRIG, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG, LOW);
  int d = pulseIn(ECHO, HIGH) / 58;
  if (d < 50) {
    int t = map(d, 0, 50, 50, 500);
    tone(8, 1000);
    delay(100);
    noTone(8);
    delay(t);
  }
}
Very close: fast beeping
Very close: fast beeping
✍️ Practise
Make the parking sensor.
✍️ Practise here in the lessonor open the lab in a new page ↗

🎯 Challenge yourself

Easy

A continuous beep if it comes closer than 10 cm.

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

void setup() {
  pinMode(TRIG, OUTPUT);
  pinMode(ECHO, INPUT);
  pinMode(8, OUTPUT);
}

void loop() {
  digitalWrite(TRIG, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG, LOW);
  int d = pulseIn(ECHO, HIGH) / 58;
  if (d < 10) {
    tone(8, 1500);
  } else {
    noTone(8);
  }
  delay(100);
}
In the lab
In the lab

Result: silent when far, continuous when close.

Medium

A higher note the closer the thing gets (map for the frequency).

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

void setup() {
  pinMode(TRIG, OUTPUT);
  pinMode(ECHO, INPUT);
  pinMode(8, OUTPUT);
}

void loop() {
  digitalWrite(TRIG, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG, LOW);
  int d = pulseIn(ECHO, HIGH) / 58;
  if (d < 60) {
    tone(8, map(d, 0, 60, 2000, 300));
  } else {
    noTone(8);
  }
  delay(100);
}
In the lab
In the lab

Result: the note rises as it comes closer.

Hard

Print the distance on the Serial too.

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

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

void loop() {
  digitalWrite(TRIG, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG, LOW);
  int d = pulseIn(ECHO, HIGH) / 58;
  Serial.println(d);
  if (d < 20) {
    tone(8, 1000);
  } else {
    noTone(8);
  }
  delay(200);
}
In the lab
In the lab
🖥️ Serial Monitor
50
50
12
12
12
12
12
12
12
12
12
12

Result: numbers on the Serial, and beeping.

Finished the lesson? Mark it so your progress counts.