-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathultrasonic_distance.ino
384 lines (339 loc) · 9.59 KB
/
ultrasonic_distance.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/*
Code written by Sam Wedge 2013
samwedge.co.uk
*/
#include <NewPing.h>
#include <SevSegThree.h>
//LED Scale Bar
#define Led1Pin 6
#define Led2Pin 9
#define Led3Pin 11
#define Led4Pin 10
#define Led5Pin 5
//Ultrasonic Sensor
#define trigPin 7
#define echoPin 8
//Alarm
#define alarmBuzzerPin 4
#define alarmIndicatorPin 12
#define alarmButtonPin 3
#define alarmButtonInterrupt 1
//3-Digit 7-Segment LED Display
#define digit1Pin 2
#define digit2Pin 1
#define digit3Pin 0
#define segmentAPin 14
#define segmentBPin 15
#define segmentCPin 16
#define segmentDPin 17
#define segmentEPin 18
#define segmentFPin 19
#define segmentGPin 13
SevSegThree sevseg;
//Ultrasonic
#define sonarMaxPingDistance 500
unsigned long lastPingTime = 0;
unsigned long prf = 5;
NewPing sonar(trigPin, echoPin, sonarMaxPingDistance);
unsigned long distance = sonarMaxPingDistance;
//Default Distances for scaling LEDs and alarm
unsigned long defaultMinDist = 10;
unsigned long defaultMaxDist = 100;
unsigned long minDist = defaultMinDist;
unsigned long maxDist = defaultMaxDist;
signed long pwmVal;
//Variables used by alarm buzzer
unsigned long lastBuzzOnTime = 0;
unsigned long lastBuzzOffTime = 0;
unsigned long buzzDelay; //determined based on distance
unsigned long maxBuzzDelay = 1000;
unsigned int buzzDuration = 20;
unsigned int buzzIntendedState = LOW;
unsigned int alarmIndicatorMode = 0;
unsigned int alarmIndicatorState = LOW;
unsigned int alarmIndicatorDelay = 100;
unsigned long alarmIndicatorTime = 0;
//Test Routine Parameters
#define testRoutineDelay 100
//Generic Variables
unsigned long currentTime = 0;
unsigned int ii,jj;
#define debounceDelay 100
//Defines what happens when the alarm button is pressed
void setAlarmDistance(){
static unsigned long lastInterruptTime = 0;
unsigned long interruptTime = millis();
if(interruptTime - lastInterruptTime > debounceDelay){
if(alarmIndicatorMode == 0){
alarmIndicatorMode = 1;
minDist = distance;
}
else if(alarmIndicatorMode == 1){
alarmIndicatorMode = 2;
if(distance > minDist){
maxDist = distance;
}
else{
alarmIndicatorMode = 0;
minDist = defaultMinDist;
maxDist = defaultMaxDist;
}
}
else{
alarmIndicatorMode = 0;
minDist = defaultMinDist;
maxDist = defaultMaxDist;
}
}
lastInterruptTime = interruptTime;
}
void testRoutine(){
analogWrite(Led1Pin,255);
delay(testRoutineDelay);
analogWrite(Led2Pin,255);
delay(testRoutineDelay);
analogWrite(Led3Pin,255);
delay(testRoutineDelay);
analogWrite(Led4Pin,255);
delay(testRoutineDelay);
analogWrite(Led5Pin,255);
delay(testRoutineDelay);
digitalWrite(alarmIndicatorPin,HIGH);
delay(testRoutineDelay);
digitalWrite(digit1Pin,HIGH);
digitalWrite(digit2Pin,HIGH);
digitalWrite(digit3Pin,HIGH);
digitalWrite(segmentFPin,LOW);
delay(testRoutineDelay);
digitalWrite(segmentAPin,LOW);
delay(testRoutineDelay);
digitalWrite(segmentBPin,LOW);
delay(testRoutineDelay);
digitalWrite(segmentCPin,LOW);
delay(testRoutineDelay);
digitalWrite(segmentDPin,LOW);
delay(testRoutineDelay);
digitalWrite(segmentEPin,LOW);
delay(testRoutineDelay);
digitalWrite(segmentGPin,LOW);
delay(testRoutineDelay);
digitalWrite(alarmBuzzerPin,HIGH);
delay(testRoutineDelay);
digitalWrite(alarmBuzzerPin,LOW);
delay(testRoutineDelay);
digitalWrite(digit3Pin,LOW);
delay(testRoutineDelay);
digitalWrite(digit2Pin,LOW);
delay(testRoutineDelay);
digitalWrite(digit1Pin,LOW);
delay(testRoutineDelay);
digitalWrite(segmentAPin,HIGH);
digitalWrite(segmentBPin,HIGH);
digitalWrite(segmentCPin,HIGH);
digitalWrite(segmentDPin,HIGH);
digitalWrite(segmentEPin,HIGH);
digitalWrite(segmentFPin,HIGH);
digitalWrite(segmentGPin,HIGH);
digitalWrite(alarmIndicatorPin,LOW);
for(ii=0;ii<1;ii++){
for(jj=255;jj>0;jj--){
analogWrite(Led5Pin,jj);
analogWrite(Led4Pin,jj);
analogWrite(Led3Pin,jj);
analogWrite(Led2Pin,jj);
analogWrite(Led1Pin,jj);
delay(2);
}
for(jj=0;jj<255;jj++){
analogWrite(Led5Pin,jj);
analogWrite(Led4Pin,jj);
analogWrite(Led3Pin,jj);
analogWrite(Led2Pin,jj);
analogWrite(Led1Pin,jj);
delay(2);
}
}
for(jj=255;jj>0;jj--){
analogWrite(Led5Pin,jj);
analogWrite(Led4Pin,jj);
analogWrite(Led3Pin,jj);
analogWrite(Led2Pin,jj);
analogWrite(Led1Pin,jj);
delay(2);
}
}
void setup(){
pinMode(Led1Pin, OUTPUT);
pinMode(Led2Pin, OUTPUT);
pinMode(Led3Pin, OUTPUT);
pinMode(Led4Pin, OUTPUT);
pinMode(Led5Pin, OUTPUT);
pinMode(alarmBuzzerPin, OUTPUT);
pinMode(alarmIndicatorPin, OUTPUT);
pinMode(alarmButtonPin, INPUT);
//3-Digit 7-Segment LED Display
pinMode(digit1Pin, OUTPUT);
pinMode(digit2Pin, OUTPUT);
pinMode(digit3Pin, OUTPUT);
pinMode(segmentAPin, OUTPUT);
pinMode(segmentBPin, OUTPUT);
pinMode(segmentCPin, OUTPUT);
pinMode(segmentDPin, OUTPUT);
pinMode(segmentEPin, OUTPUT);
pinMode(segmentFPin, OUTPUT);
pinMode(segmentGPin, OUTPUT);
digitalWrite(digit1Pin,LOW);
digitalWrite(digit2Pin,LOW);
digitalWrite(digit3Pin,LOW);
digitalWrite(segmentAPin,HIGH);
digitalWrite(segmentBPin,HIGH);
digitalWrite(segmentCPin,HIGH);
digitalWrite(segmentDPin,HIGH);
digitalWrite(segmentEPin,HIGH);
digitalWrite(segmentFPin,HIGH);
digitalWrite(segmentGPin,HIGH);
attachInterrupt(alarmButtonInterrupt, setAlarmDistance, RISING);
sevseg.Begin(1,digit1Pin,digit2Pin,digit3Pin,segmentAPin,segmentBPin,segmentCPin,segmentDPin,segmentEPin,segmentFPin,segmentGPin);
testRoutine();
}
void setScaleLedsOff(){
digitalWrite(Led1Pin,LOW);
digitalWrite(Led2Pin,LOW);
digitalWrite(Led3Pin,LOW);
digitalWrite(Led4Pin,LOW);
digitalWrite(Led5Pin,LOW);
}
void setScaleLeds(unsigned int distance, unsigned int minDist, unsigned int maxDist){
pwmVal = (255*4)-(((distance-minDist)*(long)255*(long)4)/(maxDist-minDist));
if(distance <= minDist){
digitalWrite(Led1Pin,HIGH);
digitalWrite(Led2Pin,HIGH);
digitalWrite(Led3Pin,HIGH);
digitalWrite(Led4Pin,HIGH);
digitalWrite(Led5Pin,HIGH);
}
else{
digitalWrite(Led5Pin,LOW);
if(pwmVal > (255*3)){
analogWrite(Led4Pin,pwmVal-255*3);
digitalWrite(Led3Pin,HIGH);
digitalWrite(Led2Pin,HIGH);
digitalWrite(Led1Pin,HIGH);
}
else if(pwmVal > (255*2)){
digitalWrite(Led4Pin,LOW);
analogWrite(Led3Pin,pwmVal-255*2);
digitalWrite(Led2Pin,HIGH);
digitalWrite(Led1Pin,HIGH);
}
else if(pwmVal > 255){
digitalWrite(Led4Pin,LOW);
digitalWrite(Led3Pin,LOW);
analogWrite(Led2Pin,pwmVal-255);
digitalWrite(Led1Pin,HIGH);
}
else if(pwmVal > 0){
digitalWrite(Led4Pin,LOW);
digitalWrite(Led3Pin,LOW);
digitalWrite(Led2Pin,LOW);
analogWrite(Led1Pin,pwmVal);
}
else{
digitalWrite(Led4Pin,LOW);
digitalWrite(Led3Pin,LOW);
digitalWrite(Led2Pin,LOW);
digitalWrite(Led1Pin,LOW);
}
}
}
void loop(){
currentTime = millis();
//Fire the transducer, if the PRF criteria has been met
if((currentTime - lastPingTime) > (1000/prf)){
distance = sonar.ping()/US_ROUNDTRIP_CM;
//Prevent an unintended alarm shortly after everything has fired up
if(currentTime < 5000){
distance = 0;
}
sevseg.NewNum(distance);//Update the LED display
lastPingTime = currentTime;
}
if(alarmIndicatorMode == 1){
setScaleLedsOff();
digitalWrite(alarmBuzzerPin,LOW);
}
else{
//Light up the scale-bar LEDs based upon the distance
if(distance != 0){
setScaleLeds(distance,minDist,maxDist);
}
else{
setScaleLedsOff();
}
//Turn the buzzer on or off
//--Should the last-initiated buzz be switched off?
if(digitalRead(alarmBuzzerPin)==HIGH){
if((currentTime - lastBuzzOnTime) >= buzzDuration){
buzzIntendedState = LOW;
lastBuzzOffTime = currentTime;
}
}
//--Has the buzzer delay been exceeded?
if((distance < maxDist) & (distance > minDist)){
//1000 on next line to prevent flooring of a float<1 to 0
buzzDelay = (((distance-minDist))*1000/(maxDist-minDist))*maxBuzzDelay/1000;
if((currentTime - lastBuzzOnTime) >= buzzDelay){
buzzIntendedState = HIGH;
lastBuzzOnTime = currentTime;
}
}
else if(distance >= maxDist){
buzzIntendedState = LOW;
}
else if((distance <= minDist) & (distance > 0)){
buzzIntendedState = HIGH;
}
else{
buzzIntendedState = LOW;
}
//--Change buzzer state, if required
if(digitalRead(alarmBuzzerPin) != buzzIntendedState){
digitalWrite(alarmBuzzerPin,buzzIntendedState);
}
}
//Set the alarm indicator LED on or off
if(alarmIndicatorMode == 0){
alarmIndicatorState = LOW;
}
else if(alarmIndicatorMode == 1){
if((currentTime - alarmIndicatorTime) >= alarmIndicatorDelay){
if(alarmIndicatorState == LOW){
alarmIndicatorState = HIGH;
}
else if(alarmIndicatorState == HIGH){
alarmIndicatorState = LOW;
}
alarmIndicatorTime = currentTime;
}
}
else if(alarmIndicatorMode == 2){
alarmIndicatorState = HIGH;
}
if(digitalRead(alarmIndicatorPin) != alarmIndicatorState){
digitalWrite(alarmIndicatorPin,alarmIndicatorState);
}
//Display the number on the sev seg display
if(distance != 0){
sevseg.PrintOutput();
}
else{
digitalWrite(digit1Pin,HIGH);
digitalWrite(digit2Pin,HIGH);
digitalWrite(digit3Pin,HIGH);
digitalWrite(segmentGPin,LOW);
digitalWrite(digit1Pin,LOW);
digitalWrite(digit2Pin,LOW);
digitalWrite(digit3Pin,LOW);
digitalWrite(segmentGPin,HIGH);
}
}