-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIOT102PT1.ino
346 lines (318 loc) · 5.86 KB
/
IOT102PT1.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#include <Keypad.h>
#include <EEPROM.h>
#include <LiquidCrystal.h>
#include <Servo.h>
const int rs = A0, en = A1, d4 = A2, d5 = A3, d6 = A4, d7 = A5;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
Servo myservo;
const byte ROWS = 4;
const byte COLS = 4;
char hexaKeys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}};
byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};
Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
const int motorPin = 10;
const int buzzerPin = 13;
const int TRIG_PIN = 11;
const int ECHO_PIN = 12;
int inputCount = 0;
int timeLock = 0;
bool doorOpen = false;
const int distanceThreshold = 20; // 20cm
String D2003 = "1234";
String D2004 = "5678";
String D2005 = "1357";
void setup()
{
Serial.begin(9600);
lcd.begin(16, 2);
myservo.attach(motorPin);
myservo.write(0);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
if (readStringFromEEPROM(0) == "")
{
writeStringToEEPROM(0, D2003);
}
else
{
D2003 = readStringFromEEPROM(0);
}
if (readStringFromEEPROM(5) == "")
{
writeStringToEEPROM(5, D2004);
}
else
{
D2004 = readStringFromEEPROM(5);
}
if (readStringFromEEPROM(10) == "")
{
writeStringToEEPROM(10, D2005);
}
else
{
D2005 = readStringFromEEPROM(10);
}
}
void writeStringToEEPROM(int addrOffset, String strToWrite)
{
byte len = strToWrite.length();
EEPROM.write(addrOffset, len);
for (int i = 0; i < len; i++)
{
EEPROM.write(addrOffset + 1 + i, strToWrite[i]);
}
}
String readStringFromEEPROM(int addrOffset)
{
int newStrLen = EEPROM.read(addrOffset);
char data[newStrLen + 1];
for (int i = 0; i < newStrLen; i++)
{
data[i] = EEPROM.read(addrOffset + 1 + i);
}
data[newStrLen] = '\0';
return String(data);
}
boolean verifyPassword(String enteredPassword, String password)
{
return enteredPassword.indexOf(password) != -1;
}
void enterDisplay()
{
lcd.setCursor(0, 0);
lcd.print("Enter password :");
Serial.println("Enter password :");
}
String getPasswordInput(boolean changePass)
{
lcd.clear();
unsigned long startTime = millis();
unsigned long timeout = 20000;
if (changePass == false)
{
lcd.print("Enter password :");
}
else
{
lcd.print("New password: ");
}
String lcdPass = "";
String input = "";
char key;
while (true)
{
key = customKeypad.getKey();
if (key)
{
if (key == '#')
{
break;
}
else if (key == 'C')
{
boolean success = changePassword();
if (success)
{
return "change";
}
else
{
return "not change";
}
}
else if (key == 'D')
{
lcdPass = "";
input = "";
lcd.clear();
lcd.print("Enter password :");
}
else if (key == 'B')
{
writeStringToEEPROM(0, "1234");
writeStringToEEPROM(5, "5678");
writeStringToEEPROM(10, "1357");
}
else
{
lcd.setCursor(0, 1);
lcdPass += "*";
lcd.print(lcdPass);
input += key;
}
}
if (millis() - startTime > timeout)
{
return "Timeout! No input.";
}
}
return input;
}
boolean changePassword()
{
lcd.clear();
lcd.print("Before password:");
String lcdPass = "";
String input = "";
char key;
while (true)
{
key = customKeypad.getKey();
if (key)
{
if (key == '#')
{
break;
}
else
{
lcd.setCursor(0, 1);
lcdPass += "*";
lcd.print(lcdPass);
input += key;
}
}
}
String passEEPROM = readStringFromEEPROM(0);
String newPass = "";
if (input == D2003 || input == passEEPROM)
{
newPass = getPasswordInput(true);
D2003 = newPass;
writeStringToEEPROM(0, D2003);
return true;
}
else if (input == D2004)
{
newPass = getPasswordInput(true);
D2004 = newPass;
writeStringToEEPROM(5, D2004);
return true;
}
else if (input == D2005)
{
newPass = getPasswordInput(true);
D2005 = newPass;
writeStringToEEPROM(10, D2005);
return true;
}
else
{
lcd.clear();
lcd.print("Invalid password");
delay(2000);
}
return false;
}
void incorrectPass()
{
lcd.print("Lock surcurity!");
tone(buzzerPin, 1000, 3000);
delay(3000);
noTone(buzzerPin);
lcd.setCursor(0, 0);
}
void openDoor()
{
myservo.write(90);
doorOpen = true;
}
void closeDoor()
{
myservo.write(0);
doorOpen = false;
}
void lockKeypad()
{
lcd.clear();
lcd.print("Keypad locked!");
String countDown = "";
tone(buzzerPin, 1000, 3000);
lcd.setCursor(0, 1);
timeLock += 3000;
for (int i = timeLock / 1000; i > 0; i--)
{
lcd.setCursor(0, 1);
if (i < 10)
{
lcd.print("0");
}
lcd.print(i);
delay(1000);
}
lcd.clear();
lcd.print("Unlocking...");
delay(2000);
}
void loop()
{
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
const long duration = pulseIn(ECHO_PIN, HIGH);
int distance = duration / 29 / 2;
if (distance < distanceThreshold && !doorOpen)
{
String enteredPassword = getPasswordInput(false);
String passEEPROM1 = readStringFromEEPROM(0);
String passEEPROM2 = readStringFromEEPROM(5);
String passEEPROM3 = readStringFromEEPROM(10);
if (verifyPassword(enteredPassword, passEEPROM1) ||
verifyPassword(enteredPassword, passEEPROM2) ||
verifyPassword(enteredPassword, passEEPROM3))
{
lcd.clear();
lcd.print("Door Opened!");
inputCount = 0;
openDoor();
delay(5000);
closeDoor();
}
else if (enteredPassword == "change")
{
lcd.clear();
lcd.print("Change ok!");
delay(3000);
}
else if (enteredPassword == "not change")
{
lcd.clear();
lcd.print("Change not ok!");
delay(3000);
}
else if (enteredPassword == "Timeout! No input.")
{
lcd.clear();
lcd.print("Timeout!");
delay(2000);
}
else
{
inputCount++;
if (inputCount >= 3)
{
lockKeypad();
}
else
{
lcd.clear();
lcd.print("Wrong password!");
delay(2000);
lcd.clear();
}
}
}
else if (distance > distanceThreshold)
{
lcd.clear();
}
delay(100);
}