I am trying to be better at this arduino stuff and need some guidance.
Hardware:
Arduino Minima
RC522 RFID Reader
HiLetGo MicroSD Card Reader
Essentially what I am wanting to do is write onto RFID chips a unique value, then whenever that value is detected, pull a specific file from the microSD and send it to the audio interface. I am very new to this but I do have some basic understanding and experience with hardware and software. Enough to get me started at least.
try web searches for tutorials, example code, etc - e.g. arduino-rfid-nfc
also click Tools>Manage libraries and enter a search topic such as "RFID"
you will get a list of libraries
download a library and look at File>Examples for example code
check it suitable for the Minima etc
test each device in a separate program (using examples) before attempting to implement the complete project
try this code where a digit (1 2 3 4 etc) entered on Serial Monitor selects track to play
// UNO R4 Minima DFPlayer Mini player - use Serial to select track to play
// Serial uses USB Serial1 uses D1/TX and D0/RX
// **** Once the card is formatted, MP3 files can be copied to it.
// **** They are played in the order in which they were copied to the card
// names make no difference
// library https://github.com/DFRobot/DFRobotDFPlayerMini
#include "DFRobotDFPlayerMini.h"
// UNO R4 Minima connections
// UNO R4 Minima 5V to DFPlayer VCC
// UNO R4 Minima GND to DFPlayer GND
// UNO R4 Minima pin D0/RX to DFPlayer TX
// UNO R4 Minima pin D1/TX to DFPlayer RX
// UNO R4 Minima pin 4 to DFPlayer BUSY
#define mySoftwareSerial Serial1 // use UNO R4 Minima Serial1
#define BUSY 4
DFRobotDFPlayerMini myDFPlayer;
void printDetail(uint8_t type, int value);
void setup() {
Serial.begin(115200);
delay(2000);
pinMode(4, INPUT_PULLUP); // GPIO 4 is Busy signal
Serial1.begin(9600); // DFPlayer baud rate
delay(3000);
Serial.println();
Serial.println(F("\nUNO R4 Minima DFRobot DFPlayer Mini Demo"));
Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)"));
if (!myDFPlayer.begin(mySoftwareSerial)) { //Use softwareSerial to communicate with mp3.
Serial.println(F("Unable to begin:"));
Serial.println(F("1.Please recheck the connection!"));
Serial.println(F("2.Please insert the SD card!"));
while (true)
;
}
Serial.println(F("DFPlayer Mini online."));
myDFPlayer.volume(25); //Set volume value. From 0 to 30
//myDFPlayer.play(1); //Play the first mp3
delay(10);
Serial.println("enter track to play (1, 2, 3, etc)");
}
void loop() {
static unsigned long timer = millis();
static int busy = -1;
// check if player Busy status has changed
if (digitalRead(BUSY) != busy) {
if ((busy = digitalRead(BUSY)))
Serial.println("busy " + String(busy) + " NOT playing");
else
Serial.println("busy " + String(busy) + " playing");
}
if (millis() - timer > 3000) {
timer = millis();
//myDFPlayer.next(); //Play next mp3 every 3 second.
}
// if new track (integer) entered play it
if (Serial.available()) {
int x = Serial.parseInt(); // read track number
Serial.println("playing " + String(x));
myDFPlayer.play(x); // play track
while (Serial.available()) Serial.read(); // clear EOL etc
delay(10);
}
// if player status changed display
if (myDFPlayer.available()) {
printDetail(myDFPlayer.readType(), myDFPlayer.read()); //Print the detail message from DFPlayer to handle different errors and states.
}
}
void printDetail(uint8_t type, int value) {
switch (type) {
case TimeOut:
Serial.println(F("Time Out!"));
break;
case WrongStack:
Serial.println(F("Stack Wrong!"));
break;
case DFPlayerCardInserted:
Serial.println(F("Card Inserted!"));
break;
case DFPlayerCardRemoved:
Serial.println(F("Card Removed!"));
break;
case DFPlayerCardOnline:
Serial.println(F("Card Online!"));
break;
case DFPlayerPlayFinished:
Serial.print(F("Number:"));
Serial.print(value);
Serial.println(F(" Play Finished!"));
break;
case DFPlayerError:
Serial.print(F("DFPlayerError:"));
switch (value) {
case Busy:
Serial.println(F("Card not found"));
break;
case Sleeping:
Serial.println(F("Sleeping"));
break;
case SerialWrongStack:
Serial.println(F("Get Wrong Stack"));
break;
case CheckSumNotMatch:
Serial.println(F("Check Sum Not Match"));
break;
case FileIndexOut:
Serial.println(F("File Index Out of Bound"));
break;
case FileMismatch:
Serial.println(F("Cannot Find File"));
break;
case Advertise:
Serial.println(F("In Advertise"));
break;
default:
break;
}
break;
default:
break;
}
}
Serial monitor output
UNO R4 Minima DFRobot DFPlayer Mini Demo
Initializing DFPlayer ... (May take 3~5 seconds)
DFPlayer Mini online.
enter track to play (1, 2, 3, etc)
busy 1 NOT playing
playing 1
busy 0 playing
Number:1 Play Finished!
busy 1 NOT playing
playing 2
busy 0 playing
Number:2 Play Finished!
busy 1 NOT playing
playing 3
busy 0 playing
Number:3 Play Finished!
busy 1 NOT playing
The delays are (unfortunately) required; as nice as the Mini Players are some of them don't like their Serial input being "overrun".
It's also necessary to read the player's return messages which is done by the function printDetail(myDFPlayer.readType(), myDFPlayer.read()); every time when data are available.
If this is taken care of the Mini Players are working reliably for hours. They are the most simple option to add sound from files to more or less any Arduino.