ุงู†ุชู‚ู„ ู„ู„ู…ุญุชูˆู‰
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

๐Ÿ› ๏ธ Section project: the home security system

Section 11: Smart systemsLesson 11.5 ยท โฑ 15 min
๐ŸŽฏ Project goal
The distance sensor, the buzzer and the screen: if someone comes closer than 30 cm the screen writes ALERT and the buzzer rings, otherwise SAFE and the buzzer is silent. Write all the code yourself.
โš™๏ธ The full solution: notice the distance() function we wrote to use in loop
#include <LiquidCrystal_I2C.h>

const int TRIG = 9;
const int ECHO = 10;
LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup() {
  pinMode(TRIG, OUTPUT);
  pinMode(ECHO, INPUT);
  pinMode(8, OUTPUT);
  lcd.init();
  lcd.backlight();
}

int distance() {
  digitalWrite(TRIG, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG, LOW);
  return pulseIn(ECHO, HIGH) / 58;
}

void loop() {
  int d = distance();
  lcd.setCursor(0, 0);
  if (d < 30) {
    lcd.print("ALERT");
    tone(8, 1500);
  } else {
    lcd.print("SAFE ");
    noTone(8);
  }
  delay(200);
}
Nobody: SAFE
Nobody: SAFE
Someone came close: ALERT and the buzzer rings
Someone came close: ALERT and the buzzer rings
๐Ÿ’ก Tip
Your own function: int distance() gathers the measuring lines in one place, like โ€œMy Blocksโ€ in Robo Block.
๐Ÿ–ฅ๏ธ What happens on the stage?
We ran this code in Robo Maker and the challenge passed: โ€œBravo! It worksโ€.

๐Ÿ“Œ Another example: the screen writes the distance

The same security system, and the screen writes the distance on the second line.

โš™๏ธ The screen writes the distance
#include <LiquidCrystal_I2C.h>

const int TRIG = 9;
const int ECHO = 10;
LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup() {
  pinMode(TRIG, OUTPUT);
  pinMode(ECHO, INPUT);
  pinMode(8, OUTPUT);
  lcd.init();
  lcd.backlight();
}

void loop() {
  digitalWrite(TRIG, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG, LOW);
  int d = pulseIn(ECHO, HIGH) / 58;
  lcd.clear();
  if (d < 20) {
    lcd.print("ALARM!");
    tone(8, 1500);
  } else {
    lcd.print("SAFE");
    noTone(8);
  }
  lcd.setCursor(0, 1);
  lcd.print(d);
  lcd.print(" cm");
  delay(300);
}
Safe
Safe
Alarm
Alarm
โœ๏ธ Practise
Write the security system yourself.
โœ๏ธ Practise here in the lessonor open the lab in a new page โ†—

๐ŸŽฏ Challenge yourself

Easy

The alarm comes and goes: a note for half a second and silence for half a second.

๐Ÿ‘€ Show the solution
โš™๏ธ Solution
#include <LiquidCrystal_I2C.h>

const int TRIG = 9;
const int ECHO = 10;
LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup() {
  pinMode(TRIG, OUTPUT);
  pinMode(ECHO, INPUT);
  pinMode(8, OUTPUT);
  lcd.init();
  lcd.backlight();
}

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, 1500);
    delay(500);
    noTone(8);
    delay(500);
  } else {
    noTone(8);
  }
}
In the lab
In the lab

Result: broken beeping.

Medium

Write โ€œWELCOMEโ€ if the distance is between 20 and 50, and โ€œSAFEโ€ if it is more.

๐Ÿ‘€ Show the solution
โš™๏ธ Solution
#include <LiquidCrystal_I2C.h>

const int TRIG = 9;
const int ECHO = 10;
LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup() {
  pinMode(TRIG, OUTPUT);
  pinMode(ECHO, INPUT);
  pinMode(8, OUTPUT);
  lcd.init();
  lcd.backlight();
}

void loop() {
  digitalWrite(TRIG, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG, LOW);
  int d = pulseIn(ECHO, HIGH) / 58;
  lcd.clear();
  if (d < 20) {
    lcd.print("ALARM!");
  } else if (d < 50) {
    lcd.print("WELCOME");
  } else {
    lcd.print("SAFE");
  }
  delay(300);
}
In the lab
In the lab

Result: WELCOME.

Hard

A counter: how many times did someone come close? Write the number on the screen.

๐Ÿ‘€ Show the solution
โš™๏ธ Solution
#include <LiquidCrystal_I2C.h>

const int TRIG = 9;
const int ECHO = 10;
LiquidCrystal_I2C lcd(0x27, 16, 2);
int count = 0;
bool near = false;

void setup() {
  pinMode(TRIG, OUTPUT);
  pinMode(ECHO, INPUT);
  pinMode(8, OUTPUT);
  lcd.init();
  lcd.backlight();
}

void loop() {
  digitalWrite(TRIG, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG, LOW);
  int d = pulseIn(ECHO, HIGH) / 58;
  if (d < 20 && !near) {
    count++;
    near = true;
  }
  if (d >= 20) {
    near = false;
  }
  lcd.clear();
  lcd.print("Visitors: ");
  lcd.print(count);
  delay(200);
}
In the lab
In the lab

Result: Visitors: 2

Finished the lesson? Mark it so your progress counts.