#include <SPI.h>
#include <MFRC522.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
int led1=6;
int led2=7;
int buzzer=3;
int dhanesh;
int pir=4;
int Detect= LOW;//whether detected as per state of sensor or not
int count=0;
int val_of_outPin=0;
#define RST_PIN 9
#define SS_PIN 10
byte readCard[4];
String MasterTag = "935D356"; // REPLACE this Tag ID with your Tag ID!!!
String tagID = "";
// Create instances
MFRC522 mfrc522(SS_PIN, RST_PIN);
void setup()
{
lcd.init();
lcd.backlight();
SPI.begin(); // SPI bus
mfrc522.PCD_Init(); // MFRC522
pinMode(led1,OUTPUT);
pinMode(led2,OUTPUT);
pinMode(buzzer,OUTPUT);
pinMode(pir,INPUT);
lcd.clear();
lcd.print(" Access Control ");
lcd.setCursor(0, 1);
lcd.print("Scan Your Card>>");
}
void loop()
{
Serial.begin(9600);
//Wait until new tag is available
val_of_outPin= digitalRead(pir);
if(val_of_outPin==HIGH){
if(Detect==LOW){
count=count+1;
Serial.print(count);
Serial.println(" Motions Detected");
Detect=HIGH;
}
while (getID())
{
lcd.clear();
lcd.setCursor(0, 0);
if (tagID == MasterTag)
{
lcd.print(" Access Granted!");
digitalWrite(led1,HIGH);
digitalWrite(led2,LOW);
tone(buzzer,4000);
delay(500);
noTone(buzzer);
// You can write any code here like opening doors, switching on a relay, lighting up an LED, or anything else you can think of.
}
else
{
digitalWrite(led2,HIGH);
digitalWrite(led1,LOW);
lcd.print(" Access Denied!");
tone(buzzer,1000);
delay(500);
noTone(buzzer);
delay(500);
tone(buzzer,1000);
delay(500);
noTone(buzzer);
delay(500);
tone(buzzer,1000);
delay(500);
noTone(buzzer);
delay(500);
tone(buzzer,1000);
delay(500);
noTone(buzzer);
delay(500);
tone(buzzer,1000);
delay(500);
noTone(buzzer);
}
lcd.setCursor(0, 1);
lcd.print(" ID : ");
lcd.print("confidential");
delay(2000);
digitalWrite(led2,LOW);
digitalWrite(led1,LOW);
lcd.clear();
lcd.print(" WELCOME HOME! ");
lcd.setCursor(0, 1);
lcd.print("Scan Your Card>>");
}
}else{
if(Detect==HIGH){
Serial.println("Motion Ended");
Detect= LOW;
}
lcd.noBacklight};
//Read new tag if available
boolean getID()
{
// Getting ready for Reading PICCs
if ( ! mfrc522.PICC_IsNewCardPresent()) { //If a new PICC placed to RFID reader continue
return false;
}
if ( ! mfrc522.PICC_ReadCardSerial()) { //Since a PICC placed get Serial and continue
return false;
}
tagID = "";
for ( uint8_t i = 0; i < 4; i++) { // The MIFARE PICCs that we use have 4 byte UID
//readCard[i] = mfrc522.uid.uidByte[i];
tagID.concat(String(mfrc522.uid.uidByte[i], HEX)); // Adds the 4 bytes in a single String variable
}
tagID.toUpperCase();
mfrc522.PICC_HaltA(); // Stop reading
return true;
}