-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeypad_Interfacing2.ino
More file actions
205 lines (179 loc) · 3.75 KB
/
Keypad_Interfacing2.ino
File metadata and controls
205 lines (179 loc) · 3.75 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
/*
The circuit:
* LCD RS pin to digital pin 7
* LCD Enable pin to digital pin 6
* LCD D4 pin to digital pin 5
* LCD D5 pin to digital pin 4
* LCD D6 pin to digital pin 3
* LCD D7 pin to digital pin 2
* LCD R/W pin to ground
*/
/*
For Ease of Concept & Programming,
Only Check Switch to Reset Password
When user has entered password initially
*/
// include the library code:
#include <LiquidCrystal.h>
#include <Keypad.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(8, 7, 6, 5, 4, 3);
#define SET 2
#define BUZZER 13
const byte ROWS = 4; //four rows
const byte COLS = 3; //four columns
char password[] = {
'9','5','1','3','6','9'};
/*
* Default Password: 951369
*/
char userPassword[6];
//define the symbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
{
'1','2','3' }
,
{
'4','5','6' }
,
{
'7','8','9' }
,
{
'*','0','#' }
};
byte rowPins[ROWS] = {
12, 11, 10, 9}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {
A5, A4, A3}; //connect to the column pinouts of the keypad
//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
byte trials = 0;
void setup(){
// set up the lcd's number of columns and rows:
lcd.begin(20,2);
pinMode(SET, INPUT_PULLUP);
pinMode(BUZZER, OUTPUT);
lcd.print("Enter Password:");
}
void loop(){
getPassword();
}
void setPassword(){
byte count = 0;
while(1){
char customKey = customKeypad.getKey();
if(customKey){
lcd.setCursor(count, 1);
lcd.write(customKey);
password[count]=customKey;
delay(200); // Debounce Delay
count++;
}
if(count == 6)
break;
}
lcd.clear();
lcd.print("Confirm Password:");
confirmPassword();
}
void confirmPassword(){
byte count = 0;
char confirmPassword[6];
while(1){
char customKey = customKeypad.getKey();
if(customKey){
confirmPassword[count]=customKey;
lcd.setCursor(count, 1);
lcd.write(customKey);
delay(200); // Debounce Delay
count++;
}
if(count == 6)
break;
}
byte temp=0;
for(char t=0; t<6; t++){
if(confirmPassword[t]==password[t]){
temp++;
}
}
if(temp == 6){
lcd.clear();
lcd.print("Password Matched!!");
delay(1000);
lcd.clear();
lcd.print("Enter Password:");
getPassword();
}
else{
lcd.clear();
lcd.print("Password Mismatch!!");
delay(1000);
lcd.clear();
lcd.print("Set Password:");
setPassword();
}
}
void getPassword(){
byte count = 0;
while(1){
char customKey = customKeypad.getKey();
if(customKey){
userPassword[count]=customKey;
lcd.setCursor(count, 1);
lcd.write('*');
delay(200); // Debounce Delay
count++;
}
if(count == 6)
break;
}
verifyPassword();
}
void verifyPassword(){
byte count=0;
for(char t=0; t<6; t++){
if(userPassword[t]==password[t]){
count++;
}
}
if(count == 6){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Correct Password");
delay(2000);
lcd.clear();
lcd.print("Press Switch for");
lcd.setCursor(0,1);
lcd.print("Password Reset");
while(1){
if(digitalRead(SET)==0){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Set Password:");
setPassword();
}
}
}
else{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Incorrect Password");
delay(1500);
trials++;
if(trials > 2){
lockSystem();
}
else{
lcd.clear();
lcd.print("Enter Password:");
}
}
}
void lockSystem(){
lcd.clear();
lcd.print("System Locked!!");
digitalWrite(BUZZER, HIGH);
while(1);
}