-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproximityAlarm.ino
108 lines (86 loc) · 2.8 KB
/
proximityAlarm.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
/*
Inspectio
Detects objects on the left and right of a person wearing the belt and signals them
when objects are close by
circuit:
- 1, 8 ohm speaker on digital pin 8
- #, ultrasonic sensors
created 20 Oct 2019
by Blue Wall Peeps
https://github.com/CSBar/HackUMass19
*/
// program parameters
const int minDistance = 70; //farthest object distance that will trigger a beep
const int numOfSensors = 2; //number of functional sensors connected
typedef struct sensor {
int trigPin;
int echoPin;
} sensor;
// defines pin numbers
const sensor sensorList[numOfSensors] = {{8, 7}, {3,2}};
const int buzzer = 13;
enum direction {
LEFT,
RIGHT
};
/*
* Has the specified sensor scan for nearby objects
* @param ultrasonicSensor - specified sensor with valid trigPin and echoPin connected to arduino
* @return distance (in cm) of the nearest object detected by the sensor
*/
int detectNearestObject(sensor ultrasonicSensor);
/*
* Sends a signal to the speaker to play specified sounds
* Discriminates left and right sensor signals and sounds more frequently when objects are closer to the sensor
* @param direction - whether the sensor sending a signal is from the left or right side of the person
* @param distance - distance (in cm) of the nearest object detected by the sensor
* @return NOTHING
*/
void playBuzzerSound(int direction, int distance);
void setup() {
for(int i=0; i<numOfSensors; i++) {
pinMode(sensorList[i].trigPin, OUTPUT); // Sets each trigPin as an Output
pinMode(sensorList[i].echoPin, INPUT); // Sets each echoPin as an Input
}
pinMode(buzzer, OUTPUT); // Sets the buzzer as an Output
Serial.begin(9600); // Starts the serial communication
}
void loop() {
for(int i=0; i<numOfSensors; i++) {
int distance = detectNearestObject(sensorList[i]);
if(distance < minDistance)
playBuzzerSound(i % 2, distance);
}
}
int detectNearestObject(sensor ultrasonicSensor) {
// Clears the trigPin
digitalWrite(ultrasonicSensor.trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(ultrasonicSensor.trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(ultrasonicSensor.trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
long duration = pulseIn(ultrasonicSensor.echoPin, HIGH);
// Calculating the distance
int distance= duration*0.034/2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
return distance;
}
void playBuzzerSound(int direction, int distance) {
int delayTime = distance * 3;
if(direction == LEFT) {
tone(buzzer, 1000, 200);
delay(delayTime);
noTone(buzzer);
delay(delayTime);
}
else {
tone(buzzer, 1100, 100);
delay(delayTime);
noTone(buzzer);
delay(delayTime);
}
}