#include <SPI.h>       // Include SPI comms library, RFID reader MFRC library, and EEPROM memory library
#include <MFRC522.h>
#include <EEPROM.h>
#define SS_PIN 10      // Pins 10, 9, 13, 11, and 12 are used for MFRC522 reader
#define RST_PIN 9
#define prg 5          // program jumper "prg" 
int data = 0;          // int data to int data 19 are used for storing card data
int data1 = 0;
int data2 = 0;
int data3 = 0;
int data4 = 0;
int data5 = 0;
int data6 = 0;
int data7 = 0;
int data8 = 0;
int data9 = 0;
int data10 = 0;
int data11 = 0;
int data12 = 0;
int data13 = 0;
int data14 = 0;
int data15 = 0;
int data16 = 0;
int data17 = 0;
int data18 = 0;
int data19 = 0;
int count = 0;      // This ineger acts as a register to count a number of bires when writing to EEPROM
int b = 0;         
int compare[4] = {0,0,0,0};  //  This array of four bites is used in scanning mode.  When a card is read, four bites of card data is held here.
int val = 0;
#define OUT1 0      // OUT1 and OUT2 act to output card data (1x-5x pulses for saved cards 1 thropugh 5, and 6x pulses for an incorrect card)
#define OUT2 1
boolean state = true;  // Determines whether the user is in scanning mode or program mode
MFRC522 mfrc522(SS_PIN, RST_PIN);   //Declare MRRC522 chip select and reset pin
 
void setup() 
{
  SPI.begin();      // Initiate  SPI bus    
  mfrc522.PCD_Init();   // Initiate MFRC522
  pinMode(prg,INPUT);   // prg jumper is an input
  pinMode(A0,OUTPUT);  //green led output
  pinMode(A1,OUTPUT);  //red led output
   pinMode(OUT1,OUTPUT);  // OUT1 is an output
  pinMode(OUT2,OUTPUT);  //OUT2 is an output
  if(digitalRead(prg) == HIGH)  // if the PRG is connected to 5v, then we'll enter into scanning mode.  If LOW (0v), program mode
  {
    state = false; // SCANNING MODE
    readcard();    // load all of the 5x program card data from EEPROM memory (See this function below)
  }
}

void loop()  // The void_loop is very simple.  Either continue to call the scanning mode function, or the savecards function.  
{
  if(state == false)  
  {
    scanningmode();
  }
  else
  {
    savecards();
  }
}


void savecards() // This function will acts to read and save 5x cards into EEPROM memory.  Once the fifth card has been read, the program freezes.  
// From there, power down, switch the prg jumper, and power back up again.
{
  if ( ! mfrc522.PICC_IsNewCardPresent()) // The following if statements wait for a card to be read
  {
    return;
  }
  if ( ! mfrc522.PICC_ReadCardSerial()) 
  {
    return;
  }
  String content= "";
  byte letter;                            
  ///////////////////////////////////////////////  At this point the card bas been read
  if(count == 0) // if the card is the first card being read, do the following
  {
  for (byte i = 0; i < mfrc522.uid.size; i++)   // This FOR statement places four bites of card data into EEPROM memory.  We write to EEPROM slots 0-3
  {
       EEPROM.write(i,mfrc522.uid.uidByte[i]);
  }
 digitalWrite(A0,HIGH);  // Once done, turn on the green LED for one second (1000ms), then turn it off.  This is a visual indicator.
 delay(1000);
 digitalWrite(A0,LOW);
 count = 1;              // count is now 1, which means that after the next card is read, it will be written to the next area in memory (slots 4 to 7)
  }
  else if(count == 1)    // If after the card was read, count = 1, then we'll write the card data to the second slot of EEPROM data
  {
   b = 4;   // b is important.  It tells us that we're going to start writing to EEPROM slot 4
   for (byte i = 0; i < mfrc522.uid.size; i++) 
  {
       EEPROM.write(b,mfrc522.uid.uidByte[i]);  // After every byte is written to EEPROM, b = b+1 (b++), so we'll write eeprom slots 4, 5 ,6, 7
       b++;
  }
 digitalWrite(A0,HIGH);  // once done, turn the green led on for a second, and then turn it off.  
 delay(1000);
 digitalWrite(A0,LOW);
 count = 2;              // Count is now 2, so after the next card is read, we'll write from slots 8, 9, 10, and 11.  The rest of this function repeats the same thing.
  }
   else if(count == 2)
  {
   b = 8;
   for (byte i = 0; i < mfrc522.uid.size; i++) 
  {
       EEPROM.write(b,mfrc522.uid.uidByte[i]);
       b++;
  }
 digitalWrite(A0,HIGH);
 delay(1000);
 digitalWrite(A0,LOW);
 count = 3;             // Count is now 3, we'll now write EEPROM slots 12, 13, 14, and 15.
  }
   else if(count == 3)
  {
   b = 12;
   for (byte i = 0; i < mfrc522.uid.size; i++) 
  {
       EEPROM.write(b,mfrc522.uid.uidByte[i]);
       b++;
  }
 digitalWrite(A0,HIGH);
 delay(1000);
 digitalWrite(A0,LOW);
 count = 4;              // Count is now 4, we'll now write EEPROM slots 16, 17, 18, and 19.
  }
   else if(count == 4)
  {
   b = 16;
   for (byte i = 0; i < mfrc522.uid.size; i++) 
  {
       EEPROM.write(b,mfrc522.uid.uidByte[i]);
       b++;
  }
 digitalWrite(A0,HIGH);  // All five cards have now been read, and we will now turn on the green LED.  From here, we're done saving cards.  Nothing more happens here.
 delay(1000);
  state = true;
  while(state == true){   // Wait until power down.  The program will remain stuck here until power down.
    {
      digitalWrite(A0,HIGH);
    }
  }
  }
  }
  
 

  
void scanningmode()  // As long as you have the jumper in scanning mode, we'll repeat this function
{
 if ( ! mfrc522.PICC_IsNewCardPresent())  // The following lines of code waits for a card to be read, and held.
  {
    return;
  }
  if ( ! mfrc522.PICC_ReadCardSerial()) 
  {
    return;
  }
  String content= "";
  byte letter;
  ////////////////////////////////////////////////////////////////////
  for (byte i = 0; i < mfrc522.uid.size; i++) 
  {
       compare[i] = (mfrc522.uid.uidByte[i]);     // Save four bites to array compare 0, 1, 2 ,3 - four bites of card data
  }
  delay(100);                       // Once read, wait 100ms
  if(compare[0] == data)            // Compare the values of compare0 to data integer.  If they equal one another, check data1
  { 
    if(compare[1] == data1)         // If data = compare0, and data1 = compare1, then compare compare2 to data2
  {
    if(compare[2] == data2)         // If data2 = compare2, and data1 = compare1, and data = compare, then compare data3 to compare3
  {
    if(compare[3] == data3)         // If all four compare bites equal the saved data register, then the we have read the first saved card!~
  {
    digitalWrite(OUT1,HIGH);        // If we've read card#1, then let's send this data to the output.  OUT1 will be turned on until we've sent pulses on OUT2
    delay(5);                       // Wait 5ms
    pulse();                        // Call the pulse function.   This sends one short pulse to the OUT2 pin.
    digitalWrite(OUT1,LOW);         // Once done, turn OUT1 off.
    digitalWrite(A0,HIGH);          // Turn the greenled on for one second, and then turn it off
    delay(1000);
    digitalWrite(A0,LOW);
  }
  }
  }
  }
  else if(compare[0] == data4)     //  If the card data did not match card#1, then let's do the same thing all over again and compare our read card data to saved card#2
  {
    if(compare[1] == data5)
  {
    if(compare[2] == data6)
  {
    if(compare[3] == data7)
  {
    digitalWrite(OUT1,HIGH);       // Yay!  We've read card#2.  Let's do the same thing above, only this point we will call the pulse function twice to send TWO pulses to OUT2
    delay(5);
    pulse();
    pulse();
    digitalWrite(OUT1,LOW);
    digitalWrite(A0,HIGH);
    delay(1000);
    digitalWrite(A0,LOW);
  }
  }
  }
  }
    else if(compare[0] == data8)  //  If the card data did not match card#1/2, then let's do the same thing all over again and compare our read card data to saved card#3
  {
    if(compare[1] == data9)
  {
    if(compare[2] == data10)
  {
    if(compare[3] == data11)
  {
    digitalWrite(OUT1,HIGH);     // Yay!  We've read card#3.  Let's do the same thing above, only this point we will call the pulse function thrice to send 3 pulses to OUT2
    delay(5);
    pulse();
    pulse();
    pulse();
    digitalWrite(OUT1,LOW);
    digitalWrite(A0,HIGH);
    delay(1000);
    digitalWrite(A0,LOW);
  }
  }
  }
  }
    else if(compare[0] == data12) // If the card data did not match card#1/2/3, then let's do the same thing all over again and compare our read card data to saved card#4
  {
    if(compare[1] == data13)
  {
    if(compare[2] == data14)
  {
    if(compare[3] == data15)
  {
    digitalWrite(OUT1,HIGH);  //We've read card#4.  Let's do the same thing above, only this point we will call the pulse function four times to send 4 pulses to OUT2
    delay(5);
    pulse();
    pulse();
    pulse();
    pulse();
    digitalWrite(OUT1,LOW);
    digitalWrite(A0,HIGH);
    delay(1000);
    digitalWrite(A0,LOW);
  }
  }
  }
  }
    else if(compare[0] == data16) // If the card data did not match card#1/2/3/4, then let's do the same thing all over again and compare our read card data to saved card#5
  {
    if(compare[1] == data17)
  {
    if(compare[2] == data18)
  {
    if(compare[3] == data19)
  {
     digitalWrite(OUT1,HIGH);  //We've read card#5.  Let's do the same thing above, only this point we will call the pulse function five times to send 5 pulses to OUT2
    delay(5);
    pulse();
    pulse();
    pulse();
    pulse();
    pulse();
    digitalWrite(OUT1,LOW);
    digitalWrite(A0,HIGH);
    delay(1000);
    digitalWrite(A0,LOW);
  }
  }
  }
  }
  else
  {
  digitalWrite(OUT1,HIGH); // No card data matched, sow we will send six pulses to OUT2, in order to tell it that an incorrect card has been read.
  delay(5);
    pulse();
    pulse();
    pulse();
    pulse();
    pulse();
    pulse();
    digitalWrite(OUT1,LOW);
  digitalWrite(A1,HIGH);
  delay(100);
  digitalWrite(A1,LOW);
}
}

  
  
  void readcard()  // If you power in in scanning mode, the readcard function is called, and this acts to read the EEPROM memory, and place the values in data-data19 registers
  // There are 20x registers (0-19), 5x per saved card.
  {
  data = EEPROM.read(0);
  data1 = EEPROM.read(1);
  data2 = EEPROM.read(2);
  data3 = EEPROM.read(3);
  data4 = EEPROM.read(4);
  data5 = EEPROM.read(5);
  data6 = EEPROM.read(6);
  data7 = EEPROM.read(7);
  data8 = EEPROM.read(8);
  data9 = EEPROM.read(9);
  data10 = EEPROM.read(10);
  data11 = EEPROM.read(11);
  data12 = EEPROM.read(12);
  data13 = EEPROM.read(13);
  data14 = EEPROM.read(14);
  data15 = EEPROM.read(15);
  data16 = EEPROM.read(16);
  data17 = EEPROM.read(17);
  data18 = EEPROM.read(18);
  data19 = EEPROM.read(19);
  delay(1000);  //Once done, wait 1 second before returning
  }
  
  void pulse()  // Send a 5ms pulse to OUT2
  {
    digitalWrite(OUT2,HIGH);
    delay(5);
    digitalWrite(OUT2,LOW);
    delay(5);
  }




   
