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

#define  SS   10
#define  RST  9

LiquidCrystal_I2C lcd(0x27, 20, 4);
MFRC522 scanner(SS,RST);
SoftwareSerial mySoftwareSerial(4,3);  // RX, TX
DFRobotDFPlayerMini myDFPlayer;

const uint8_t maximum=6;
uint8_t index; 
int count=0;
String tag[6]={"9314b72d","f12fee1b","d27b920","83d2382d","9382c62d","939e3f2d"};
String goods[6]={"BISCUIT","JUICE","APPLE","CHOCOLATE","HALUA","FLOWER"};
String price[6]={"500/-","1000/-","2000/-","5000/-","3000/-","6000/-"};

void setup()
{
  // ONLY FOR SIMULATION
  pinMode(2, INPUT_PULLUP);

  lcd.init();
  lcd.backlight();
  welcomeDisplay();

  mySoftwareSerial.begin(9600);
  Serial.begin(115200);

  SPI.begin();
  scanner.PCD_Init();

  if (!myDFPlayer.begin(mySoftwareSerial)) {
    Serial.println(F("Not initialized:"));
    Serial.println(F("1. Check the DFPlayer Mini connections"));
    Serial.println(F("2. Insert an SD card"));
    //while (true);  COMMENTED FOR SIMULATION
  }
  Serial.println();
  Serial.println(F("DFPlayer Mini module initialized!"));
  // Initial settings
  myDFPlayer.volume(30);        // Volume 5
  myDFPlayer.play(11);
  init_display();
}

void loop()
{
  String tempTag=readTag();
  if(tempTag!="error")
  {
    uint8_t tempIndex=analyseTag(tempTag);
    if(tempIndex != 99)
    {
      goods_display(tempIndex);
    }
  }
  //for simulation only
  if(digitalRead(2)==LOW)
  {
    delay(500);
    goods_display(count);
    count++;
    if(count>5)
    {count=0;}
  }
}

void init_display()
{
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("SYSTEM RUNNING"); 
  lcd.setCursor(0,1);
  lcd.print("GOODS SCANNING");
}

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;
}

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


void welcomeDisplay()
{
     lcd.clear();
     lcd.setCursor(0,0);
     lcd.print("   BLIND SHOPPING  "); 
     lcd.setCursor(0,1);
     lcd.print("     ASSISTANT     "); 
     lcd.setCursor(0,2);
     lcd.print("INITIALIZING SYS");
     lcd.setCursor(0,3);
     lcd.print("PLEASE WAIT ....");
     delay(3000);
}

void goods_display(uint8_t i)
{
     lcd.clear();
     lcd.setCursor(0,0);
     lcd.print("***DETECTION INFO***"); 
     lcd.setCursor(0,1);
     lcd.print("GOODS:"); 
     lcd.setCursor(7,1);
     lcd.print(goods[i]);  
     lcd.setCursor(0,2);
     lcd.print("PRICE:"); 
     lcd.setCursor(7,2);
     lcd.print(price[i]); 
     lcd.setCursor(0,3);
     lcd.print("******END INFO******");  
     //myDFPlayer.play(i); comment for simulation
}
mp3-player-moduleBreakout
rfid-rc522Breakout