#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
bool firstSignalReceived = true;
bool secondSignalReceived = true;
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(4,0);
lcd.print("Scan Tag");
lcd.setCursor (2,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)
{
if(Slot>0)
{
if (secondSignalReceived)
{
if(content.substring(1) == uid1)
{
servo_A2.write(0);
Slot = Slot-1;
lcd.setCursor(0,0);
lcd.print("Hello Mr.Ahmed");
delay(2000);
servo_A2.write(90);
}
else
{
Serial.println(" Access Denied");
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Unknown");
lcd.setCursor(0,1);
lcd.print("Access Denied");
delay(2000);
lcd.clear();
servo_A2.write(90);
}
}
}
else
{
lcd.setCursor (0,0);
lcd.print(" SORRY :( ");
lcd.setCursor (0,1);
lcd.print(" Parking Full ");
delay (3000);
lcd.clear();
}
firstSignalReceived = false;
}
if(digitalRead (IR2) == LOW)
{
if (firstSignalReceived)
{
secondSignalReceived = false;
servo_A2.write(0);
delay(2000);
servo_A2.write(90);
}
}
delay(3000);
return
/*if(firstSignalReceived == false)
{
if(secondSignalReceived == false)
{
delay(2000);
secondSignalReceived = true;
firstSignalReceived = true;
}
}*/
}