I am new to the hobby (into it for 3 weeks) and I am having so much trouble getting my Arduinos to communicate with a DFPlayer Mini.
After days of fighting I finally realized I needed to use SoftwareSerial library and switch my RX and TX to pins 10 and 11. I'm not sure why I can't use the designated RX/TX pins 0 and 1. Can someone tell me what I'm doing wrong? Do you you have to define pins 0 and 1 in the code and if so, how?
Serial connections were a pain to figure out with my Arduino Uno as well but I got that working last week. I also have Arduino Nanos arriving today. I'm looking to really understand how to get the Uart RX TX pins connected properly so I can avoid this headache in the future.
My current code is as follows:
#include "Arduino.h"
#include "DFRobotDFPlayerMini.h"
#include "ezButton.h"
#if !defined(UBRR1H)
#include <SoftwareSerial.h>
SoftwareSerial softSerial(10, 11); // RX, TX
#endif
#define FPSerial softSerial
//definitions
DFRobotDFPlayerMini myDFPlayer;
void printDetail(uint8_t type, int value);
void printLimiSwitch();
ezButton limitSwitch(7);
void setup()
{
limitSwitch.setDebounceTime(50);
Serial.begin(115200);
FPSerial.begin(9600);
Serial.println(F("DFRobot DFPlayer Mini Demo"));
Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)"));
if (!myDFPlayer.begin(FPSerial, /*isACK = */true, /*doReset = */true)) { //Use serial 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){
delay(0); // Code to compatible with ESP8266 watch dog.
}
}
Serial.println(F("DFPlayer Mini online."));
myDFPlayer.play(1);
delay(100);
}
void loop()
{
static unsigned long timer = millis();
//Serial.println(digitalRead(pauseButton));
limitSwitch.loop(); // MUST call the loop() function first
printLimitSwitch();
/*
int state = limitSwitch.getState();
Serial.println("The limit state: " + state);
if(state == HIGH)
Serial.println("The limit switch: UNTOUCHED");
else
Serial.println("The limit switch: TOUCHED");
*/
/*if (myDFPlayer.available()) {
printDetail(myDFPlayer.readType(), myDFPlayer.read()); //Print the detail message from DFPlayer to handle different errors and states.
}
*/
}
void printLimitSwitch(){
if(limitSwitch.isPressed())
{
Serial.println("The limit switch: UNTOUCHED -> TOUCHED");
myDFPlayer.pause();
delay(300);
}
if(limitSwitch.isReleased())
{
Serial.println("The limit switch: TOUCHED -> UNTOUCHED");
myDFPlayer.start();
delay(300);
}
}
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 DFPlayerUSBInserted:
Serial.println("USB Inserted!");
break;
case DFPlayerUSBRemoved:
Serial.println("USB Removed!");
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;
}
}