-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArduino-Smart-Lock.ino
More file actions
219 lines (191 loc) · 4.76 KB
/
Copy pathArduino-Smart-Lock.ino
File metadata and controls
219 lines (191 loc) · 4.76 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
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
#include <Keypad.h>
#include <LiquidCrystal.h>
#include <Servo.h>
#define PWD_LEN 5
#define SERVO_PIN 10
#define LOCK_TIMEOUT 4000
#define PENALTY_TIMEOUT 10000
// --- FINITE STATE MACHINE STATES ---
#define STATE_BOOT 0
#define STATE_LOCKED 1
#define STATE_INPUT 2
#define STATE_EVALUATE 3
#define STATE_UNLOCKED 4
#define STATE_LOCKOUT 5
int currentState = STATE_BOOT;
// --- HARDWARE INTERFACES ---
Servo myservo;
const int rs = A0, en = A1, d4 = A2, d5 = A3, d6 = A4, d7 = A5;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}};
byte rowPins[ROWS] = {0, 1, 2, 3};
byte colPins[COLS] = {4, 5, 6, 7};
Keypad customKeypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// --- VARIABLES ---
char Data[PWD_LEN];
char Master[PWD_LEN] = "A753";
byte data_count = 0;
byte failed_attempts = 0;
unsigned long stateTimer = 0;
int last_sec = -1; // Tracks the last displayed second to prevent LCD flickering
// --- FUNCTION PROTOTYPES ---
void TransitionTo(int nextState);
void ProcessKeypadInput();
void UnlockDoor();
void LockDoor();
void HandleFailedAttempt();
void clearData();
void setup()
{
myservo.attach(SERVO_PIN);
myservo.write(90); // Initialize locked position
lcd.begin(16, 2);
lcd.print("System Booting..");
stateTimer = millis();
currentState = STATE_BOOT;
}
void loop()
{
// FINITE STATE MACHINE CONTROLLER
switch (currentState)
{
case STATE_BOOT:
if (millis() - stateTimer >= 2000)
{
lcd.clear();
TransitionTo(STATE_LOCKED);
}
break;
case STATE_LOCKED:
lcd.setCursor(0, 0);
lcd.print("Enter Password:");
TransitionTo(STATE_INPUT);
break;
case STATE_INPUT:
ProcessKeypadInput();
if (data_count == PWD_LEN - 1)
{
TransitionTo(STATE_EVALUATE);
}
break;
case STATE_EVALUATE:
Data[data_count] = '\0'; // Null terminate for safety
if (!strcmp(Data, Master))
{
failed_attempts = 0;
UnlockDoor();
stateTimer = millis();
TransitionTo(STATE_UNLOCKED);
}
else
{
failed_attempts++;
HandleFailedAttempt();
}
clearData();
break;
case STATE_UNLOCKED:
if (millis() - stateTimer >= LOCK_TIMEOUT)
{
LockDoor();
TransitionTo(STATE_LOCKED);
}
break;
case STATE_LOCKOUT:
{
unsigned long elapsed = millis() - stateTimer;
// If the penalty time is over, unlock the system
if (elapsed >= PENALTY_TIMEOUT)
{
failed_attempts = 0;
lcd.clear();
TransitionTo(STATE_LOCKED);
}
else
{
// Calculate remaining seconds (+999 ensures it rounds up beautifully from 10 down to 1)
int remaining_sec = (PENALTY_TIMEOUT - elapsed + 999) / 1000;
// Only update the LCD if the second has actually changed
if (remaining_sec != last_sec)
{
lcd.setCursor(0, 1);
lcd.print("Wait: ");
lcd.print(remaining_sec);
lcd.print(" Sec "); // Extra spaces clear leftover characters like the '0' from '10'
last_sec = remaining_sec;
}
}
}
break;
}
}
// --- HELPER FUNCTIONS ---
void TransitionTo(int nextState)
{
currentState = nextState;
}
void ProcessKeypadInput()
{
char customKey = customKeypad.getKey();
if (customKey)
{
if (customKey == 'C')
{ // Input correction
clearData();
lcd.clear();
TransitionTo(STATE_LOCKED);
return;
}
Data[data_count] = customKey;
lcd.setCursor(data_count, 1);
lcd.print('*');
data_count++;
}
}
void UnlockDoor()
{
lcd.clear();
lcd.print("Access Granted");
myservo.write(0); // Open
}
void LockDoor()
{
lcd.clear();
lcd.print("Securing...");
myservo.write(90); // Close
delay(500);
lcd.clear();
}
void HandleFailedAttempt()
{
lcd.clear();
lcd.print("Access Denied");
lcd.setCursor(0, 1);
lcd.print("Attempts: ");
lcd.print(failed_attempts);
delay(1500);
if (failed_attempts >= 3)
{
lcd.clear();
lcd.print("SYSTEM LOCKED");
last_sec = -1; // Reset our LCD tracker for the countdown
stateTimer = millis();
TransitionTo(STATE_LOCKOUT);
}
else
{
lcd.clear();
TransitionTo(STATE_LOCKED);
}
}
void clearData()
{
memset(Data, 0, sizeof(Data));
data_count = 0;
}