-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocial_distancing_detector code.ino
112 lines (90 loc) · 2.83 KB
/
social_distancing_detector code.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// C++ code
/*
*****This system is used to get alert when any moving entity comes closer to you.
i.e. when distance between you and him is less than 200
*/
//importing library for LCD
#include<LiquidCrystal.h>
//setting function for LCD 16x2. passing arduino pin nos that are connected as parameters.
LiquidCrystal lcd(A0, A1, 5, 4, 3, 2);
//pins connected to ultrasonic distance sensor
int triggerPin = 7;
int echoPin = 6;
//variables
unsigned long duration;
int distance;
//buzzer pin connected to piezo
int pinBuzzer =13;
//pins connected to red color and green color of LED RGB
int rPin =10;
int gPin =8;
//pin connected to PIR sensor
int pirPin =12;
//variable
int isHuman =0;
void setup()
{
//setting pin mode for each of the pins declared above of arduino
//whether pin is for input or output
pinMode(triggerPin, OUTPUT);
pinMode (echoPin, INPUT);
pinMode(pinBuzzer, OUTPUT);
pinMode(rPin, OUTPUT);
pinMode(gPin, OUTPUT);
pinMode(pirPin, INPUT);
//initializing for monitor
Serial.begin(9600);
//initializing LCD 16x2
lcd.begin(16, 2);
}
//we haven't used any function. If functions are used, they must be called within void loop
void loop()
{
//if PIR pin finds any motion, it will be store in isHuman variable
isHuman = digitalRead(pirPin);
Serial.println(isHuman);
if (isHuman ==1){
//when human is present, calculating distance
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
//clearing the trigger
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
// capturing the time duration for sound wave to travel in microseconds
duration = pulseIn(echoPin, HIGH);
distance = 0.01723 * duration; //formula to calculate distance
Serial.print(distance); //print distance
Serial.println("cm");
if (isHuman == 1 && distance < 200 ){
//when distance is less than 200, send a message on LCD 16x2, LED RGB will turn red, buzzer/piezo will sound
//LCD RGB will be red
digitalWrite(rPin, HIGH);
digitalWrite(gPin, LOW);
//piezo/buzzer will sound
tone(pinBuzzer,293);
//display message on LCD 16x2
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" STAY!!");
lcd.setCursor(0,1);
lcd.print(" AWAY!!");
//after 200sec stop buzzer/piezo sound, LCD RGB will be off, LCD 16x2 display will be cleared
delay(200);
noTone(pinBuzzer);
digitalWrite(rPin, LOW);
lcd.clear();
}
else {
//if human is present, but distance is more than 200
//LCD RGB will glow green
digitalWrite(rPin, LOW);
digitalWrite(gPin, HIGH);
//LCD 16x2 will show safe message
lcd.setCursor(0,0);
lcd.print(" You are safe");
lcd.setCursor(0,1);
lcd.print(" ");
}
}
}