-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdigital-lock.ino
More file actions
73 lines (62 loc) · 1.51 KB
/
digital-lock.ino
File metadata and controls
73 lines (62 loc) · 1.51 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
#include <Keypad.h>
#include <LiquidCrystal.h>
// Initialize the LCD: (RS, EN, D4, D5, D6, D7)
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
// Define the keypad layout
const byte ROWS = 4; // number of rows
const byte COLS = 4; // number of columns
char keyMap[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
// Connect keypad rows and columns to Arduino pins
byte rowPins[ROWS] = {9, 8, A3, A2};
byte colPins[COLS] = {A1, A0, 13, 12};
// Create the keypad object
Keypad keypad = Keypad(makeKeymap(keyMap), rowPins, colPins, ROWS, COLS);
// Set your password here
String correctPassword = "1234";
String userInput = "";
void setup() {
lcd.begin(16, 2); // Set LCD size
lcd.print("Enter Password:");
lcd.setCursor(0, 1); // Move to second line
}
void loop() {
char pressedKey = keypad.getKey();
if (pressedKey) {
if (pressedKey == '#') {
// User submitted the input
checkPassword();
}
else if (pressedKey == '*') {
// Clear input if * is pressed
resetInput();
}
else {
// Add character to input
if (userInput.length() < 16) {
userInput += pressedKey;
lcd.print('*'); // Hide input for security
}
}
}
}
void checkPassword() {
lcd.clear();
if (userInput == correctPassword) {
lcd.print("Access Granted");
} else {
lcd.print("Access Denied");
}
delay(2000);
resetInput();
}
void resetInput() {
userInput = "";
lcd.clear();
lcd.print("Enter Password:");
lcd.setCursor(0, 1);
}