/*
  Voltaat learn (http://learn.voltaat.com)
  Link for full tutorial:
  RFID reader library: https://learn.voltaat.com/wp-content/uploads/2023/04/rfid-master.zip
  Tutorial: RFID Lock System With Servo Using Arduino
  The purpose of this sketch is to open and close a door with a servo motor
  when a unique card is brought near the RFID module.
  
  Connections from the Arduino to the breadboard:
  • Arduino GND pin → Breadboard ground line
  
  • Arduino 5V pin → Breadboard 5V line
  
  
  Connections from the RFID reader to the Arduino:
  
   
  • RFID 3.3V pin → Arduino VCC (3.3V) pin
  
  • RFID RST pin → Arduino pin 9
  
  • RFID GND pin → Breadboard ground line
  
  • RFID IRQ pin → unconnected
  
  • RFID MISO pin → Arduino pin 12
  
  • RFID MOSI pin → Arduino pin 11
  
  • RFID SCK pin → Arduino pin 13
  
  • RFID SDA pin → Arduino pin 10
  
  
  
  Connections from the servo motor:
  
  • Servo GND pin→ breadboard GND line
  
  • Servo 5V pin→ breadboard 5V line
  
  • Servo signal pin → Arduino pin 3
  
  
  
  Connections from the passive buzzer :
  
  • passive buzzer GND pin → Breadboard ground line
  
  • passive buzzer VCC pin (+ pin) → Arduino pin 6
  
  
  
  Connections from the Green LED :
  
  • Green LED anode pin → Arduino pin 5
  
  • Green LED cathode pin → 330 ohm resistor first pin → Breadboard ground line
  
  
  Connections from the Red LED :
  
  • Red LED anode pin → Arduino pin 7
  
  • Red LED cathode pin → 330 ohm resistor first pin → Breadboard ground line
  
*/
 // MFRC522 Library  
 // https://github.com/miguelbalboa/rfid  
 #include "SPI.h"  
 #include "MFRC522.h"  
 #include "Servo.h"  
 #include "Wire.h"   
 #include "LiquidCrystal_I2C.h" 
 LiquidCrystal_I2C lcd(0x27,16,2);  
 Servo s1;  
 #define SS_PIN 10  
 #define RST_PIN 9  
 #define LED_G 5  //define green LED pin  
 #define LED_R 7  //define red LED  
 #define BUZZER 6 //buzzer pin  
 MFRC522 mfrc522(SS_PIN, RST_PIN);  // Create MFRC522 instance.  
 //Servo myServo; //define servo name  
 void setup()   
 {  
  Serial.begin(9600);  // Initiate a serial communication  
  SPI.begin();     // Initiate SPI bus  
   lcd.init();             
  lcd.backlight();  
 ;  
  mfrc522.PCD_Init();  // Initiate MFRC522  
  s1.attach(3); //servo pin  
  // myServo.write(0); //servo start position  
  pinMode(LED_G, OUTPUT);  
  pinMode(LED_R, OUTPUT);  
  pinMode(BUZZER, OUTPUT);  
  noTone(BUZZER);  
  Serial.println("Put your card to the reader...");  
  Serial.println();  
  lcd.setCursor(0,0);  
  lcd.print(" Put your card   ");  
 }  
 void loop()   
 {  
  // 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) == "10 A6 DC 51") //change here the UID of the card/cards that you want to give access  
  {  
   Serial.println("Authorized access");  
   Serial.println();  
     lcd.setCursor(0,0);  
     lcd.print(" CARD IS VALID      ");  
     lcd.setCursor(0,1);  
     lcd.print("Opening the Door   ");  
     digitalWrite(LED_G, HIGH); //Green LED ON  
     s1.write(0);  
     delay(3000);  
     s1.write(90);  
     lcd.setCursor(0,1);  
     lcd.print("closing the Door   ");  
     digitalWrite(LED_G, LOW);  //Green LED OFF  
     delay(2000);  
     lcd.setCursor(0,0);  
     lcd.print(" Put your card   ");  
     lcd.setCursor(0,1);  
     lcd.print("                ");  
  }  
  else   
  {  
   Serial.println("CARD IS INVALID");  
    lcd.setCursor(0,1);  
    lcd.print("CARD IS INVALID   ");  
    digitalWrite(LED_R, HIGH);   // Red LED ON  
    tone(BUZZER, 300);       // Buzzer ON  
    delay(2000);  
    digitalWrite(LED_R, LOW);  
    noTone(BUZZER);  
    lcd.setCursor(0,1);  
    lcd.print("               ");  
  }  
 }