-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArduino-alarm.cpp
More file actions
90 lines (78 loc) · 2.19 KB
/
Arduino-alarm.cpp
File metadata and controls
90 lines (78 loc) · 2.19 KB
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
/* NOTE
1) Press the ultra sonic distance sensor at top left
2) Move the dot in range to trigger the alarm
3) Enter 123ABC in the touchpad to disable the alarm
*/
/* This arduino triggers an alarm if someone is sensed
in front of the ultrasonic sensor, to disable the alarm,
the password must be entered in the keypad */
#include <Keypad.h>
#include<Arduino.h>
using namespace std;
// constants
#define SIGNAL 11
#define ALARM 13
#define RED_LIGHTS 12
void setup(){
init();
Serial.begin(9600);
pinMode(ALARM,OUTPUT);
pinMode(RED_LIGHTS,OUTPUT);
}
int main(){
setup();
int time,distance;
// setting up the keypad
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {5, 4, 3, 2}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
// password to stop alarm
char password[]= "123ABC";
int pw_length = 6;
int counter = 0;
while(true){
// to get distance to ultrasound distance sensor
pinMode(SIGNAL,OUTPUT);
digitalWrite (SIGNAL, LOW);
delayMicroseconds (2);
digitalWrite(SIGNAL, HIGH);
delayMicroseconds(5);
digitalWrite(SIGNAL,LOW);
pinMode(SIGNAL, INPUT);
time = pulseIn(SIGNAL, HIGH);
distance = (time * 0.034) / 2;
if (distance <= 300 && distance>=0){
// someone in front of sensor
digitalWrite(ALARM,HIGH);
digitalWrite(RED_LIGHTS,HIGH);
// keypad inputs
char key = keypad.getKey();
if (key){
Serial.print(key);
if (key == password[counter]){
counter++;
if (counter == pw_length){
Serial.println(" ");
Serial.println("ALARM DISABLED");
digitalWrite(ALARM, LOW);
digitalWrite(RED_LIGHTS, LOW);
break;
}
}else{
counter = 0; //reset
}
}
}else{
digitalWrite(ALARM,LOW);
digitalWrite(RED_LIGHTS,LOW);
}
}
}