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