-
Notifications
You must be signed in to change notification settings - Fork 0
/
home_security.ino
64 lines (53 loc) · 1.36 KB
/
home_security.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
int lampPin = 11;
int pressurePin = 2;
int alarmPin = 12;
int buzzerPin = 6;
int switchPin = 8;
int timer;
bool alarm = false;
bool blinking = false;
bool activate = false;
int prevSwitch;
int prevPressureValue;
void setup() {
// put your setup code here, to run once:
pinMode(lampPin, OUTPUT);
pinMode(alarmPin, OUTPUT);
pinMode(pressurePin, INPUT);
pinMode(switchPin, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
int pmValue = analogRead(A0);
int mappedValue = map(pmValue, 0, 1023, -10, 255);959
int brightness = constrain(mappedValue, 0, 255);
analogWrite(lampPin, brightness);
// checking if alarm system is turned on
int switchValue = digitalRead(switchPin);
if (prevSwitch == 0 && switchValue == 1){
activate = !activate;
digitalWrite(alarmPin, HIGH);
}
prevSwitch = switchValue;
// checking if an intruder is stepping on the pressure plate
int pressureValue = digitalRead(pressurePin);
if (pressureValue == 1 && alarm == 0 && activate == 1) {
//trigger alarm
alarm = true;
timer = millis();
}
if (alarm) {
//trigger light and sound
if (millis() > timer) {
blinking = !blinking;
timer = millis() + 500;
}
if (blinking) {
digitalWrite(alarmPin, 255);
tone(buzzerPin, 400, 500 * 0.2);
}
else {
digitalWrite(alarmPin, 0);
}
}
}