I am using an Arduino Nano Every to interface with the DF Player Mini using the code below. I have a button to transition to the next file in the SD card on the DF player. I am using EEPROM to save the last file played so that I can play the subsequent files after turning off the Arduino without having to start from the first file. I have verified that my code works without error using an Arduino Uno but I wanted to use an Arduino Nano Every to minimize the size of the hardware. When I test this code on the Nano Every, the other buttons work (repeat file, increase vol, decrease vol) but the next button does not work. When I press the button to transition to the next file, it simply repeats the file and doesn't play the next file.
Moreover, it stops running entirely as soon as I bring up the serial monitor. If the serial monitor is brought up while the program is running, the buttons do not respond and the LED on the DF player does not turn on. On the serial monitor I see the initialization and df_player error messages that is unable to begin. Pressing the reset button while the serial monitor is only repeats these messages.
I am able to play all of the files by using the myDFPlayer.next()
command so I know the SD card isn't corrupted. I have tried different EEPROM addresses and that did not change anything.
Any ideas on the cause of this issue or what I can troubleshoot?
#include "Arduino.h"
#include "EEPROM.h"
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"
SoftwareSerial mySoftwareSerial(10, 11); // TX, RX
DFRobotDFPlayerMini myDFPlayer;
void printDetail(uint8_t type, int value);
const int buttonPin = 2; // the number of the next file pushbutton pin
int buttonState = 0; // variable for reading the pushbutton status
const int volUp = 4; // the number of the volume UP pushbutton pin
const int volDown = 5; // the number of the volume DOWN pushbutton pin
int volUpState = 0; // variable for reading the volume UP button status
int volDownState = 0; // variable for reading the volume DOWN button status
int volume = 30;
int addr = 0;
int file_count = 0;
int file = 0;
const int repeatPin = 6; // the number of the repeat pushbutton pin
int repeatState = 0; // variable for reading the repeat status
void setup()
{
mySoftwareSerial.begin(9600);
Serial.begin(9600);
Serial.println();
Serial.println(F("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);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
pinMode(volUp, INPUT);
pinMode(volDown, INPUT);
pinMode(repeatPin, INPUT);
}
Serial.println(F("DFPlayer Mini online."));
myDFPlayer.volume(volume); //Set volume value. From 0 to 30
// myDFPlayer.play(4); //Play the first mp3
file_count = myDFPlayer.readFileCountsInFolder(1); //read fill counts in folder SD:/01
Serial.println(F("File count:"));
Serial.println(file_count);
volume = myDFPlayer.readVolume();
}
void loop()
{
static unsigned long timer = millis();
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
volUpState = digitalRead(volUp);
volDownState = digitalRead(volDown);
repeatState = digitalRead(repeatPin);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState == HIGH) {
file = EEPROM.read(addr);
if (file >= file_count){
file = 1;
}
else {
file++;
}
myDFPlayer.playMp3Folder(file); //play specific mp3 in SD:/MP3/0004.mp3; File Name(0~65535)
// myDFPlayer.next(); //Play next mp3
EEPROM.write(addr, file);
Serial.println(file); //read current play file number
delay(1000);
}
if (repeatState == HIGH) {
myDFPlayer.playMp3Folder(file);
delay(1000);
}
if (volDownState == HIGH) {
volume = myDFPlayer.readVolume();
if (volume > 0) {
volume--;
myDFPlayer.volume(volume);
delay(1000);
}
}
if (volUpState == HIGH) {
volume = myDFPlayer.readVolume();
if (volume < 30) {
volume++;
myDFPlayer.volume(volume);
delay(1000);
}
}
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;
}
}