๐ ๏ธ 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.
#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);
}


๐ก 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.
#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);
}


โ๏ธ Practise
Write the security system yourself.
โ๏ธ Practise here in the lessonโ Close the labor 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๐ Hide the 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);
}
}

Result: broken beeping.
Medium
Write โWELCOMEโ if the distance is between 20 and 50, and โSAFEโ if it is more.
๐ Show the solution๐ Hide the 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);
}

Result: WELCOME.
Hard
A counter: how many times did someone come close? Write the number on the screen.
๐ Show the solution๐ Hide the 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);
}

Result: Visitors: 2

