Robin2:
Did you try the code I suggested without any changes? If so, what happened?
...R
Yes it did, it said card removed when no card was present, then when card was added it Serial.printed the uid.
So then moving on i added all the other mp3 parts. Now it will only play a sound when the tag is removed, it will follow the fadeOutMP3, but nothing else, thats when i started playing around with the first if statement.
NoCard
NoCard
NoCard
0002.mp3 // card placed
NewCard 0x0A 0x97 0xF3 0x16 //once card removed i got the fadeOutMP3,
//then serial monitor done nothing else
Here is the code before i started playing
/*
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
*/
// = 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
#include <SPI.h>
#include <MFRC522.h>
#include <string.h> // added
#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
//////////////////////////////////////////////////////////
//Added
//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
mfrc522.PCD_Init(); // Init MFRC522 card
//////////////////////////////////////////////////////////
//Added
//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 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;
//-------------------------------------------
//////////////////////////////////////////////////////////
//changed the !mfrc522,,
// Look for new cards
// Look for new cards
if ( !mfrc522.PICC_IsNewCardPresent()) {
fadeOutMP3();
}
else {
if ( !mfrc522.PICC_ReadCardSerial()) {
// do nothing;
}
else {
playMP3();
}
}
Serial.println("NoCard");
delay(500); //change value if you want to read cards faster
mfrc522.PICC_HaltA();
mfrc522.PCD_StopCrypto1();
}
//*****************************************************************************************//a
void playMP3() {
//////////////////////////////////////////////////////////
//Added
String content = "";
byte letter;
for (byte i = 0; i < mfrc522.uid.size; i++)
{
content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
content.concat(String(mfrc522.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);
//////////////////////////////////////////////////////////
//PrintHex(id.uidByte, id.size);
//Serial.println("hello");
bool result = true;
uint8_t buf_len = 4;
cpid(&id);
Serial.print("NewCard ");
PrintHex(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 {
fadeOutMP3();
// return; ////////added
}
}
}
}
//==========
void fadeOutMP3() {
//////////////////////////////////////////////////////////
//Added
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
//////////////////////////////////////////////////////////
}
//////////////////////////////////////////////////////////
//Added
// -------------------------------------------------------------------------------------
// 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);
}
//////////////////////////////////////////////////////////