I have a project where I need a sound file to be played continuously whilst at the same time being able cycle through two other sound files by using a momentary switch.
My basic plan is to use two DFPlayers, one to play the continuous file and one to swap between the two other files.
I'm trying to switch between the two serial communication to send the "Play" command but I'm not able to get any sound out. But with only one DFPlayer connected it works.
#include "Arduino.h"
#include "SoftwareSerial.h"
#include "DFPlayerMini_Fast.h"
SoftwareSerial Ser1(15, 14); // RX, TX Sound Byte
SoftwareSerial Ser2(17, 16); // RX, TX Ambiant Sound
DFPlayerMini_Fast player;
int sw1 = 49;
int sw2 = 51;
int sw3 = 53;
void setup()
{
Ser1.begin(9600);
Ser2.begin(9600);
Serial.begin(115200);
//3 push buttons with pullups
pinMode(sw1,INPUT_PULLUP); //Define each button as input with pullup
pinMode(sw2,INPUT_PULLUP);
pinMode(sw3,INPUT_PULLUP);
}
void loop()
{
Serial.println("Start");
Ser1.listen(); // Select Ser1 for receiving
delay(1000); // Give it time to send a response or you'll get nothing!
player.play(1);
Ser1.print("Serial 1 Playing sound 1");
Ser2.listen(); // Select Ser2 for receiving
delay(1000); // Give it time to send a response or you'll get nothing!
player.play(1);
Ser1.print("Serial 2 Playing sound 1");
if(!digitalRead(sw1))
{
Ser1.listen();
player.play(1); // Play audio track 0001
delay(1000); // 1s of delay
}
if(!digitalRead(sw2))
{
Ser1.listen();
player.play(2); // Play audio track 0002
delay(1000); // 1s of delay
}
if(!digitalRead(sw3))
{
Ser1.listen();
player.play(3); // Play audio track 0003
delay(1000); // 1s of delay
}
}
Given You can trigger a sound playing device to start and go on, on its own, it will work. If the sound player plays the song and then returns, Your plan will not work. Check the library used!
Never used the DFPlayer...... Can You post schematics showing how they are connected.
A link to the datasheet would be fine.
The code you posted works if only one player is connected? I would be surprised. I would not expect your code to work at all. It is missing an important code line: player.begin().
I think you need to use 2 DFPlayerMini_Fast objects, one for each player.
With your current code, I do not think you need to use Ser1.listen() or Ser2.listen() at all, because you are not using any commands that require data to be received back from the players.
If this is a Mega, there are at least two hardware serial ports available. Hardware serial ports should always be used in preference to software serial ports when they are available. Example sketches often use software serial so that they can run on Uno, Nano etc.
If you did need to read data back from the players, using hardware serial ports will not require any use of SerX.listen() because hardware serial ports can listen simultaneously on each port. One reason for reading data back from the players is to find out whether a sound has finished playing.
Lots more detail needed to be confident of your requirement. Here's my first rough interpretation of what you mean. Please correct this where necessary.
You have a project where you need a sound file SD\mp3\0001.mp3 to be played CONTINUOUSLY. No interruptions even when another file is playing.
On power-up nothing will play. File 0001.mp3 will be started by pressing button sw1 and it must play to its end.
One of the other two files will play by pressing either button sw2 or sw3. That file too must play to its end, unless interrupted by the other. After 0002 or 0003 ends, 0001 will usually still be playing (but not always).
BTW, I'd guess that over 90% of users of this DF Robot module do so with the DFR library, DFRobotDFPlayerMini.h, like me. Over recent months I've tried several libraries (and non-library methods) and find that despite its flaws it's the best for me. But note that I'm a relatively novice 'Arduino programmer', with no C/C++ training. Hundreds of sketch examples using it are available as are posts/articles about its documentation errors and gaps. What prompted you to use the DFPlayerMini_Fast.h library?
EDIT: I forgot to ask: are the (mono) sounds to be delivered from a single speaker?
How do I utilize the hardware serial ports, instead of the software serial ports?
That's the only thing that you got wrong but the rest pretty accurate! So the on startup I want to play an ambient sound on player1 and player2 is use to play sound bytes like you described.
This library has a play.loop() function, it will use for the ambient sound only.
2 different speaker with 2 different amplifier. So one for ambient sound and the other one for sound bytes.
I can see how processing two player modules, amplifiers and speakers from one Arduino is likely to get messy. If user interaction is confined to button pressing, have you ruled out by-passing the Arduino altogether and using the modules in simple key mode, as shown in DFR's documentation? https://wiki.dfrobot.com/DFPlayer_Mini_SKU_DFR0299
But if, as I expect, you do want to control the playing programmatically, I'm afraid I'm not going to be able to help using your current library, but hopefully the more experienced folk here can.
BTW, one general point in case you're not already aware of it. There are many versions of this module on the market and performance is inconsistent. I've often spent hours struggling with a problem that disappears when I swap it for another.
Did you read the DFR documentation I referenced? ADkey control allows 20 buttons, so that specific requirement you mention would be no obstacle. But (and we're still guessing here!) if you need control based on sensors, time, etc, then of course you'll need your Arduino.
DFPlayerMini_Fast player1;
DFPlayerMini_Fast player2;
//inside setup()
Serial2.begin(); // hardware uart
Serial3.begin(); // hardware uart
player1.begin(Serial2); // Serial2 uart assoc'd with player1
player2.begin(Serial3); // Serial3 uart assoc'd with player2
// issuing commands --
player1.play(1); // some long track
player2.play(1); // short track
delay(500); // long enough to clear (2)'s time
player2.play(3); // some other short time track
Details of which pins to use on Mega can be found on this page. With hardware serial, the pins cannot be chosen, they are fixed, and so don't need to be specified in your code.
#include "Arduino.h"
#include "SoftwareSerial.h"
#include "DFPlayerMini_Fast.h"
DFPlayerMini_Fast playerAmbiant;
DFPlayerMini_Fast playerSound;
int sw1 = 49;
int sw2 = 51;
int sw3 = 53;
void setup()
{
Serial2.begin(9600); // hardware uart
Serial3.begin(9600); // hardware uart
Serial.begin(115200);
playerAmbiant.begin(Serial2); // Serial2 uart assoc'd with player1
playerSound.begin(Serial3); // Serial3 uart assoc'd with player2
//3 Bouton pour les sound bytes
pinMode(sw1, INPUT_PULLUP); //Define each button as input with pullup
pinMode(sw2, INPUT_PULLUP);
pinMode(sw3, INPUT_PULLUP);
//Pin pour LED du bouton
pinMode(2, OUTPUT);
playerAmbiant.volume(30);
playerSound.volume(30);
delay(500);
playerAmbiant.play(1);
delay(500);
Serial.println("Looping Ambiant Track 1");
}
void loop()
{
digitalWrite(2, HIGH); // turn the LED on (HIGH is the voltage level)
delay(500); // wait for a second
digitalWrite(2, LOW); // turn the LED off by making the voltage LOW
delay(500); // wait for a second
Serial.println("Checking button press");
if (!digitalRead(sw1))
{
playerSound.play(1); // Play audio track 0001
delay(1000);
Serial.println("Play Sound 1");
}
if (!digitalRead(sw2))
{
playerSound.play(2); // Play audio track 0002
delay(1000);
Serial.println("Play Sound 2");
}
if (!digitalRead(sw3))
{
playerSound.play(3); // Play audio track 0003
delay(1000);
Serial.println("Play Sound 3");
}
}