﻿#include <SPI.h>
#include <MFRC522.h> // rfid library
#include <Servo.h>
#include <LiquidCrystal_I2C.h> // lcd library
 
#define SS_PIN 10 // rfid pin
#define RST_PIN 9 // rfid pin
MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance.
Servo myServo; //servo 


LiquidCrystal_I2C lcd(0x27, 16, 2);


void setup() 
{
  Serial.begin(9600);
  SPI.begin();
  
  lcd.init();
  lcd.backlight();
  
  mfrc522.PCD_Init();   // Initiate MFRC522
  myServo.attach(3); //servo pin
  myServo.write(0); //servos start position
  Serial.println("Put your card to the reader...");
  Serial.println();


}
void loop() 
{
  lcd.clear(); // clear data on lcd
  lcd.setCursor(0,0);
  lcd.print("Make Payment"); // prints to lcd
  lcd.clear();
  
  // Look for new cards
  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));
  }
  Serial.println();
  Serial.print("Message : ");
  content.toUpperCase();
  if (content.substring(1) == "35 33 03 15") //card that gets access for gate
  {
    Serial.println("Authorized");
    Serial.println();
    lcd.clear();
    lcd.print("Approved"); // printing to screen
    delay(200);
    myServo.write(30);
    delay(3000);
    myServo.write(90);
    lcd.clear();
  }
 
 else   {
    Serial.println(" Access denied");
    lcd.clear();
    lcd.print("Denied");
    delay(1000);
    lcd.clear(); // clears everything on lcd screen
  }
}