This repository has been archived by the owner on Jun 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAlarm_System.ino
211 lines (182 loc) · 4.19 KB
/
Alarm_System.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include <Keypad.h>
#include <Password.h>
#include <LiquidCrystal.h>
String newPasswordString; //hold the new password
char newPassword[6]; //charater string of newPasswordString
const int rs = 41, en = 40, d4 = 45, d5 = 44, d6 = 43, d7 = 42;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
int RedPin = 48;
int GreenPin = 49;
int BPin = 46;
int DoorPin = 13;
int AlarmPinOut = 34;
int DoorPinOut = 36;
int Door;
int AlarmArmed = 0;
//int DoorPin = 30;
//int WindowPin = 31;
//initialize password to 1333
//you can use password.set(newPassword) to overwrite it
Password password = Password( "1333" );
byte maxPasswordLength = 6;
byte currentPasswordLength = 0;
const byte ROWS = 4; // Four rows
const byte COLS = 4; // Four columns
//Define the keymap
char keys[ROWS][COLS] =
{
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
//// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = {5,4,3,2}; //connect to row pinouts
// Connect keypad COL0, COL1, COL2 and COL3 to these Arduino pins.
byte colPins[COLS] = {9,8,7,6}; //connect to column pinouts
// Create the Keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup()
{ // put your setup code here, to run once:
pinMode(RedPin, OUTPUT);
pinMode(GreenPin, OUTPUT);
pinMode(BPin, OUTPUT);
pinMode(AlarmPinOut, OUTPUT);
pinMode(DoorPinOut, OUTPUT);
pinMode(DoorPin, INPUT);
lcd.begin(16, 2);
lcd.print("Alarm");
Serial.begin(9600);
}
void loop()
{
keypadprocess();
Door = digitalRead(DoorPin);
//Serial.println(Door);
//delay(100000);
if (Door == 0 && AlarmArmed == 1)
{
digitalWrite(DoorPinOut, HIGH);
delay(500);
digitalWrite(DoorPinOut, LOW);
int i = 0;
while(AlarmArmed == 1 && i<100)
{
keypadprocess();
digitalWrite(BPin, HIGH);
delay(100);
digitalWrite(BPin, LOW);
delay(100);
i++;
}
checkPassword();
if(AlarmArmed == 1)
{
digitalWrite(AlarmPinOut, HIGH);
delay(500);
digitalWrite(AlarmPinOut, LOW);
}
}
}
void keypadprocess()
{
lcd.setCursor(0, 1);
char key = keypad.getKey();
if (key != NO_KEY)
{
delay(60);
switch (key)
{
case 'A': break;
case 'B': break;
case 'C': break;
case 'D': break;
case '#': checkPassword(); break;
default: processNumberKey(key);
}
}
}
void processNumberKey(char key)
{
Serial.print(key);
//lcd.print(key);
currentPasswordLength++;
password.append(key);
if (currentPasswordLength == maxPasswordLength)
{
checkPassword();
}
if (currentPasswordLength == 1)
{
lcd.print("*");
}
else if (currentPasswordLength == 2)
{
lcd.print("**");
}
else if (currentPasswordLength == 3)
{
lcd.print("***");
}
else if (currentPasswordLength == 4)
{
lcd.print("****");
}
else if (currentPasswordLength == 5)
{
lcd.print("*****");
}
else if (currentPasswordLength == 6)
{
lcd.print("******");
}
}
void checkPassword()
{
if (password.evaluate())
{
lcd.print("Password OK");
Serial.println(" OK.");
digitalWrite(BPin, HIGH);
digitalWrite(GreenPin, HIGH);
delay(1500);
digitalWrite(GreenPin, LOW);
digitalWrite(BPin, LOW);
if(AlarmArmed == 0)
{
AlarmArmed = 1;
lcd.begin(16, 2);
lcd.print("Alarm Armed");
}
else
{
AlarmArmed = 0;
lcd.begin(16, 2);
lcd.print("Alarm");
}
}
else
{
lcd.print("Password Wrong");
Serial.println(" Wrong passwowrd!");
digitalWrite(BPin, HIGH);
digitalWrite(RedPin, HIGH);
delay(1500);
digitalWrite(RedPin, LOW);
digitalWrite(BPin, LOW);
if(AlarmArmed == 1)
{
digitalWrite(AlarmPinOut, HIGH);
delay(500);
digitalWrite(AlarmPinOut, LOW);
}
lcd.begin(16, 2);
lcd.print("Alarm");
}
resetPassword();
}
void resetPassword()
{
password.reset();
currentPasswordLength = 0;
}