#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 servo_A2;
int IR1 = 2;
int IR2 = 4;
int Slot = 4; //Enter Total number of parking Slots
int flag1 = 0;
int flag2 = 0;
int B = 0;
void setup()
{
pinMode(IR1, INPUT);
pinMode(IR2, INPUT);
servo_A2.attach(A2);
servo_A2.write(90);
lcd.init();
lcd.init();
lcd.backlight();
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()
{
servo_A2.write(90);
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" Scan Tag ");
lcd.setCursor (0,1);
lcd.print("Slot Left: ");
lcd.print(Slot);
// 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;
}
//Show UID on serial monitor
Serial.print("UID tag :");
String content= "";
byte letter;
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));
}
lcd.clear();
Serial.println();
Serial.print("Message : ");
content.toUpperCase();
if(digitalRead (IR1) == LOW && flag1==0 )
{
if(Slot>0)
{
B=1;
flag1=1;
if(flag2==0 && content.substring(1) == uid1 && B==0 )
{
servo_A2.write(0);
Slot = Slot-1;
lcd.setCursor(0,0);
lcd.print("Hello Mr.Ahmed");
}
}
else
{
lcd.setCursor (0,0);
lcd.print(" SORRY :( ");
lcd.setCursor (0,1);
lcd.print(" Parking Full ");
delay (3000);
lcd.clear();
}
}
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();
servo_A2.write(90);
}
if(digitalRead (IR2) == LOW && flag2==0 )
{
flag2=1;
if(flag1==0 && content.substring(1) == uid1 && B==1)
{
servo_A2.write(0);
Slot = Slot+1;
lcd.setCursor(0,0);
lcd.print("Goodbye Mr.Ahmed");
}
}
if(flag1==1 && flag2==1)
{
delay (1000);
servo_A2.write(90);
flag1=0, flag2=0;
}
}