-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDoor_Lock_System.ino
162 lines (143 loc) · 4.35 KB
/
Door_Lock_System.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
// Include required libraries
#include <MFRC522.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#include <Servo.h>
#include <SPI.h>
// Create instances
LiquidCrystal_I2C lcd(0x27, 16, 2);
MFRC522 mfrc522(10, 9); // MFRC522 mfrc522(SS_PIN, RST_PIN)
Servo sg90;
// Initialize Pins for led's, servo and buzzer
// Blue LED is connected to 5V
constexpr uint8_t greenLed = 7;
constexpr uint8_t redLed = 6;
constexpr uint8_t servoPin = 8;
constexpr uint8_t buzzerPin = 5;
char initial_password[4] = {'1', '2', '3', '4'}; // Variable to store initial password
String tagUID = "33 F4 6E 03"; // String to store UID of tag. Change it with your tag's UID
char password[4]; // Variable to store users password
boolean RFIDMode = true; // boolean to change modes
char key_pressed = 0; // Variable to store incoming keys
uint8_t i = 0; // Variable used for counter
// defining how many rows and columns our keypad have
const byte rows = 4;
const byte columns = 4;
// Keypad pin map
char hexaKeys[rows][columns] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
// Initializing pins for keypad
byte row_pins[rows] = {A0, A1, A2, A3};
byte column_pins[columns] = {2, 1, 0};
// Create instance for keypad
Keypad keypad_key = Keypad( makeKeymap(hexaKeys), row_pins, column_pins, rows, columns);
void setup() {
// Arduino Pin configuration
pinMode(buzzerPin, OUTPUT);
pinMode(redLed, OUTPUT);
pinMode(greenLed, OUTPUT);
sg90.attach(servoPin); //Declare pin 8 for servo
sg90.write(0); // Set initial position at 90 degrees
lcd.begin(16,2); // LCD screen
lcd.backlight();
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522
lcd.clear(); // Clear LCD screen
}
void loop() {
// System will first look for mode
if (RFIDMode == true) {
lcd.setCursor(0, 0);
lcd.print(" Door Lock");
lcd.setCursor(0, 1);
lcd.print(" Scan Your Tag ");
// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent()) {
return;
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial()) {
return;
}
//Reading from the card
String tag = "";
for (byte j = 0; j < mfrc522.uid.size; j++)
{
tag.concat(String(mfrc522.uid.uidByte[j] < 0x10 ? " 0" : " "));
tag.concat(String(mfrc522.uid.uidByte[j], HEX));
}
tag.toUpperCase();
//Checking the card
if (tag.substring(1) == tagUID)
{
// If UID of tag is matched.
lcd.clear();
lcd.print("Tag Matched");
digitalWrite(greenLed, HIGH);
delay(3000);
digitalWrite(greenLed, LOW);
lcd.clear();
lcd.print("Enter Password:");
lcd.setCursor(0, 1);
RFIDMode = false; // Make RFID mode false
}
else
{
// If UID of tag is not matched.
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Wrong Tag Shown");
lcd.setCursor(0, 1);
lcd.print("Access Denied");
digitalWrite(buzzerPin, HIGH);
digitalWrite(redLed, HIGH);
delay(3000);
digitalWrite(buzzerPin, LOW);
digitalWrite(redLed, LOW);
lcd.clear();
}
}
// If RFID mode is false, it will look for keys from keypad
if0 (RFIDMode == false) {
key_pressed = keypad_key.getKey(); // Storing keys
if (key_pressed)
{
password[i++] = key_pressed; // Storing in password variable
lcd.print("*");
}
if (i == 4) // If 4 keys are completed
{
delay(200);
if (!(strncmp(password, initial_password, 4))) // If password is matched
{
lcd.clear();
lcd.print("Pass Accepted");
sg90.write(100); // Door Opened
digitalWrite(greenLed, HIGH);
delay(3000);
digitalWrite(greenLed, LOW);
sg90.write(0); // Door Closed
lcd.clear();
i = 0;
RFIDMode = true; // Make RFID mode true
}
else // If password is not matched
{
lcd.clear();
lcd.print("Wrong Password");
digitalWrite(buzzerPin, HIGH);
digitalWrite(redLed, HIGH);
delay(3000);
digitalWrite(buzzerPin, LOW);
digitalWrite(redLed, LOW);
lcd.clear();
i = 0;
RFIDMode = true; // Make RFID mode true
}
}
}
}