Integrating DFPlayer mp3 into existing project

I have a sketch for model railroad that's been working flawlessly for a while now, and I'd like to intergrade mp3 sounds using the DFPlayer Mini MP3 Player from DF Robot. Everything runs off a regulated computer power supply (5 volts to the servos and LEDs and 12 volts to the Arduino at VIN.

I have successfully tested the DFPlayer on the breadboard using a Mega 2560, with an SD card, so I know how it's supposed to work.

The trick will be getting the Mega to communicate serially with the RDPlayer without messing up the existing sketch.

Note about the button on A0: I have tried all sorts of ways to use a pullup on a digital pin, and have never been able to get it to work in this sketch, so I have just stuck with A0 and a 1K resistor.

Also the LED signals are a commercial product that has V+ as common, and seperate grounds for each LED. They can't physically be rewired without damage.

Here is my successful sketch at present:


//Controls Two Turnouts And Three LED Signals
//Activates two servos and turns signal leds from green to yellow to red
//Coded by Artie Langston 8-10-2023
//artie@artielangston.com
/*****************************************************************/
const int button = A0;
const int rl1Pin = 49;
const int yl1Pin = 51;
const int gl1Pin = 53;
const int rl2Pin = 43;
const int yl2Pin = 45;
const int gl2Pin = 47;
const int rl3Pin = 37;
const int yl3Pin = 39;
const int gl3Pin = 41;
const int blrPin = 35;
const int blgPin = 33;
#define SIGON LOW
#define SIGOFF HIGH
#define ON HIGH
#define OFF LOW
/*************************************************************/
int start1 = 90;
int stop1 = 160;
#include <Servo.h>
Servo servo_one;
Servo servo_two;
int pos = 90;
/*****************************************************************/
void setup() {
  Serial.begin(9600);
  servo_one.attach(2);
  servo_two.attach(3);
  pinMode (blrPin, OUTPUT);
  pinMode (blgPin, OUTPUT);
  pinMode(rl1Pin, OUTPUT);
  pinMode(yl1Pin, OUTPUT);
  pinMode(gl1Pin, OUTPUT);
  pinMode(rl2Pin, OUTPUT);
  pinMode(yl2Pin, OUTPUT);
  pinMode(gl2Pin, OUTPUT);
  pinMode(rl3Pin, OUTPUT);
  pinMode(yl3Pin, OUTPUT);
  pinMode(gl3Pin, OUTPUT);
}
enum SWITCHSTATES {
  ST_OFF1,
  ST_OFF2,
  ST_STRAIGHT,
  ST_TURN,
};
SWITCHSTATES switchState = ST_OFF1;
/********************************************************************/
void loop() {
  int buttonRead = analogRead(button);
  delay(200);
  switch (switchState) {

    case ST_OFF1:
      switchoff1(buttonRead);

      digitalWrite(blrPin, OFF);
      digitalWrite(blgPin, ON);

      digitalWrite(gl1Pin, SIGON);
      digitalWrite(yl1Pin, SIGOFF);
      digitalWrite(rl1Pin, SIGOFF);
      
      digitalWrite(gl2Pin, SIGOFF);
      digitalWrite(yl2Pin, SIGOFF);
      digitalWrite(rl2Pin, SIGON);
      
      digitalWrite(gl3Pin, SIGOFF);
      digitalWrite(yl3Pin, SIGOFF);
      digitalWrite(rl3Pin, SIGON);
      
      break;


    case ST_OFF2:
      switchoff2(buttonRead);
      break;

    case ST_STRAIGHT:
      switchstraight(buttonRead);

      digitalWrite(blrPin, OFF);
      digitalWrite(blgPin, ON);

      digitalWrite(rl1Pin, SIGOFF);
      digitalWrite(yl1Pin, SIGON);
      delay(500);
      digitalWrite(gl1Pin, SIGON);
      
      digitalWrite(gl2Pin, SIGOFF);
      digitalWrite(yl2Pin, SIGON);
      delay(500);
      digitalWrite(rl2Pin, SIGON);
      
      digitalWrite(gl3Pin, SIGOFF);
      digitalWrite(yl3Pin, SIGON);
      delay(500);
      digitalWrite(rl3Pin, SIGON);
      
      break;


    case ST_TURN:
      switchturn(buttonRead);
      
      digitalWrite(blrPin, ON);
      digitalWrite(blgPin, OFF);

      digitalWrite(gl1Pin, SIGOFF);
      digitalWrite(yl1Pin, SIGON);
      delay(500);
      digitalWrite(yl1Pin, SIGON);
      digitalWrite(rl1Pin, SIGOFF);

      digitalWrite(rl2Pin, SIGOFF);
      digitalWrite(yl2Pin, SIGON);
      delay(500);
      digitalWrite(yl2Pin, SIGOFF);
      digitalWrite(gl2Pin, SIGON);

      digitalWrite(rl3Pin, SIGOFF);
      digitalWrite(yl3Pin, SIGON);
      delay(500);
      digitalWrite(gl3Pin, SIGON);
      digitalWrite(yl3Pin, SIGOFF);

      break;
  }
}
/**************************************************************/
void switchoff1(int buttonRead) {
  servo_one.write(start1);
  servo_two.write(start1);

  if (buttonRead > 500) {
    switchState = ST_TURN;
  }
}
/****************************************************************/
void switchturn(int buttonRead) {
  for (pos = start1; pos <= stop1; pos += 1) {
    servo_one.write(pos);
    delay(3);
    servo_two.write(pos);
    delay(3);
  }
  switchState = ST_OFF2;
}
/*****************************************************************************************/
void switchoff2(int buttonRead) {
  servo_one.write(stop1);
  servo_two.write(stop1);

  if (buttonRead > 500) {
    switchState = ST_STRAIGHT;
  }
}
/***************************************************************************************/
void switchstraight(int buttonRead) {
  for (pos = stop1; pos >= start1; pos -= 1) {
    servo_one.write(pos);
    delay(3);
    servo_two.write(pos);
    delay(3);
    switchState = ST_OFF1;
  }
}

Here is the schematic. I apologise, but I couldn't figure out how to change the board to a Mega 2560:

All it really has to do is communicate serially with the DFPlayer over pins 18 and 19, which it does just fine using their library on the breadboard.

So far, I have found no way to do this in one sketch.

Thanks much for taking a moment to look at this

Please post the total code with the DFPlayer integrated.

Why not use digitalRead?

I have tried a number of times to do that in this sketch, and it never works. I don't know why. :slight_smile: So I sticks with what works, A0.

I have used digital pullup in other sketches, and it works just fine. It's a mystery.

Then make a simple test sketch, just reading that input! How it can fail is a mystory.

I've done a project very similar to this. If it's helpful I could send you my code?

That would be terrific! Thanks! I'm relatively new at this.

#include <Arduino.h>
#include "DYPlayerArduino.h"
#include <SoftwareSerial.h>

SoftwareSerial SoftSerial(10, 11);
DY::Player player(&SoftSerial);

#define minTime 300000
#define maxTime 3600000

const int buttonPin = 2;  // the number of the pushbutton pin
const int ledPin = 12;    // the number of the LED pin

int buttonState = 0;  // variable for reading the pushbutton status

unsigned long endSound = 0;        // will store last time LED was updated

unsigned long timer, eventTime, startSound, currentMillis;

const long fileLength = 102000;           // interval at which to blink (milliseconds)

bool soundActive = false;

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  digitalWrite(7, LOW);
  digitalWrite(ledPin, HIGH);
  pinMode(buttonPin, INPUT);
  player.begin();
  Serial.begin(9600);
  player.setVolume(22);
  player.setCycleMode(DY::PlayMode::Sequence);
  eventTime = millis() + random(25);
  startSound = millis() + random(minTime, maxTime);
}

void loop() {
  if (millis() > eventTime) {
    analogWrite(6, random(250) + 135);
    eventTime = millis() + random(25);
  }
  if (millis() > startSound && soundActive == false) {
    soundActive = true;
    digitalWrite(4, HIGH);
    player.play();
    endSound = millis() + fileLength;
  }
  if (millis() > endSound && soundActive == true) {
    soundActive = false;
    endSound = millis();
    startSound = millis() + random(minTime, maxTime);
    digitalWrite(4, LOW);
  }
}

Slightly different, however I had a flickering LED non-stop, couldn't freeze even a little bit.
Had a DFPlayer Mini and LEDs running on a Arduino Uno clone.
Might be something you could scrounge from that, see what you can do!

Just because the A* pins are capable of being used as analogue inputs does not mean that they have to be used that way. They can equally well be used as digital inputs along with a pinMode() of INPUT_PULLUP

Like many people new to programing, I borrowed a big chunk of code that has been floating around YouTube and model railroad magazines for some time.

Instead of HIGH or LOW, for the button input, there's:

int buttonRead = analogRead(button);
delay(200);
switch (switchState)

and then, for example:

if (buttonRead > 500) {
switchState = ST_TURN;

or

if (buttonRead > 500) {
switchState = ST_STRAIGHT;

to tell the servos what to do. It works well, and makes sure the button is pressed for at least a second or so before activation.

It sounds as though whoever originated the code did not know that the A* pins could also be use as digital pins. This code will then have been copied over and over again and become gospel, but they could have done something like this

int buttonRead = digitalRead(buttonPin);
if (buttonRead)
{
  switchState = ST_TURN;

or even

if (digitalRead(buttonPin))
{
  switchState = ST_TURN;

Personally I would use INPUT_PULLUP in pinMode() and invert the wiring and logic and use

if (!digitalRead(buttonPin))
{
  switchState = ST_TURN;

But each to their own

Incidentally, nothing in the code snippet that you posted ensures that the button is pressed for one second. Once the analogRead() has been done and the variable has been set to the value read then no amount of delay() is going to change the value of the variable or ensure that the button is still pressed

@bassweasel
I've used DFRobot Extensively
I'll give you a code that works and some instructions to follow

  1. This code is based on the basic code that you'll get from the DFRobot website.
    However that code is really seriously lacking in accuracy when it comes to troubleshooting the DFRobot Player mini

  2. so i decided to add a few more troubleshooting features of my own, and i'll explain them to you and what they mean and what they prove at each step.
    Feel free to get rid of anything that you don't want, this worked for me , it may not be something you need or want.

  3. First of all, Here is the code, Insert it as appropriate into your code


#include <Arduino.h>             //Arduino Base Library
#include <DFRobotDFPlayerMini.h> //Controls DFRobot MP3 Player (Audio)
#include <SoftwareSerial.h>      // Software Serial Library

SoftwareSerial mySoftwareSerial(17, 16); // RX, TX
DFRobotDFPlayerMini myDFPlayer;
void printDetail(uint8_t type, int value);

/*/////////////////////////////////////////////////////////////////////////////////////////
 BEGIN SETUP
*//////////////////////////////////////////////////////////////////////////////////////////
void setup() 
{  // OPENS void setup Function
//-----------------------------------------------------------------------------------------
// BAUD Rate Setup
//-----------------------------------------------------------------------------------------
  mySoftwareSerial.begin(9600); // Setup of Serial BAUD Rate for DFRobot Player
  Serial.begin(115200);         // Setup of Serial BAUD Rate for serial Monitor
//-----------------------------------------------------------------------------------------
// Audio Initialization Sequence - Serial Monitor Output
//-----------------------------------------------------------------------------------------
  Serial.println();
  Serial.println(F("WELCOME - DFROBOT PLAYER DEMO"));
  Serial.println(F("PLEASE WAIT - INITIALIZING DFROBOT PLAYER "));
//-----------------------------------------------------------------------------------------
// IF ERROR - Serial Monitor Output
//-----------------------------------------------------------------------------------------
  if (!myDFPlayer.begin(mySoftwareSerial)) {
    Serial.println(F("ERROR - MP3 SOMETHING IS WRONG - TROUBLESHOOT:"));
    while (true);
  }
//-----------------------------------------------------------------------------------------
// IF NO ERROR - Serial Monitor Output
//-----------------------------------------------------------------------------------------
  Serial.println(F("AUDIO ONLINE - DFROBOT PLAYER STARTED SUCCESSFULLY"));
//-----------------------------------------------------------------------------------------
// SET THE OVERALL SYSTEM VOLUME
//-----------------------------------------------------------------------------------------
myDFPlayer.volume(25);  // Value = From 0 to 30 

///////////////////////////////////////////////////////////////////////////////////////////  
  Serial.println(F("================================================================"));
  Serial.println(F("              READING SD CARD ATTRIBUTES"));
  Serial.println(F("================================================================")); 
  Serial.print(F("    - READ STATE = "));   
  Serial.print(myDFPlayer.readState()); //read mp3 state . Reading or Not Reading 
  value = myDFPlayer.readState();
  Serial.println(F(" (1 = Reading/ 0 = NOT Reading)"));
//-----------------------------------------------------------------------------------------     
  Serial.print(F("    - STORAGE VOLUME CAPACITY = "));  
  Serial.print(myDFPlayer.readVolume()); //read current volume capacity
  Serial.print(F(" MB"));
  Serial.println(F(""));
//-----------------------------------------------------------------------------------------  
  Serial.print(F("    - READ FILE COUNTS "));
  Serial.println(myDFPlayer.readFileCounts()); //read all file counts in SD card //Produces 0
  value = myDFPlayer.readFileCounts(); //read all file counts in SD card
//-----------------------------------------------------------------------------------------  
  Serial.print(F("    - READ CURRENT FILE NUMBER "));
  Serial.println(myDFPlayer.readCurrentFileNumber()); //read current play file number
  value = myDFPlayer.readCurrentFileNumber();
//-----------------------------------------------------------------------------------------  
  Serial.print(F("    - readFileCountsInFolder"));
  Serial.println(myDFPlayer.readFileCountsInFolder(1)); //read file counts in folder SD:/03 PRODUCES ERROR
  Serial.println(F("================================================================")); 

  Serial.println(F("================================================================"));
  Serial.println(F("           BEGINNING SOUND SEQUENCE"));
  Serial.println(F("================================================================")); 
  Serial.println(F("  - DETAILS :"));
  Serial.println("AUDIO ONLY - Started");  
///////////////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////////////////
// SOUND SEQUENCE
myDFPlayer.playMp3Folder(3); // Play MP3 File (0003)
//delay(3);     
Serial.println("AUDIO ONLY - Completed");
//-----------------------------------------------------------------------------------------
///////////////////////////////////////////////////////////////////////////////////////////

}  // CLOSES void setup Function
///////////////////////////////////////////////////////////////////////////////////////////

/*/////////////////////////////////////////////////////////////////////////////////////////
 BEGIN LOOP
*//////////////////////////////////////////////////////////////////////////////////////////
void loop() 
  {  // OPENS void loop Function

  }  // CLOSES void loop Function
///////////////////////////////////////////////////////////////////////////////////////////

Most of it is self Explanatory

  1. Before we go into the explanation of the code Understand this.
  • Your SD Card must be formatted as FAT32 File system

  • It must not exceed 32GB (However i have used a 64GB SD Card and it worked)

  • This is important....

    • There is a supposed rule that says

    • Your files must be named in the format of 0000 so file 1 is 0001 and file 1 is 0002
      and so on THIS IS TRUE

    • Then we are told you must keep your entire SD Card free and have 1 folder called
      "mp3" in the root of the SD Card and all files reside in the directory level below that.
      THIS IS NOT TRUE, Not completely.
      You can do it this way or...
      You can dump your mp3 files into the root directory directly.

    • The Difference is... If putting the mp3 files directly into the root.
      You will then use this instruction to play the files
      .. myDFPlayer.play(1); to signify playing file 0001.mp3
      However if you create a folder called mp3 , to play file 0001 in that case you would
      use myDFPlayer.playMp3Folder(3);

      so the . play instruction plays from the root
      the .playMp3Folder instruction plays from within the first folder

  1. check out this link for the pinouts for DFRobot Player Mini Useful info.

  2. You now want to hook up the Rx and Tx lines to the data pins of your arduino.
    In my sketch i am using the library "Software Serial" if you are using ESP32 you
    can consider using the inbuilt Hardware Serial Pins

  3. NOW LET ME EXPLAIN WHAT THE CODE DOES...
    it should be straight forward to drop everything into it's correct place
    but let me explain anyway....

FIRST THE PRE SETUP (GLOBAL SECTION)

These are the included libraries that go right at the top
Arduino controls the board
DFRobotPlayerMini.h is the MP3 player library
NOW PAY ATTENTION TO THIS....
SoftwareSerial.h controls the Serial Transmission between the Arduino Dev board
and the Rx and Tx Pins on your DFRobot Player
DO NOT CONFUSE THIS FOR THE SERIAL COMMUNICATION ON SERIAL MONITOR
it has nothing to do with that .

#include <Arduino.h>             //Arduino Base Library
#include <DFRobotDFPlayerMini.h> //Controls DFRobot MP3 Player (Audio)
#include <SoftwareSerial.h>      // Software Serial Library

This sets up softwareserial.h as being referred to by the object name of "mySoftwareSerial" and defines the data pins (change these to suit your pin needs)
it also sets up your DFRobot Player for later use

SoftwareSerial mySoftwareSerial(17, 16); // RX, TX
DFRobotDFPlayerMini myDFPlayer;
void printDetail(uint8_t type, int value);

Now we start the SETUP Function
Here we setup the BAUD Rates for the DFRobot Player and the Serial Monitor
as indicated in the comments

/*/////////////////////////////////////////////////////////////////////////////////////////
 BEGIN SETUP
*//////////////////////////////////////////////////////////////////////////////////////////
void setup() 
{  // OPENS void setup Function
//-----------------------------------------------------------------------------------------
// BAUD Rate Setup
//-----------------------------------------------------------------------------------------
  mySoftwareSerial.begin(9600); // Setup of Serial BAUD Rate for DFRobot Player
  Serial.begin(115200);         // Setup of Serial BAUD Rate for serial Monitor
//-----------------------------------------------------------------------------------------

NOW THIS PART IS GOING TO HELP YOU TROUBLESHOOT

If you run Serial Monitor and DO see this message upon system
startup then :

  • This does not mean that your DFRobot Player actually started.
  • it Just proves that your baud rate and serial library is working as well your print
    statements,
    Seeing this has absolutely not relevance to the DFRobot player.
//-----------------------------------------------------------------------------------------
// Audio Initialization Sequence - Serial Monitor Output
//-----------------------------------------------------------------------------------------
  Serial.println();
  Serial.println(F("WELCOME - DFROBOT PLAYER DEMO"));
  Serial.println(F("PLEASE WAIT - INITIALIZING DFROBOT PLAYER "));
//-----------------------------------------------------------------------------------------

If you run Serial Monitor and DO NOT see this message upon system
startup then you need to do the following :

  • check Power to the Dev Board
  • Check that your baud rates are correct and that serial monitor has the correct Baud
    Rate
  • Check that the library is installed

Now is the point where the Rx and Tx pins are going to have data going over them
and an attempt to communicate with the DFRobot will actually happen

//-----------------------------------------------------------------------------------------
// IF ERROR - Serial Monitor Output
//-----------------------------------------------------------------------------------------
  if (!myDFPlayer.begin(mySoftwareSerial)) {
    Serial.println(F("ERROR - MP3 SOMETHING IS WRONG - TROUBLESHOOT:"));
    while (true);
  }
//-----------------------------------------------------------------------------------------

now.. if the Attempt is NOT SUCCESSFUL, this message will occur
if you are seeing this. You do not need to troubleshoot as before, as seeing this
message PROVES that the serial stuff is setup,
There may however be a problem with the SoftwareSerial.h library though
or the code or the MP3 player could be faulty

  • TOUCH THE METAL CASING AND SEE IF IT'S HOT
    if it is , you've created a short circuit

  • check voltage on the DFRobot Between GND and Vcc for Presence of 5V DC

  • check with an Oscilloscope if there is any Data Transmission going down your
    Rx and Tx Pins
    Verify this both at the DFRobot end and at the Dev board end, Maybe the Dev board
    is not sending the signal or is faulty

NOW, IF THERE IS NO ERROR this will happen

//-----------------------------------------------------------------------------------------
// IF NO ERROR - Serial Monitor Output
//-----------------------------------------------------------------------------------------
  Serial.println(F("AUDIO ONLINE - DFROBOT PLAYER STARTED SUCCESSFULLY"));
//-----------------------------------------------------------------------------------------

this DOES NOT mean that it started successfully and fully.

it merely means and proves

  • there is a Data signal going to the DFRobot Player and it has been received Successfully.

  • it doesn't necessarily prove that audio can play

  • at this point you want to ensure that you do actually have A SPEAKER ATTACHED.
    to SPK+ and SPK- on the DFRobot Player

it is as this point where the code on the DFRobot site will stop and give you the illusion
that everything is now working BUT IT'S NOT

What could happen from here to still be faulty

  • speaker could not be attached
  • SD Card could be Faulty
  • SD Card could be formatted incorrectly
  • DFRobot Player could have a fault with playing the file
  • The file could be in the wrong place and the code doesn't know how to find it
    etc etc...

so i have now implemented a few things that absolutely prove that it works

First. I set the system volume

//-----------------------------------------------------------------------------------------
// SET THE OVERALL SYSTEM VOLUME
//-----------------------------------------------------------------------------------------
myDFPlayer.volume(25);  // Value = From 0 to 30 

Now... READ STATE
a value of -1 or 1 means that it has read the data on the SD Card
a value of 0 means it cannot read the data, You may have a corrupted file or faulty SD Card

///////////////////////////////////////////////////////////////////////////////////////////  
  Serial.println(F("================================================================"));
  Serial.println(F("              READING SD CARD ATTRIBUTES"));
  Serial.println(F("================================================================")); 
  Serial.print(F("    - READ STATE = "));   
  Serial.print(myDFPlayer.readState()); //read mp3 state . Reading or Not Reading 
  value = myDFPlayer.readState();
  Serial.println(F(" (1 = Reading/ 0 = NOT Reading)"));
//----------------------------------------------------------------------------------------- 

This one is not yet perfected and not definitive but it does prove something.
Here you are using the READ VOLUME command. it's supposed to read
the volume capacity of your SD Card (to my knowledge)
i used it on a 64GB Card and it returns the value of 512
i have included a text line so that it reads 512MB
if you get that , it is literally reading the card attributes and the fact that your
card returns the values gives another level of confirmation that it works.
If you get a value of 0 Your card has failed to provide attribute information.
Possible Faulty DFRobot or SD Card

//-----------------------------------------------------------------------------------------     
  Serial.print(F("    - STORAGE VOLUME CAPACITY = "));  
  Serial.print(myDFPlayer.readVolume()); //read current volume capacity
  Serial.print(F(" MB"));
  Serial.println(F(""));
//----------------------------------------------------------------------------------------- 

Next...
let's cover 3 in 1

the idea here is that it's going to interrogate the SD Card and count how many
files you have in it
which one gives you a value will depend if you have it in root or in the mp3 folder.
read file counts if if you have it in root
read file counts in folder is if it's in the folder
read current file number is not yet perfected , i'm still working on it, You can remove
it if you like.

//-----------------------------------------------------------------------------------------  
  Serial.print(F("    - READ FILE COUNTS "));
  Serial.println(myDFPlayer.readFileCounts()); //read all file counts in SD card //Produces 0
  value = myDFPlayer.readFileCounts(); //read all file counts in SD card
//-----------------------------------------------------------------------------------------  
  Serial.print(F("    - READ CURRENT FILE NUMBER "));
  Serial.println(myDFPlayer.readCurrentFileNumber()); //read current play file number
  value = myDFPlayer.readCurrentFileNumber();
//-----------------------------------------------------------------------------------------  
  Serial.print(F("    - readFileCountsInFolder"));
  Serial.println(myDFPlayer.readFileCountsInFolder(1)); //read file counts in folder SD:/03 PRODUCES ERROR
  Serial.println(F("================================================================")); 

so at the end of all this...
You have serial monitor output that starts the troubleshooting
You know you've setup serial stuff correctly
You know that Rx and Tx lines are communicating
You know that no error occurred
You have file attributes and file information displayed to you therefore
DFRobot player is communicating with the board
and the SD Card is functioning and setup properly

You now just need Audible confirmation of the file playing

  Serial.println(F("================================================================"));
  Serial.println(F("           BEGINNING SOUND SEQUENCE"));
  Serial.println(F("================================================================")); 
  Serial.println(F("  - DETAILS :"));
  Serial.println("AUDIO ONLY - Started");  
///////////////////////////////////////////////////////////////////////////////////////////

Getting this message just means IT'S ATTEMPTING TO PLAY THE FILE

then this happens

///////////////////////////////////////////////////////////////////////////////////////////

// SOUND SEQUENCE

myDFPlayer.playMp3Folder(3); // Play MP3 File (0003)

//delay(3);

Serial.println("AUDIO ONLY - Completed");

//-----------------------------------------------------------------------------------------

///////////////////////////////////////////////////////////////////////////////////////////

the line

myDFPlayer.playMp3Folder(3);

Plays your file number 0003.mp3 IF IT'S IN A FOLDER CALLED mp3 (Case Sensitive)
if its' in root, you do this

myDFPlayer.play(3);

if you get to this line

Serial.println("AUDIO ONLY - Completed");

and you didn't hear Audio

  • Check speaker connection
  • check voltage to DFRobot
  • Check system volume
  • Check the file number in the code is correct and exists
    At this stage there should be almost no reason that the file wouldn't play

Beyond that, You can put some of this stuff in the Loop Function if you need it to repeat.

Let me know if you have any questions

I may have left something important out of that snippet. There does seem to be a delay on the button push somewhere.

Thanks very much! I'll experiment with this and see what I can learn. It's easier to refine something that's already functional.

This is wonderful! Thanks very much.

Since I had no problem getting the DFPlayer to work, and since I have the other sketch working perfectly, I think instead of trying to make the one Arduino do everything, at this point I will simply use 2 of them. :slight_smile: The one I have working signal lights and servos can just trigger the other one to play a sound effect under certain conditions. Right now, that's just a whistle, a bell and a diesel horn, so, not many pins involved.

I'll continue to experiment with integrating the code into the original sketch, but since I have already several unemployed Unos, I'll just use another one for the player.

When you have a problem with a single Arduino the answer is rarely to add a second one

If the main Arduino is going to pass a message to the second one telling it to play a sound then it might just as well send a message to the DFPlayer to play a sound

3 Likes

You're Welcome
THAT.... Right there is approx 6 months of stuffing around and wondering
why doesn't this work LOL

Yes, if its' working Just roll with that

although For afterwards

  1. Keep the code i gave you as handy and insert it later on for effective troubleshooting

  2. Now i'll give you another gem up your sleeve....
    that code was written in a time when i thought all i needed was Serial.print and Serial.println
    Now using these will also chew up your SRAM if you go overboard

Here is a way around that

#define AltText 1

#if AltText == 1
#define SameLine(x) Serial.print(x)
#define NewLine(x) Serial.println(x)

#else
#define Serial.print(x)
#define Serial.println(x)
#endif
void setup()
{
  Serial.begin(115200);
  delay(1000);
  SameLine("1 + 1 = ");
  NewLine("2");
}
void loop()
{

}

What your'e doing here is
Defining an object as "AltText"
the "1" Means this mode is ON / True
if you change this to 0 it will then be OFF and as such not compiled

then we have if AltText is True do this

#if AltText == 1
#define SameLine(x) Serial.print(x)
#define NewLine(x) Serial.println(x)

Which means
whenever you see the object "SameLine(x)" replace it for Serial.print(x)
and so on

You're effectively avoiding the writing of all text strings to your memory and only using
them when they are needed

and this..

#else

#define Serial.print(x)

#define Serial.println(x)

Accounts for happens when you change the value to 0 so that you don't get errors

After that manipulate the lines that output 1+1 = 2

Absolutely agree

Except there is no "problem", just as using A0 for the button isn't either. It's one solution that works. It's just not optimal or perhaps the most efficient best solution.

If I use the 2 arduinos, I immediately get the exact result I want, while I figure out how to integrate the code on just one. In the meantime, I get to enjoy using the layout.

Very simple to change when the time comes. I'm not going to quit working and testing, but that would be just a quick way to get exactly what I want.

I will definitely share, if I ever get it working.

The problem there, is that the simple test sketch will always work fine. The problem lies someplace else in the code that won't be revealed in a test sketch. The whole sketch is needed to troubleshoot it.

That's very simple in theory, but not as easy in practice with other serial devices like servos going on at the same time. At least, that's what I encounter so far. Also, not sure about conflicts with the baud rates of different serial devices - servos and player - if they are not the same for some reason.

I'm assuming the assigned serial pins are autonomous when set up, and there's no global baud rate? It looks like they have to match a board setting?