-
Notifications
You must be signed in to change notification settings - Fork 0
/
SmartDoor.ino
327 lines (280 loc) · 6.81 KB
/
SmartDoor.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
// Library Section
#include <SPI.h>
#include <MFRC522.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#include <Servo.h>
// Constant Section
#define SS_PIN 10
#define RST_PIN 9
const byte ROWS = 4;
const byte COLS = 4;
// Tools
//1. KeyPad
/// KeyPad Map
char keys[ROWS][COLS] = {
{'1','4','7','*'},
{'2','5','8','0'},
{'3','6','9','#'},
{'A','B','C','D'}
};
/// KeyPad Pins
byte rowPins[ROWS] = {4, 3, 2, 0}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {8, 7, 6, 5}; //connect to the column pinouts of the keypad
/// KeyPad Variables
int counter=0;
// Users Details
char User[][6]={ // Users Name
{"Ahmed"}, // User 1 ( Card 1, Name Ahmed, Enable)
{"Ali"}, // User 2 ( Card 2, Name Ali, Enable)
// {"Max"} // User 3 ( Card 3, Name Max, Disable)
},
cardsAccept[][13]={ // Users Card
{"45 5E 80 20"},// User 1 ( Card 1, Name Ahmed, Enable)
{"40 B0 02 74"},// User 2 ( Card 2, Name Ali, Enable)
// {"D0 33 81 7A"} // User 3 ( Card 3, Name Max, Disable)
},
UserPassword[][6]={ // Users Password
{"C157B"}, // User 1 ( Card 1, Name Ahmed, Enable)
{"1AC45"}, // User 2 ( Card 2, Name Ali, Enable)
// {"A12DC"} // User 3 ( Card 3, Name Max, Disable)
};
//2. RFID
int cardsNumber = sizeof(cardsAccept) / sizeof (cardsAccept[0]);
//3. LED
int ledR = A2, ledG = A3;
//4. Sound
//int openSound = A0;
// Class Section
LiquidCrystal_I2C lcd(0x27,16,2);
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
Servo servoOpen;
Servo servoLock;
void setup()
{
// Public
Serial.begin(9600);
// RFID
SPI.begin();
mfrc522.PCD_Init();
Serial.println("Approximate your card to the reader...");
Serial.println();
// LCD i2c
lcd.init();
lcd.backlight();
// Servo Motor
servoLock.attach(A0);
servoOpen.attach(A1);
// Leds
pinMode(ledR, OUTPUT);
pinMode(ledG, OUTPUT);
// Sound
pinMode(1, OUTPUT);
}
void loop()
{
// Is Cards Access Authorized Or Not.
bool access;
int myCard;
closeBoth(); // Close Both Door & Lock
welcomCard(); //Say Hello On LCD
digitalWrite(ledR,HIGH);
digitalWrite(ledG,LOW);
// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent())
return;
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial())
return;
// Wait Checking Access On LCD
waitChecking();
//Show UID on serial monitor
Serial.print("UID tag :");
String content= "";
byte letter;
for (byte i = 0; i < mfrc522.uid.size; i++)
{
Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(mfrc522.uid.uidByte[i], HEX);
content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
content.concat(String(mfrc522.uid.uidByte[i], HEX));
}
Serial.println();
Serial.print("Message : ");
content.toUpperCase();
for(int i=0; i<cardsNumber; i++){
if ( content.substring(1) == cardsAccept[i] ){ //change here the UID of the card/cards that you want to give access
access = true;
myCard = i;
break;
}
else
access =false;
}
if(access)
Serial.println(" Authorized access");
else
Serial.println(" Access denied");
checkAccept(access, myCard);
Serial.println();
delay(300);
}
// Screen Section
void welcomCard(){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Hello There!"); // Print a message to the LCD.
lcd.setCursor(0,1);
lcd.print("Slide your card.");
delay(500);
}
void waitChecking(){
lcd.clear();
lcd.setCursor(0,1);
lcd.print("When read a card");
lcd.setCursor(0,0);
lcd.print("Please Wait."); // Print a message to the LCD.
delay(500);
lcd.print('.');
delay(500);
lcd.print('.');
delay(500);
}
void checkAccept(bool access, int userNumber){
// نشغل الشاشة مع الترحيب
lcd.clear();
lcd.setCursor(0,0);
if( access == true ){
lcd.print(" Ok, Now Write");
lcd.setCursor(0,1);
lcd.print(" The Password ");
delay(1000);
setPassword(userNumber);
} else {
lcd.print(" OPS! ");
lcd.setCursor(0,1);
lcd.print(" Card Undefine!");
delay(1500);
}
}
void setPassword(int user){
char TFP;
lcd.clear();
lcd.setCursor(0,0);
lcd.print(User[user]);
lcd.print(" Password: ");
lcd.setCursor(0,1);
TFP = keyProcess(user);
if(TFP == 'T'){
Serial.println("Password True!");
openDoorLCD();
} else if (TFP == 'E'){
return;
} else {
Serial.println("Password False!");
tryAgain(user);
}
}
void tryAgain(int user){
char TFP;
lcd.clear();
lcd.setCursor(0,0);
lcd.print(User[user]);
lcd.print(" Try Again: ");
lcd.setCursor(0,1);
TFP = keyProcess(user);
if(TFP == 'T'){
Serial.println("Password True!");
openDoorLCD();
} else if (TFP == 'E'){
return;
} else {
Serial.println("Password False!");
tryAgain(user);
}
}
void openDoorLCD(){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Successful!");
lcd.setCursor(0,1);
lcd.print("The door opens!");
digitalWrite(ledG,HIGH);
digitalWrite(ledR,LOW);
openDoor();
}
// KeyPad Section
char keyProcess(int userID){
bool prse=true, check = false;
char inputPass[15];
Serial.print("Password: ");
for(counter = 0; prse; counter++){
inputPass[counter] = keypad.waitForKey();
Serial.print(inputPass[counter]);
if( inputPass[counter] == '#' ){
prse = false;
check = checkPass(inputPass, userID);
break;
}
else if ( inputPass[counter] == '*' ){
return 'E';
}
else
lcd.print('*');
}
Serial.println();
if( check ){
Serial.println("Door Unlock!");
return 'T';
} else {
Serial.println("Door Still Lock!");
return 'F';
}
delay(100);
}
bool checkPass( char pass[], int user ){
int passCounter=0;
for(int i=0; i<counter; i++){
if( pass[i] == UserPassword[user][i] )
passCounter++;
else
return false;
}
if( passCounter == 5 )
return true;
else
return false;
}
// Door Lock
void closeBoth(){
// Close Both
servoLock.write(175); // زاوية الاغلاق لماطور القفل
servoOpen.write(160); // زاوية الاغلق للباب
}
void openDoor(){
// Open Lock
delay(350);
digitalWrite(ledG,LOW);
delay(350);
digitalWrite(ledG,HIGH);
servoLock.write(100); // زاوية الفتح للقفل
delay(350);
digitalWrite(ledG,LOW);
delay(350);
digitalWrite(ledG,HIGH);
//Open Door
servoOpen.write(10); // زاوية الفتح للباب
for(int i=0; i<10;i++){
delay(350);
digitalWrite(ledG,LOW);
delay(350);
digitalWrite(ledG,HIGH);
}
//Close Door
servoOpen.write(180); // اغلاق الباب
delay(350);
// Close Lock
servoLock.write(175); // اغلاق القفل
}