-
Notifications
You must be signed in to change notification settings - Fork 0
/
MotionAlarm.ino
98 lines (83 loc) · 2.19 KB
/
MotionAlarm.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
#include <RCSwitch.h>
#define A 16736113
#define B 14527912
#define C 16736114
#define D 16736120
#define TRIG_PIN 3
#define ECHO_PIN 4
#define SPEAKER_PIN 5
#define TRIGGER_DISTANCE 80 // in cm
RCSwitch mySwitch = RCSwitch();
bool alarmActive = false;
bool sirenActive = false;
void setup() {
Serial.begin(9600);
mySwitch.enableReceive(0); // D2 433Mhz RX
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(SPEAKER_PIN, OUTPUT);
}
void playOkSound() {
tone(SPEAKER_PIN, 1000, 100);
delay(100);
noTone(SPEAKER_PIN);
}
void playResetSound() {
tone(SPEAKER_PIN, 500, 100);
delay(100);
noTone(SPEAKER_PIN);
}
void loop() {
if (mySwitch.available()) {
unsigned long value = mySwitch.getReceivedValue();
switch (value) {
case A:
alarmActive = true;
Serial.println("Alarm activated");
playOkSound();
break;
case B:
alarmActive = false;
sirenActive = false;
noTone(SPEAKER_PIN);
Serial.println("Alarm deactivated");
playResetSound();
break;
case C:
sirenActive = true;
Serial.println("Siren activated");
break;
case D:
sirenActive = false;
noTone(SPEAKER_PIN);
Serial.println("Siren deactivated");
playResetSound();
break;
}
mySwitch.resetAvailable();
}
long duration, distance;
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
duration = pulseIn(ECHO_PIN, HIGH);
distance = (duration * 0.034) / 2; // distance in cm
if (alarmActive && distance < TRIGGER_DISTANCE) {
sirenActive = true;
}
if (!sirenActive) {
noTone(SPEAKER_PIN);
} else {
tone(SPEAKER_PIN, 4000);
delay(100);
noTone(SPEAKER_PIN);
delay(100);
tone(SPEAKER_PIN, 4500);
delay(100);
noTone(SPEAKER_PIN);
delay(100);
}
delay(50);
}