-
Notifications
You must be signed in to change notification settings - Fork 0
/
parking_sensor.ino
152 lines (129 loc) · 3.07 KB
/
parking_sensor.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include <LiquidCrystal.h>
int echoPin = 2;
int trigPin = 3;
int speakerPin = 5;
int redPin = 4;
int greenPin = 1;
int yellowPins[] = {6, 7};
int rs = 12;
int enable = 13;
int d4 = 8, d5 = 9, d6 = 10, d7 = 11;
LiquidCrystal lcd(rs, enable, d4, d5, d6, d7);
void setup()
{
Serial.begin(9600);
lcd.begin(16, 2);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
for (int i = 4; i <= 7; i++)
pinMode(i, OUTPUT);
}
void loop()
{
long duration = 0, distanceCM = 0;
long FirstWarningDistance = 50;
long SecondWarningDistance = 30;
long ThirdWarningDistance = 20;
long maxDistance = 10;
int lcd_dt = 250; //delay time (LCD)
//clear trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
//wave for 10 us
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
//echoPin reads wave travel time in us
duration = pulseIn(echoPin, HIGH);
distanceCM = 0.017 * duration;
if (distanceCM <= maxDistance)
{
finalWarning();
}
else if (distanceCM <= ThirdWarningDistance)
{
thirdWarning(distanceCM, ThirdWarningDistance);
redBlink(distanceCM, ThirdWarningDistance);
}
else if (distanceCM <= SecondWarningDistance)
{
secondWarning();
}
else if (distanceCM <= FirstWarningDistance)
{
firstWarning();
}
else
{
noWarning();
}
//LCD printing
lcd.setCursor(0, 0);
lcd.print("Distance: ");
lcd.print(distanceCM);
lcd.print(" cm");
delay(lcd_dt);
lcd.clear();
//Serial Monitor printing
Serial.print("Distance: ");
Serial.print(distanceCM);
Serial.print(" cm\n");
}
void firstWarning()
{
analogWrite(speakerPin, 0);
digitalWrite(greenPin, HIGH);
digitalWrite(yellowPins[0], HIGH);
digitalWrite(yellowPins[1], LOW);
digitalWrite(redPin, LOW);
}
void secondWarning()
{
analogWrite(speakerPin, 0);
digitalWrite(greenPin, HIGH);
digitalWrite(yellowPins[0], HIGH);
digitalWrite(yellowPins[1], HIGH);
digitalWrite(redPin, LOW);
}
void thirdWarning(long distance, long thirdWarningValue)
{
int dt = 100;
while(distance <= thirdWarningValue){
analogWrite(speakerPin, 100);
delay(dt);
analogWrite(speakerPin, 0);
break;
}
digitalWrite(greenPin, HIGH);
digitalWrite(yellowPins[0], HIGH);
digitalWrite(yellowPins[1], HIGH);
digitalWrite(redPin, LOW);
}
void finalWarning()
{
analogWrite(speakerPin, 100);
digitalWrite(greenPin, HIGH);
digitalWrite(yellowPins[0], HIGH);
digitalWrite(yellowPins[1], HIGH);
digitalWrite(redPin, HIGH);
}
void noWarning()
{
analogWrite(speakerPin, 0);
digitalWrite(greenPin, HIGH);
digitalWrite(yellowPins[0], LOW);
digitalWrite(yellowPins[1], LOW);
digitalWrite(redPin, LOW);
}
void redBlink(long distance, long maxValue)
{
int dt = 50;
while (distance <= maxValue)
{
digitalWrite(redPin, HIGH);
delay(dt);
digitalWrite(redPin, LOW);
delay(dt);
break;
}
}