#include <SPI.h>
#include <MFRC522.h>
#include <LiquidCrystal_I2C.h>
#define RST_PIN 9
#define SS_PIN 10
LiquidCrystal_I2C lcd(0x27, 16, 2);
MFRC522 mfrc522(SS_PIN, RST_PIN);
String read_rfid;
String ok_rfid_1="4accfea";
String ok_rfid_2="3918c463";
int lock = 7;
int lock2 = 7;
void setup() {
Serial.begin(9600);
lcd.init();
home();
lcd.backlight();
while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 card
pinMode(lock, OUTPUT);
pinMode(lock2, OUTPUT);
}
void dump_byte_array(byte *buffer, byte bufferSize) {
read_rfid="";
for (byte i = 0; i < bufferSize; i++) {
read_rfid=read_rfid + String(buffer[i], HEX);
}
}
void open_lock() {
digitalWrite(lock,HIGH);
lcd.clear();
lcd.backlight();
lcd.setCursor(2, 1);
lcd.print("OK Open The Door");
delay(3000);
digitalWrite(lock,LOW);
lcd.clear();
lcd.backlight();
lcd.setCursor(3, 1);
lcd.print("Close The Door");
home();
}
void close_lock2() {
digitalWrite(lock2, HIGH);
lcd.clear();
lcd.backlight();
lcd.setCursor(3, 1);
lcd.print("OK Open The Door");
delay(3000);
digitalWrite(lock2,LOW);
lcd.clear();
lcd.backlight();
lcd.setCursor(3, 1);
lcd.print("Close The Door");
home();
}
void home(){
lcd.clear();
lcd.setCursor(1, 0);
lcd.print("Please put Keycard");
lcd.setCursor(4, 1);
lcd.print("<<<<---->>>>");
lcd.setCursor(5, 2);
lcd.print("<<<---->>>");
lcd.setCursor(6, 3);
lcd.print("<<---->>");
}
void loop() {
// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent())
return;
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial())
return;
dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
Serial.println(read_rfid);
if (read_rfid==ok_rfid_1) {
//ok, open the door.
open_lock();
}
Serial.println(read_rfid);
if (read_rfid==ok_rfid_2) {
//ok, open the door.
close_lock2();
}
}