#include <SPI.h>
#include <MFRC522.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <Servo.h>
#define RST_PIN 5 // Configurable, see typical pin layout above
#define SS_PIN 53 // Configurable, see typical pin layout above
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance
LiquidCrystal_I2C lcd(0x27,16,2);
String uid1 = "53 2C 91 0D";
String uid2 = "32 D7 46 1E";
Servo myservo1;
int IR1 = 2;
int IR2 = 4;
int Slot = 4; //Enter Total number of parking Slots
int flag1 = 0;
int flag2 = 0;
int A = 0;
void setup()
{
lcd.backlight();
pinMode(IR1, INPUT);
pinMode(IR2, INPUT);
myservo1.attach(3);
myservo1.write(0);
lcd.init();
lcd.init();
Serial.begin(9600); // Initiate a serial communication
SPI.begin(); // Initiate SPI bus
mfrc522.PCD_Init(); // Initiate MFRC522
Serial.println("Approximate your card to the reader...");
Serial.println();
}
void loop()
{
String content= "";
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));
}
String allowedUIDs[] = {"uid1", "uid2"};
int numAllowedUIDs = sizeof(allowedUIDs) / sizeof(allowedUIDs[0]);
bool uidMatched = false;
for (int i = 0; i < numAllowedUIDs; i++) {
if (content == allowedUIDs[i])
{
uidMatched = true;
break;
}
}
// Reset the loop if no new card present on the sensor/reader. This saves the entire process when idle.
if ( ! mfrc522.PICC_IsNewCardPresent())
{
return;
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial())
{
return;
}
Serial.print("UID tag :");
byte letter;
content.toUpperCase();
if(digitalRead (IR1) == LOW && flag1==0)
{
if(Slot>0)
{
if (uidMatched && A==0)
{
flag1=1;
A=1;
if(flag2==0)
{
lcd.setCursor (0,0);
lcd.print("Welcome Mr.Ali");
myservo1.write(90); Slot = Slot-1;
}
}
else
{
Serial.println(" Access Denied");
lcd.setCursor(3,0);
lcd.clear();
lcd.print("Unknown");
lcd.setCursor(0,1);
lcd.print("Access Denied");
delay(3000);
lcd.clear();
}
}
else
{
lcd.setCursor (0,0);
lcd.print(" SORRY :( ");
lcd.setCursor (0,1);
lcd.print(" Parking Full ");
delay (3000);
lcd.clear();
}
}
if(digitalRead (IR2) == LOW && flag2==0)
{
flag2=1;
if (uidMatched && A==1)
{
if(flag1==0)
{
lcd.setCursor (0,0);
lcd.print("Goodbye Mr.Ali");
myservo1.write(90); Slot = Slot+1;
}
}
}
if(flag1==1 && flag2==1)
{
delay (1000);
myservo1.write(0);
flag1=0, flag2=0;
}
lcd.setCursor(0,0);
lcd.print(" Scan Tag ");
lcd.setCursor (0,1);
lcd.print("Slot Left: ");
lcd.print(Slot);
delay(1000);
}