//#include <SPI.h> 
//#include <MFRC522.h> 
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
#include "DFRobotDFPlayerMini.h"

#define  SS   5
#define  RST  14

LiquidCrystal_I2C lcd(0x27, 20, 4);
//MFRC522 scanner(SS,RST);
DFRobotDFPlayerMini myDFPlayer;

const uint8_t maximum=6;
//uint8_t index;
String tag[6]={"xx","xx","xx","xx","xx","xx"};
String password[6]={"xx","xx","xx","xx","xx","xx"};

void setup()
{
  Serial.begin(9600);
  Serial.println("setup in progress");
  lcd.init();
  lcd.backlight();
  welcomeDisplay();
  Serial2.begin(9600);
 
  //SPI.begin();
  //scanner.PCD_Init();

  if (!myDFPlayer.begin(Serial2)) {
    Serial.println(F("Not initialized:"));
    Serial.println(F("1. Check the DFPlayer Mini connections"));
    Serial.println(F("2. Insert an SD card"));
    //while (true);
  }
  Serial.println();
  Serial.println(F("DFPlayer Mini module initialized!"));
  // Initial settings
  myDFPlayer.volume(30);        // Volume 5
  myDFPlayer.play(5);
  welcomeDisplay();
  lcd.clear();
  Serial.println("setup done");
}

void loop()
{
  init_display();
  String tempTag=readTag();
  if(tempTag!="error")
  {
    uint8_t tempIndex=analyseTag(tempTag);
    if(tempIndex != 99)
    {
      tag[tempIndex]="xx";
      Serial.print(String(tempIndex+1));
      delay(7000);
    }
    else
    {
      uint8_t tempIndex2=offer_room(tempTag);
      if(tempIndex2 != 99)
      {
        Serial.print(String(tempIndex2+1));
      }
    }
    lcd.clear();
  }
}

void init_display()
{
  lcd.setCursor(0,0);
  lcd.print("--------<<>>--------");
  lcd.setCursor(0,1);
  lcd.print("Scan to be Granted/");
  lcd.setCursor(0,2);
  lcd.print(" Unlock Locker Room ");
  lcd.setCursor(0,3);
  lcd.print("--------<<>>--------");
}

String readTag()
{
   /* if(!scanner.PICC_IsNewCardPresent())
    {
      //Serial.println("no card present");
      return "error";
    }
    if(!scanner.PICC_ReadCardSerial())
    {
      //Serial.println("card not readable");
      return "error";
    }
    //save data and return output
    String myTag;
    for (int i = 0; i < scanner.uid.size; i++) 
    {
      myTag += String(scanner.uid.uidByte[i],HEX);
    }
    //Serial.print("THE TAG DETECTED IS:");
    //Serial.println(myTag);
    return myTag;*/
    String myTag;
    if(Serial.available())
    {
      myTag=Serial.readString();
      return myTag;
    }
    else
    {
      return "error";
    }   
}

unsigned int analyseTag(String tagId)
{
  for(uint8_t i=0;i<6;i++)
  {
    if(tagId==tag[i])
    {
      result1_display(i);
      return i;   
    }
  }
  result2_display();
  return 99;  
}

uint8_t offer_room(String tagId)
{
  for(uint8_t i=0;i<6;i++)
  {
    if(tag[i]=="xx")
    {
      tag[i]=tagId;
      result3_display(i);
      return i;       
    }
  }
  result4_display();
  return 99;   
}

void welcomeDisplay()
{
     lcd.clear();
     lcd.setCursor(0,0);
     lcd.print("SECURE ACCESS LOCKER"); 
     lcd.setCursor(0,1);
     lcd.print("       SYSTEM      "); 
     lcd.setCursor(0,2);
     lcd.print("SYSTEM INITIALIZE");
     lcd.setCursor(0,3);
     lcd.print("PLEASE WAIT");
     for(uint8_t i=11;i<20;i++)
     {
      lcd.setCursor(i,3);
      lcd.print("_");
      delay(500);
     }
}

void result1_display(uint8_t i)
{
  myDFPlayer.play((i+7));
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("--------<<>>--------");
  lcd.setCursor(0,1);
  lcd.print("Take your belongings");
  lcd.setCursor(0,2);
  lcd.print("from room number:");
  lcd.setCursor(17,2);
  lcd.print(String(i+1));
  lcd.setCursor(0,3);
  lcd.print("--------<<>>--------");
}

void result2_display()
{
  myDFPlayer.play(13);
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("--------<<>>--------");
  lcd.setCursor(0,1);
  lcd.print("Serching for Room in");
  lcd.setCursor(0,2);
  lcd.print("locker.Please wait..");
  lcd.setCursor(0,3);
  lcd.print("--------<<>>--------");
  delay(3000);
}

void result3_display(uint8_t i)
{
  uint8_t num;
  if(i>=0 && i<=1){num=14+i;}
  else if(i>1 && i<6){num=i-1;}
  myDFPlayer.play(num);
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("--------<<>>--------");
  lcd.setCursor(0,1);
  lcd.print("Place ur belongings");
  lcd.setCursor(0,2);
  lcd.print("in room number:");
  lcd.setCursor(15,2);
  lcd.print(String(i+1));
  lcd.setCursor(0,3);
  lcd.print("--------<<>>--------");
  delay(3000);
}

void result4_display()
{
  myDFPlayer.play(6);
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("--------<<>>--------");
  lcd.setCursor(0,1);
  lcd.print("Soory!,Locker Loaded");
  lcd.setCursor(0,2);
  lcd.print("Please Retry Later");
  lcd.setCursor(0,3);
  lcd.print("--------<<>>--------");
  delay(6000);
}
mp3-player-moduleBreakout
rfid-rc522Breakout