-
Notifications
You must be signed in to change notification settings - Fork 0
/
Project_code.txt
87 lines (79 loc) · 1.68 KB
/
Project_code.txt
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
// Final Social Distancing Indicator and Alarming System
#include <Adafruit_NeoPixel.h>
int ledPin= 3;
int ledNo= 12;
Adafruit_NeoPixel strip= Adafruit_NeoPixel(ledNo,ledPin,NEO_RGB+NEO_KHZ800);
int buzzerPin= 2;
int echoPin= 6;
int trigPin= 5;
int minDistance = 100;
int maxDistance = 300;
void setup()
{
pinMode(buzzerPin, OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial. begin(9600);
strip.begin();
for(int i = 0; i < ledNo; i++)
{
strip.setPixelColor(i,strip.Color(0,0,0));
}
strip.show();
}
void loop()
{
int distance = calcDistance();
Serial.println(distance);
int ledsToGlow = map(distance, minDistance, maxDistance, ledNo, 1);
Serial.println(ledsToGlow);
if(ledsToGlow == 12)
{
digitalWrite(buzzerPin, HIGH);
}
else
{
digitalWrite(buzzerPin, LOW);
}
for(int i = 0; i < ledsToGlow; i++)
{
if(i < 4)
{
strip.setPixelColor(i,strip.Color(50,0,0));//green,red,blue
}
else if(i >= 4 && i < 8)
{
strip.setPixelColor(i,strip.Color(50,50,0));//green,red,blue
}
else if(i >= 8 && i < 12)
{
strip.setPixelColor(i,strip.Color(0,50,0));//green,red,blue
}
}
for(int i = ledsToGlow; i < ledNo; i++)
{
strip.setPixelColor(i,strip.Color(0,0,0));
}
strip.show();
delay(50);
}
int calcDistance()
{
long distance,duration;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration/29/2;
if(distance >= maxDistance)
{
distance = maxDistance;
}
if(distance <= minDistance)
{
distance = minDistance;
}
return distance;
}