-
Notifications
You must be signed in to change notification settings - Fork 0
/
finalproj.c
51 lines (40 loc) · 1.02 KB
/
finalproj.c
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
int dist_pin = 0;
//save distance reading
int current_dist = 0;
int sensor_dist= 0;
//alarm = 1 if set off or 0 is no alarm is needed
//int alarm = 0;
void setup() {
Serial.begin(9600);
}
void loop() {
//read in the sensor distance
sensor_dist = analogRead(dist_pin);
//convert into cm
current_dist=28250/(sensor_dist - 229.5);
//check to see if there is an accurate reading
//if there is a weird reading i.e negative/ large reading read again
while(current_dist < 0 || current_dist> 500){
sensor_dist = analogRead(dist_pin);
//convert into cm
current_dist=28250/(sensor_dist - 229.5);
}
//for debug, print the values on the monitor
//Serial.print(current_dist);
//Serial.println("cm");
//check to see if the attacker is about 4 feet away
//if yes then set alarm off
if(current_dist < 130){
//set alarm off
Serial.write("alarm");
}
/*
//if the alarm was triggered set off then turn alarm to 0
if(alarm = 1){
Serial.write("alarm");
alarm = 0;
}
*/
//delay by half a second
delay(500);
}