Need to exit this loop, to then be ready to start again.

Please bare with me as a noob.

So tag goes to RFID reader and plays track.mp3, i then remove the tag and the track fades out. The only problem i have is now when i put the same or another tag on the reader it only then plays as i REMOVE the tag as it FADES OUT.

My thoughts here are that i'm not exiting the loop to start a fresh, but i really do not know if this is correct or how to correct it .

Note i am using 3 tags, 1 rfid reader and 3 tracks

Regards

//rfid
#include <SPI.h>
#include <MFRC522.h>
#define RST_PIN         9           // reset pin 9
#define SS_PIN          10          // sda pin 10
MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance

String read_rfid;
String ValidCard_1 = "a97f316";     //  = 0001.mp3
String ValidCard_2 = "3a5fde16";    // = 0002.mp3
String ValidCard_3 = "8cde63a9";    // = 0003.mp3

//mp3 player
#include<SoftwareSerial.h>
int mp3Pin[2] = { 5, 4 };           //Rx/Rx

// For the checksum to the mp3
#define startByte 0x7E
#define endByte 0xEF
#define versionByte 0xFF
#define dataLength 0x06
#define infoReq 0x01

SoftwareSerial mp3(mp3Pin[0], mp3Pin[1]); // Rx, Tx

// -------------------------------------------------------------------------------------
// SETUP     SETUP     SETUP     SETUP     SETUP     SETUP     SETUP     SETUP     SETUP
// -------------------------------------------------------------------------------------
void setup() {
  Serial.begin(9600);                     // Initialize serial communications with the PC
  SPI.begin();                            // Init SPI bus
  mfrc522.PCD_Init();                     // Init MFRC522 card
  delay(30);
  mfrc522.PCD_DumpVersionToSerial();      // Print version of rfid

  //Mp3
  mp3.begin(9600);                        // Init mp3
  delay(1000);

  sendMP3Command(0x3F, 0, 0);             // Send request for initialization parameters
  sendMP3Command(0x06, 0, 30);            // Vol set to 30
  sendMP3Command(0x07, 0, 0);             // EQ = pop
}
//Card removal
uint8_t control = 0x00;                   //For the removal of card part


// -------------------------------------------------------------------------------------
// LOOP     LOOP     LOOP     LOOP     LOOP     LOOP     LOOP     LOOP     LOOP     LOOP
// -------------------------------------------------------------------------------------

void loop() {

  // Look for new cards
  if ( !mfrc522.PICC_IsNewCardPresent()) {
    return;
  }
  if ( !mfrc522.PICC_ReadCardSerial()) {
    return;
  }
  Serial.print(F(": Card UID:"));
  dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
  Serial.println();
}

//Setup data for UID
void dump_byte_array(byte * buffer, byte bufferSize) {
  read_rfid = "";
  for (byte i = 0; i < bufferSize; i++) {
    read_rfid = read_rfid + String(buffer[i], HEX);
  }


  if (ValidCard_1 == read_rfid) {
    Serial.println("0001.mp3");
    sendMP3Command(0x03, 0, 0001);  // Play track 0001.mp3
    sendMP3Command(0x19, 0, 0);     // Loop track playing
    delay(30);
  }

  if (ValidCard_2 == read_rfid) {
    Serial.println("0002.mp3");
    sendMP3Command(0x03, 0, 0002);  // Play track 0002.mp3
    sendMP3Command(0x19, 0, 0);     // Loop track playing
    delay(30);
  }

  if (ValidCard_3 == read_rfid) {
    Serial.println("0003.mp3");
    sendMP3Command(0x03, 0, 0003);  // Play track 0003.mp3
    sendMP3Command(0x19, 0, 0);     // Loop track playing
    delay(30);
  }


  // -------------------------------------------------------------------------------------
  // Card Removal script    Card Removal script   Card Removal script   Card Removal scrip
  // -------------------------------------------------------------------------------------
  bool result = true;
  uint8_t buf_len = 4;


  while (true) {
    control = 0;
    for (int i = 0; i < 3; i++) {
      if (!mfrc522.PICC_IsNewCardPresent()) {
        if (mfrc522.PICC_ReadCardSerial()) {
          //Serial.print('a');
          control |= 0x16;
        }
        if (mfrc522.PICC_ReadCardSerial()) {
          //Serial.print('b');
          control |= 0x16;
        }
        //Serial.print('c');
        control += 0x1;
      }
      //Serial.print('d');
      control += 0x4;
    }

    //Serial.println(control);
    if (control == 13 || control == 14) {
      //card is still there
    } else {
      break;
    }
  }

  Serial.println("CardRemoved");

  // -------------------------------------------------------------------------------------
  // Fade Out & Stop    Fade Out & Stop   Fade Out & Stop   Fade Out & Stop   Fade Out & S
  // -------------------------------------------------------------------------------------
  sendMP3Command(0x06, 0, 28); // Reduce vol by 2 and fade out
  delay(30);
  sendMP3Command(0x06, 0, 26);
  delay(30);
  sendMP3Command(0x06, 0, 24);
  delay(30);
  sendMP3Command(0x06, 0, 22);
  delay(30);
  sendMP3Command(0x06, 0, 20);
  delay(30);
  sendMP3Command(0x06, 0, 18);
  delay(30);
  sendMP3Command(0x06, 0, 16);
  delay(30);
  sendMP3Command(0x06, 0, 14);
  delay(30);
  sendMP3Command(0x06, 0, 12);
  delay(30);
  sendMP3Command(0x06, 0, 10);
  delay(30);
  sendMP3Command(0x06, 0, 8);
  delay(30);
  sendMP3Command(0x06, 0, 6);
  delay(30);
  sendMP3Command(0x06, 0, 4);
  delay(30);
  sendMP3Command(0x06, 0, 2);
  delay(30);
  sendMP3Command(0x06, 0, 0);
  //sendMP3Command(0x16, 0, 0); // Stop
  delay(500);                 // Change value if you want to read cards faster
 
  //mfrc522.PICC_HaltA();       // Instructs a PICC in state ACTIVE(*) to go to state HALT.
  mfrc522.PCD_StopCrypto1();  // Exit the PCD from its authenticated state

}


// -------------------------------------------------------------------------------------
// MP3 COMMAND AND DATA     MP3 COMMAND AND DATA     MP3 COMMAND AND DATA     MP3 COMMAN
// -------------------------------------------------------------------------------------

void sendMP3Command(byte Command, byte Param1, byte Param2) {

  // Calculate the checksum
  unsigned int checkSum = -(versionByte + dataLength + Command + infoReq + Param1 + Param2);

  // Construct the command line
  byte commandBuffer[10] = { startByte, versionByte, dataLength, Command, infoReq, Param1, Param2, highByte(checkSum),
                             lowByte(checkSum), endByte
                           };

  for (int cnt = 0; cnt < 10; cnt++) {
    mp3.write(commandBuffer[cnt]);
  }

  // Delay needed between successive commands
  delay(30);
}

//*****************************************************************************************//a
void loop() {

  // Look for new cards
  if ( !mfrc522.PICC_IsNewCardPresent()) {
    return;
  }

You started with the stupidest example code on the internet. Ctrl-A, Ctrl-x and write your own code that does NOT ignore everything else when there is no card present.

Hi, i take it that you mean the !, before the mfrc522?
Regards

PaulS:

void loop() {

// Look for new cards
  if ( !mfrc522.PICC_IsNewCardPresent()) {
    return;
  }



You started with the stupidest example code on the internet. Ctrl-A, Ctrl-x and write your own code that does NOT ignore everything else when there is no card present.

Paul, i'm sorry but the person who wrote this library for the reader uses this exact same code in his examples. Could you elaborate a little more? Not only himself, but there is a lot more people that also use this. Part of what you stand for seems to be honesty and truth, you mention the art of getting a good answer is a good question, now excuse me if i am wrong, but i explain, and ask about the loop function and how i am stuck, i also explain that i do not know much, so IF, the (!mfrc) bit is wrong or what is causing my problems, great, if not then it's really not helpful if this is a personal grudge with the person who wrote the library for this?

Still stuck on this one.

What happens if you delete the IF clause that @PaulS has identified?

...R

Robin2:
What happens if you delete the IF clause that @PaulS has identified?

...R

  // Look for new cards
  ( !mfrc522.PICC_IsNewCardPresent()) {
    return;
  }
  ( !mfrc522.PICC_ReadCardSerial()) {
    return;

= exit status 1
expected ';' before '{' token

 // Look for new cards
  !mfrc522.PICC_IsNewCardPresent(); {
    return;
  }
  !mfrc522.PICC_ReadCardSerial(); {
    return;
  }

= cards are not recognised.

  !mfrc522.PICC_IsNewCardPresent();
  !mfrc522.PICC_ReadCardSerial();

= : Card UID:
CardRemoved
: Card UID:
CardRemoved
: Card UID:
0002.mp3 // added card here, then i get a constant loop, off the mp3, but only the fade out(as the
CardRemoved // card would have been removed)
: Card UID:
0002.mp3
CardRemoved
: Card UID:
0002.mp3
CardRemoved
: Card UID:
0002.mp3

And without the mfrc522 the same as above, it is almost like i need to get out of the loop, refresh the whole loop byte data etc and then to start again. Like i say i'm really not that great at this stuff.
Regards, and thank you

!mfrc522.PICC_IsNewCardPresent(); {
    return;
  }

Completely unconditional return.
What's the point of that?

AWOL:

!mfrc522.PICC_IsNewCardPresent(); {

return;
  }


Completely unconditional return.
What's the point of that?

I think i'm slowly getting the hang of it, so it is saying if nothing then do nothing apart from move onto the next if nothing do nothing. Ok let me try if new card do something.

chefslot:
And without the mfrc522 the same as above,.....

You need to post the complete program. It seems like you did not delete the piece of code correctly.

...R

Ok i think i'm going to get my head around this and re write the whole code and create another thread.

chefslot:
Ok i think i'm going to get my head around this and re write the whole code and create another thread.

Don't start another Thread - it is much easier to help when all of the info for a project is in one place.

...R

Robin2:
Don't start another Thread - it is much easier to help when all of the info for a project is in one place.

...R

  //rfid
#include <SPI.h>
#include <MFRC522.h>
#define RST_PIN         9           // reset pin 9
#define SS_PIN          10          // sda pin 10
MFRC522 rfid(SS_PIN, RST_PIN);   // Create MFRC522 instance


// = 0001.mp3    8C DE 63 A9   uid's for me to relate to mp3 tracks.
// = 0002.mp3    0A 97 F3 16 /
// = 0003.mp3    3A 5F DE 16

//mp3 player
#include<SoftwareSerial.h>
int mp3Pin[2] = { 5, 4 };           //Rx/Rx

// For the checksum to the mp3
#define startByte 0x7E
#define endByte 0xEF
#define versionByte 0xFF
#define dataLength 0x06
#define infoReq 0x01

SoftwareSerial mp3(mp3Pin[0], mp3Pin[1]); // Rx, Tx

// -------------------------------------------------------------------------------------
// SETUP     SETUP     SETUP     SETUP     SETUP     SETUP     SETUP     SETUP     SETUP
// -------------------------------------------------------------------------------------
void setup() {
  Serial.begin(9600);                     // Initialize serial communications with the PC
  SPI.begin();                            // Init SPI bus
  rfid.PCD_Init();                        // Init MFRC522
  rfid.PCD_DumpVersionToSerial();         // Print version of rfid

  //Mp3
  mp3.begin(9600);                        // Init mp3
  delay(1000);
  sendMP3Command(0x3F, 0, 0);             // Send request for initialization parameters
  sendMP3Command(0x06, 0, 20);            // Vol set to 30
  sendMP3Command(0x07, 0, 0);             // EQ = pop
}



// -------------------------------------------------------------------------------------
// LOOP     LOOP     LOOP     LOOP     LOOP     LOOP     LOOP     LOOP     LOOP     LOOP
// -------------------------------------------------------------------------------------


void loop() {

  // Look for new cards
  if (rfid.PICC_IsNewCardPresent() && rfid.PICC_ReadCardSerial()) { //In new card found and new serial
    /*printHex(rfid.uid.uidByte, rfid.uid.size);                    // Print the Hex uid &
      Serial.println();                                               // use to get new uids.*/
    {

      String content = "";
      byte letter;
      for (byte i = 0; i < rfid.uid.size; i++)
      {
        content.concat(String(rfid.uid.uidByte[i] < 0x10 ? " 0" : " "));
        content.concat(String(rfid.uid.uidByte[i], HEX));
      }
      content.toUpperCase();
      if (content.substring(1) == "0A 97 F3 16") //change here the UID of the card/cards that you want to give access
      {
        //Serial.println("hello World");
        Serial.println("0002.mp3");
        sendMP3Command(0x03, 0, 0002);  // Play track 0002.mp3
        sendMP3Command(0x19, 0, 0);     // Loop track playing
        delay(30);
      }
      /*else if while for or?? {
        
        sendMP3Command(0x06, 0, 28); // Reduce vol by 2 and fade out
        delay(30);
        sendMP3Command(0x06, 0, 26);
        delay(30);
        sendMP3Command(0x06, 0, 24);
        delay(30);
        sendMP3Command(0x06, 0, 22);
        delay(30);
        sendMP3Command(0x06, 0, 20);
        delay(30);
        sendMP3Command(0x06, 0, 18);
        delay(30);
        sendMP3Command(0x06, 0, 16);
        delay(30);
        sendMP3Command(0x06, 0, 14);
        delay(30);
        sendMP3Command(0x06, 0, 12);
        delay(30);
        sendMP3Command(0x06, 0, 10);
        delay(30);
        sendMP3Command(0x06, 0, 8);
        delay(30);
        sendMP3Command(0x06, 0, 6);
        delay(30);
        sendMP3Command(0x06, 0, 4);
        delay(30);
        sendMP3Command(0x06, 0, 2);
        delay(30);
        sendMP3Command(0x06, 0, 0);
        //sendMP3Command(0x16, 0, 0); // Stop
        delay(500);                 // Change value if you want to read cards faster

        }*/
    }




    // Halt PICC
    rfid.PICC_HaltA();

    // Stop encryption on PCD
    rfid.PCD_StopCrypto1();
  }
}

// -------------------------------------------------------------------------------------
// MP3 COMMAND AND DATA     MP3 COMMAND AND DATA     MP3 COMMAND AND DATA     MP3 COMMAN
// -------------------------------------------------------------------------------------

void sendMP3Command(byte Command, byte Param1, byte Param2) {

  // Calculate the checksum
  unsigned int checkSum = -(versionByte + dataLength + Command + infoReq + Param1 + Param2);

  // Construct the command line
  byte commandBuffer[10] = { startByte, versionByte, dataLength, Command, infoReq, Param1, Param2, highByte(checkSum),
                             lowByte(checkSum), endByte
                           };

  for (int cnt = 0; cnt < 10; cnt++) {
    mp3.write(commandBuffer[cnt]);
  }

  // Delay needed between successive commands
  delay(30);
}



/*   // Print the Hex uid & use to get new uids.
  void printHex(byte * buffer, byte bufferSize) {
  for (byte i = 0; i < bufferSize; i++) {
    Serial.print(buffer[i] < 0x10 ? " 0" : " ");
    Serial.print(buffer[i], HEX);
  }
  }*/

@chefslot, you have posted an updated version of your program but you have not told us what happens when you run that program or what you want it to do that is different.

...R

Robin2:
@chefslot, you have posted an updated version of your program but you have not told us what happens when you run that program or what you want it to do that is different.

...R

Sorry when i take the tag away from the RFID player I want it to follow the fade out part and stop.
Currently it just plays the track, as it is looped via command to the MP3 player it continues to play.
Regards

Can you post a link to the documentation for the MFRC522 library - I have been unable to find it and I am not familiar with the device.

It seems to me you need to separate the existence of the device from reading the device.

How does the library know that there is a device within range and subsequently when it has moved out of range?

Your program needs to identify the change from when there is no device in range and when a device comes into range and at that point you need to read the data once.

Based on the data you can start the MP3 playing.

Then you need to repeatedly check that the same device continues to be within range and when it changes from being in range to not being in range you need to call the code that makes the MP3 fade out.

...R

chefslot:
Duplicate thread sorry

Thank you. Other thread removed.

Robin2:
Can you post a link to the documentation for the MFRC522 library - I have been unable to find it and I am not familiar with the device.

Thank you, and here GitHub - miguelbalboa/rfid: Arduino RFID Library for MFRC522

Now, this is where i got the other sketch from that had a control value in too, what it would have some sort of argument within it's self, saying 13,14,13,14, then it would ping back a value of 15 when no card/tag.
But the person who wrote it couldn't really remember or know why it worked. Here is the shortened version of the sketch,

#include <SPI.h>
#include <MFRC522.h>

#define RST_PIN         9           // Configurable, see typical pin layout above
#define SS_PIN          10          // Configurable, see typical pin layout above

MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance

//*****************************************************************************************//
void setup() {
  Serial.begin(9600);                                           // Initialize serial communications with the PC
  SPI.begin();                                                  // Init SPI bus
  mfrc522.PCD_Init();                                              // Init MFRC522 card
}
uint8_t control = 0x00;

//*****************************************************************************************//



void loop() {

  // Look for new cards
  if ( !mfrc522.PICC_IsNewCardPresent()) {
    return;
  }
  if ( !mfrc522.PICC_ReadCardSerial()) {
    return;
  }

  bool result = true;
  uint8_t buf_len = 4;

  Serial.println("NewCard ");
  
  while (true) {
    control = 0;
    for (int i = 0; i < 3; i++) {
      if (!mfrc522.PICC_IsNewCardPresent()) {
        if (mfrc522.PICC_ReadCardSerial()) {
          //Serial.print('a');
          control |= 0x16;
        }
        if (mfrc522.PICC_ReadCardSerial()) {
          //Serial.print('b');
          control |= 0x16;
        }
        //Serial.print('c');
        control += 0x1;
      }
      //Serial.print('d');
      control += 0x4;
    }

    Serial.println(control);
    if (control == 13 || control == 14) {
      //card is still there
    } else {
      break;
    }
  }
  Serial.println("CardRemoved");
  delay(500); //change value if you want to read cards faster

  mfrc522.PICC_HaltA();
  mfrc522.PCD_StopCrypto1();
}
//*****************************************************************************************//a

and here is the full length version

/*
 * Initial Author: ryand1011 (https://github.com/ryand1011)
 *
 * Reads data written by a program such as "rfid_write_personal_data.ino"
 *
 * See: https://github.com/miguelbalboa/rfid/tree/master/examples/rfid_write_personal_data
 *
 * Uses MIFARE RFID card using RFID-RC522 reader
 * Uses MFRC522 - Library
 * -----------------------------------------------------------------------------------------
 *             MFRC522      Arduino       Arduino   Arduino    Arduino          Arduino
 *             Reader/PCD   Uno/101       Mega      Nano v3    Leonardo/Micro   Pro Micro
 * Signal      Pin          Pin           Pin       Pin        Pin              Pin
 * -----------------------------------------------------------------------------------------
 * RST/Reset   RST          9             5         D9         RESET/ICSP-5     RST
 * SPI SS      SDA(SS)      10            53        D10        10               10
 * SPI MOSI    MOSI         11 / ICSP-4   51        D11        ICSP-4           16
 * SPI MISO    MISO         12 / ICSP-1   50        D12        ICSP-1           14
 * SPI SCK     SCK          13 / ICSP-3   52        D13        ICSP-3           15
*/

#include <SPI.h>
#include <MFRC522.h>

#define RST_PIN         9           // Configurable, see typical pin layout above
#define SS_PIN          10          // Configurable, see typical pin layout above

MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance

//*****************************************************************************************//
void setup() {
  Serial.begin(9600);                                           // Initialize serial communications with the PC
  SPI.begin();                                                  // Init SPI bus
  mfrc522.PCD_Init();                                              // Init MFRC522 card
}
void PrintHex(uint8_t *data, uint8_t length) // prints 8-bit data in hex with leading zeroes
{
     char tmp[16];
       for (int i=0; i<length; i++) { 
         sprintf(tmp, "0x%.2X",data[i]); 
         Serial.print(tmp); Serial.print(" ");
       }
}
uint8_t buf[10]= {};
MFRC522::Uid id;
MFRC522::Uid id2;
bool is_card_present = false;
//*****************************************************************************************//

void cpid(MFRC522::Uid *id){
  memset(id, 0, sizeof(MFRC522::Uid));
  memcpy(id->uidByte, mfrc522.uid.uidByte, mfrc522.uid.size);
  id->size = mfrc522.uid.size;
  id->sak = mfrc522.uid.sak;
}

bool cmpid(MFRC522::Uid *id1, MFRC522::Uid *id2){
  return memcmp(id1, id2, sizeof(MFRC522::Uid));
}

void deregister_card(){
  is_card_present = false;
  memset(&id,0, sizeof(id));
}
uint8_t control = 0x00;
void loop() {

  MFRC522::MIFARE_Key key;
  for (byte i = 0; i < 6; i++) key.keyByte[i] = 0xFF;
  MFRC522::StatusCode status;

  //-------------------------------------------

  // Look for new cards
  if ( !mfrc522.PICC_IsNewCardPresent()) {
    return;
  }
  if ( !mfrc522.PICC_ReadCardSerial()) {
    return;
  }
  //PrintHex(id.uidByte, id.size);
  //Serial.println("hello");
  bool result = true;
  uint8_t buf_len=4;
  cpid(&id);
  Serial.print("NewCard ");
  qPrintHex(id.uidByte, id.size);
  Serial.println("");
  while(true){
    control=0;
    for(int i=0; i<3; i++){
      if(!mfrc522.PICC_IsNewCardPresent()){
        if(mfrc522.PICC_ReadCardSerial()){
          //Serial.print('a');
          control |= 0x16;
        }
        if(mfrc522.PICC_ReadCardSerial()){
          //Serial.print('b');
          control |= 0x16;
        }
        //Serial.print('c');
          control += 0x1;
      }
      //Serial.print('d');
      control += 0x4;
    }
    
    //Serial.println(control);
    if(control == 13 || control == 14){
      //card is still there
    } else {
      break;
    }
  }
  Serial.println("CardRemoved");
  delay(500); //change value if you want to read cards faster

  mfrc522.PICC_HaltA();
  mfrc522.PCD_StopCrypto1();
}
//*****************************************************************************************//a

Regards

I had seen that GITHUB link but it does not seem to have documentation to explain each of the library's functions so I don't know if there are other functions that would be more helpful for your project.

I have had a look at the full-length program in Reply #17 and I have restructured it in two steps to try to make the functioning more obvious so it can be adapted to your needs. Obviously I have been unable to test my changes.

The purpose of the following two versions is to try to enable you to understand how the second version evolved from the version in Reply #17. In the first case I have eliminated the return statements and just use regular IF/ELSE. That should not change the functionality, but I hope it will be clearer.

In the second example I have moved a large chunk of code into a function just so that it does not clutter up loop(). And it should (I hope) suggest a pattern that you can use to add extra functionality for when the card is removed.

First Version

/*
 * Initial Author: ryand1011 (https://github.com/ryand1011)
 *
 * Reads data written by a program such as "rfid_write_personal_data.ino"
 *
 * See: https://github.com/miguelbalboa/rfid/tree/master/examples/rfid_write_personal_data
 *
 * Uses MIFARE RFID card using RFID-RC522 reader
 * Uses MFRC522 - Library
 * -----------------------------------------------------------------------------------------
 *             MFRC522      Arduino       Arduino   Arduino    Arduino          Arduino
 *             Reader/PCD   Uno/101       Mega      Nano v3    Leonardo/Micro   Pro Micro
 * Signal      Pin          Pin           Pin       Pin        Pin              Pin
 * -----------------------------------------------------------------------------------------
 * RST/Reset   RST          9             5         D9         RESET/ICSP-5     RST
 * SPI SS      SDA(SS)      10            53        D10        10               10
 * SPI MOSI    MOSI         11 / ICSP-4   51        D11        ICSP-4           16
 * SPI MISO    MISO         12 / ICSP-1   50        D12        ICSP-1           14
 * SPI SCK     SCK          13 / ICSP-3   52        D13        ICSP-3           15
*/

#include <SPI.h>
#include <MFRC522.h>

#define RST_PIN         9           // Configurable, see typical pin layout above
#define SS_PIN          10          // Configurable, see typical pin layout above

MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance

//*****************************************************************************************//
void setup() {
    Serial.begin(9600);                                           // Initialize serial communications with the PC
    SPI.begin();                                                  // Init SPI bus
    mfrc522.PCD_Init();                                              // Init MFRC522 card
}
void PrintHex(uint8_t *data, uint8_t length) // prints 8-bit data in hex with leading zeroes
{
         char tmp[16];
             for (int i=0; i<length; i++) {
                 sprintf(tmp, "0x%.2X",data[i]);
                 Serial.print(tmp); Serial.print(" ");
             }
}
uint8_t buf[10]= {};
MFRC522::Uid id;
MFRC522::Uid id2;
bool is_card_present = false;
//*****************************************************************************************//

void cpid(MFRC522::Uid *id){
    memset(id, 0, sizeof(MFRC522::Uid));
    memcpy(id->uidByte, mfrc522.uid.uidByte, mfrc522.uid.size);
    id->size = mfrc522.uid.size;
    id->sak = mfrc522.uid.sak;
}

bool cmpid(MFRC522::Uid *id1, MFRC522::Uid *id2){
    return memcmp(id1, id2, sizeof(MFRC522::Uid));
}

void deregister_card(){
    is_card_present = false;
    memset(&id,0, sizeof(id));
}
uint8_t control = 0x00;


void loop() {

    MFRC522::MIFARE_Key key;
    for (byte i = 0; i < 6; i++) {
        key.keyByte[i] = 0xFF;
    }
    MFRC522::StatusCode status;

    //-------------------------------------------

    // Look for new cards
    if ( !mfrc522.PICC_IsNewCardPresent()) {
        // do nothing;
    }
    else {
        if ( !mfrc522.PICC_ReadCardSerial()) {
            // do nothing;
        }
        else {
            //PrintHex(id.uidByte, id.size);
            //Serial.println("hello");
            bool result = true;
            uint8_t buf_len=4;
            cpid(&id);
            Serial.print("NewCard ");
            qPrintHex(id.uidByte, id.size);
            Serial.println("");
            while(true){
                control=0;
                for(int i=0; i<3; i++){
                    if(!mfrc522.PICC_IsNewCardPresent()){
                        if(mfrc522.PICC_ReadCardSerial()){
                                //Serial.print('a');
                            control |= 0x16;
                        }
                        if(mfrc522.PICC_ReadCardSerial()){
                                //Serial.print('b');
                            control |= 0x16;
                        }
                            //Serial.print('c');
                        control += 0x1;
                    }
                        //Serial.print('d');
                    control += 0x4;
                }
                 
                //Serial.println(control);
                if(control == 13 || control == 14){
                    //card is still there
                } 
                else {
                    break;
                }
            }
        }
    }
    Serial.println("CardRemoved");
    delay(500); //change value if you want to read cards faster

    mfrc522.PICC_HaltA();
    mfrc522.PCD_StopCrypto1();
}
//*****************************************************************************************//a

void playMP3() {
        //PrintHex(id.uidByte, id.size);
        //Serial.println("hello");
    bool result = true;
    uint8_t buf_len=4;
    cpid(&id);
    Serial.print("NewCard ");
    qPrintHex(id.uidByte, id.size);
    Serial.println("");
    while(true){
        control=0;
        for(int i=0; i<3; i++){
            if(!mfrc522.PICC_IsNewCardPresent()){
                if(mfrc522.PICC_ReadCardSerial()){
                        //Serial.print('a');
                    control |= 0x16;
                }
                if(mfrc522.PICC_ReadCardSerial()){
                        //Serial.print('b');
                    control |= 0x16;
                }
                    //Serial.print('c');
                control += 0x1;
            }
                //Serial.print('d');
            control += 0x4;
        }
         
        //Serial.println(control);
        if(control == 13 || control == 14){
            //card is still there
        } 
        else {
            break;
        }
    }
}

Second Version in next Reply

...R

Second Version

/*
 * Initial Author: ryand1011 (https://github.com/ryand1011)
 *
 * Reads data written by a program such as "rfid_write_personal_data.ino"
 *
 * See: https://github.com/miguelbalboa/rfid/tree/master/examples/rfid_write_personal_data
 *
 * Uses MIFARE RFID card using RFID-RC522 reader
 * Uses MFRC522 - Library
 * -----------------------------------------------------------------------------------------
 *             MFRC522      Arduino       Arduino   Arduino    Arduino          Arduino
 *             Reader/PCD   Uno/101       Mega      Nano v3    Leonardo/Micro   Pro Micro
 * Signal      Pin          Pin           Pin       Pin        Pin              Pin
 * -----------------------------------------------------------------------------------------
 * RST/Reset   RST          9             5         D9         RESET/ICSP-5     RST
 * SPI SS      SDA(SS)      10            53        D10        10               10
 * SPI MOSI    MOSI         11 / ICSP-4   51        D11        ICSP-4           16
 * SPI MISO    MISO         12 / ICSP-1   50        D12        ICSP-1           14
 * SPI SCK     SCK          13 / ICSP-3   52        D13        ICSP-3           15
*/

#include <SPI.h>
#include <MFRC522.h>

#define RST_PIN         9           // Configurable, see typical pin layout above
#define SS_PIN          10          // Configurable, see typical pin layout above

MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance

//*****************************************************************************************//
void setup() {
    Serial.begin(9600);                                           // Initialize serial communications with the PC
    SPI.begin();                                                  // Init SPI bus
    mfrc522.PCD_Init();                                              // Init MFRC522 card
}
void PrintHex(uint8_t *data, uint8_t length) // prints 8-bit data in hex with leading zeroes
{
         char tmp[16];
             for (int i=0; i<length; i++) {
                 sprintf(tmp, "0x%.2X",data[i]);
                 Serial.print(tmp); Serial.print(" ");
             }
}
uint8_t buf[10]= {};
MFRC522::Uid id;
MFRC522::Uid id2;
bool is_card_present = false;
//*****************************************************************************************//

void cpid(MFRC522::Uid *id){
    memset(id, 0, sizeof(MFRC522::Uid));
    memcpy(id->uidByte, mfrc522.uid.uidByte, mfrc522.uid.size);
    id->size = mfrc522.uid.size;
    id->sak = mfrc522.uid.sak;
}

bool cmpid(MFRC522::Uid *id1, MFRC522::Uid *id2){
    return memcmp(id1, id2, sizeof(MFRC522::Uid));
}

void deregister_card(){
    is_card_present = false;
    memset(&id,0, sizeof(id));
}
uint8_t control = 0x00;


void loop() {

    MFRC522::MIFARE_Key key;
    for (byte i = 0; i < 6; i++) {
        key.keyByte[i] = 0xFF;
    }
    MFRC522::StatusCode status;

    //-------------------------------------------

    // Look for new cards
    if ( !mfrc522.PICC_IsNewCardPresent()) {
        fadeOutMP3();
    }
    else {
        if ( !mfrc522.PICC_ReadCardSerial()) {
            // do nothing;
        }
        else {
            playMP3()
        }
    }
    Serial.println("CardRemoved");
    delay(500); //change value if you want to read cards faster

    mfrc522.PICC_HaltA();
    mfrc522.PCD_StopCrypto1();
}
//*****************************************************************************************//a

void playMP3() {
        //PrintHex(id.uidByte, id.size);
        //Serial.println("hello");
    bool result = true;
    uint8_t buf_len=4;
    cpid(&id);
    Serial.print("NewCard ");
    qPrintHex(id.uidByte, id.size);
    Serial.println("");
    while(true){
        control=0;
        for(int i=0; i<3; i++){
            if(!mfrc522.PICC_IsNewCardPresent()){
                if(mfrc522.PICC_ReadCardSerial()){
                        //Serial.print('a');
                    control |= 0x16;
                }
                if(mfrc522.PICC_ReadCardSerial()){
                        //Serial.print('b');
                    control |= 0x16;
                }
                    //Serial.print('c');
                control += 0x1;
            }
                //Serial.print('d');
            control += 0x4;
        }
         
        //Serial.println(control);
        if(control == 13 || control == 14){
            //card is still there
        } 
        else {
            break;
        }
    }
}

//==========

void fadeOutMP3() {
    // don't know what should go here
}

...R