-
Notifications
You must be signed in to change notification settings - Fork 0
/
door-lock-system-code.cpp
95 lines (82 loc) · 2.04 KB
/
door-lock-system-code.cpp
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
//Include required libraries
#include <LiquidCrystal_I2C.h>
#include <SPI.h>
#include <MFRC522.h>
#include <Servo.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;
String tagUID = "/*enter your tag’s UID*/"; // String to store UID of tag. Change it with your tag's UID
void setup() {
// Arduino Pin configuration
pinMode(buzzerPin, OUTPUT);
pinMode(redLed, OUTPUT);
pinMode(greenLed, OUTPUT);
sg90.attach(servoPin); //Declare pin 9 for servo
sg90.write(0); // Set initial position at 90 degrees
lcd.begin(); // LCD screen
lcd.backlight();
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522
lcd.clear();
}
void loop() {
lcd.setCursor(0, 0);
lcd.print(" RFID Door Lock");
lcd.setCursor(0, 1);
lcd.print(" Show 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 i = 0; i < mfrc522.uid.size; i++)
{
tag.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
tag.concat(String(mfrc522.uid.uidByte[i], HEX));
}
tag.toUpperCase();
//Checking the card
if (tag.substring(1) == tagUID) //change here the UID of the card/cards that you want to give access
{
// If UID of tag is matched.
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Access Granted");
lcd.setCursor(0, 1);
lcd.print("Door Opened");
sg90.write(90);
digitalWrite(greenLed, HIGH);
delay(3000);
digitalWrite(greenLed, LOW);
sg90.write(0);
lcd.clear();
}
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();
}
}