1

After rewriting and getting a good grip on what I have got in my sketch so far, I need some assistance.

So in my sketch I can play an MP3 track with a certain tag, but when that tag is removed I can't start the fading out process.

So for all that helped on previous ones, where I just mashed a lot of code together and hoped for the best, this one should start to have some good foundations to build on.

But I'm stuck on the part that's commented out.

//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

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
}

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 !rfid.PICC_IsNewCardPresent();{
        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();
  }
}

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);
    }
    }*/
4
  • 1
    how is it that you write several for loops and then you write linear code like that commented out code?
    – jsotola
    Commented Apr 22, 2018 at 3:02
  • Which RFID reader? Which Arduino? Using "programming" as a tag is rather generic. Obviously it is programming.
    – Nick Gammon
    Commented Apr 22, 2018 at 6:38
  • In general, RFID readers send a message when a card is presented. They don't send a message when a card is moved out of range. Unless you have a fancy reader that tells you that (and in that case it would help to mention the reader type).
    – Nick Gammon
    Commented Apr 22, 2018 at 6:41
  • Thank you for your quick replies. Sorry, a mfrc522 reader coupled with a nano and a dfplayer MP3. Regards
    – jonboy79
    Commented Apr 22, 2018 at 9:01

1 Answer 1

1

Basically, you have written this code:

void loop() 
{
  // Look for new cards
  if (rfid.PICC_IsNewCardPresent() && rfid.PICC_ReadCardSerial()) 
  {
    {
      // ...
      if (content.substring(1) == "0A 97 F3 16") 
      {
        // play
      } else
      {
        // stop
      }
    }
    // ...
  }
}

Try something like this:

bool playing = false;

void loop() 
{
  // Look for new cards
  if (rfid.PICC_IsNewCardPresent() && rfid.PICC_ReadCardSerial()) 
  {
    // ...
    if (content.substring(1) == "0A 97 F3 16") 
    {
      // play
      playing = true;
    }
    // ... 
  }
  if (playing && !anyCardPresent())
  {
    // stop
    playing = false;
  }
}

and for the implementation od anyCardPresent refer to this discussion - if more consecutive calls of PICC_ReadCardSerial fail, you can consider this as card being removed

1
  • Thank you gabonator, I will try this when I'm home. I did use some code that could tell when the card was removed but the person who wrote it and myself didn't really understand it or how it worked. I can post it later if that might help. Regards
    – jonboy79
    Commented Apr 22, 2018 at 9:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.