I have 2 DFmini MP3 players attached to one Nano. This is not my first MP3 project, but the first with 2 players.
I can only get one or the other to play when I don’t start the other one.
Both players will start and return the message that they have started, and the two “delay loops” will run. When the sketch reaches the point where it is to play the selected file, the sketch locks up.
If I start only one player and run only its respective “delay loop,” that player will play.
As you can see in the attached code,
I have included the libraries “SoftwareSerial.h” and “DFRobotDFPlayerMini.h” and
I have declared two different players and SoftwareSerial serials.
I have resistors (1K) on both the TX and RX pins. This is something I have always done with my mp3 projects. It might be overkill, but it works for me.
I use the “busy pin” of the player to get feedback from the player. I have tried commenting that part out, and the sketch still locks up.
Here is my wiring diagram.
#include <Arduino.h>
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"
void startPlayer_1();
void startPlayer_2();
void Player_1_delayLoop();
void Player_2_delayLoop();
#define BusyPin_1 12
#define BusyPin_2 13
#define rxPin_1 10 // pin 3 on player 1 with 1K resistor
#define txPin_1 11 // pin 2 on player 1 with 1K resistor
#define rxPin_2 8 // pin 3 on player 2 with 1K resistor
#define txPin_2 9 // pin 2 on player 2 with 1K resistor
DFRobotDFPlayerMini Mp3Player1;
SoftwareSerial MySerial_1(rxPin_1, txPin_1);
DFRobotDFPlayerMini Mp3Player2;
SoftwareSerial MySerial_2(rxPin_2, txPin_2);
unsigned long player_1_loopDelay = 0;
unsigned long player_1_PreviousMillis;
unsigned long player_2_loopDelay = 0;
unsigned long player_2_PreviousMillis;
int player_1_fileNumber;
int player_2_fileNumber;
int Player_1_lastPlayed = 0;
int Player_2_lastPlayed = 0;
void setup() {
Serial.begin(9600);
pinMode(BusyPin_1, INPUT_PULLUP); // PIN 12 ON NANO
pinMode(BusyPin_2, INPUT_PULLUP); // PIN 13 ON NANO
startPlayer_1();
startPlayer_2();
}
void loop() {
Player_1_delayLoop();
Player_2_delayLoop();
}
/* delay loops follow */
void Player_1_delayLoop() {
if (player_1_loopDelay == 0) {
player_1_PreviousMillis = millis();
player_1_loopDelay = 10000;
}
unsigned long player_1_CurrentMillis = millis();
if (player_1_CurrentMillis - player_1_PreviousMillis >= player_1_loopDelay) {
randomSeed(analogRead(0));
player_1_fileNumber = random(1, 13);
while (player_1_fileNumber == Player_1_lastPlayed) { // prvents playing same file as before
randomSeed(analogRead(0));
player_1_fileNumber = random(1, 13);
}
Serial.print("Player 1 file number = ");
Serial.println(player_1_fileNumber);
Player_1_lastPlayed = player_1_fileNumber;
Serial.println("attempting to play player 1");
Mp3Player1.play(player_1_fileNumber);
int x = digitalRead(BusyPin_1);
Serial.print("the int x = ");
Serial.println(x);
while (x == 1) {
x = digitalRead(BusyPin_1);
}
delay(250);
while (x == 0) {
Serial.print("Playing File # ");
Serial.println(player_1_fileNumber);
x = digitalRead(BusyPin_1);
}
if (x == 1) {
player_1_loopDelay = 0;
}
}
player_1_CurrentMillis = millis();
Serial.println("Player1 loop delay");
}
/* end of delay loop 1 */
void Player_2_delayLoop() {
if (player_2_loopDelay == 0) {
player_2_PreviousMillis = millis();
player_2_loopDelay = 30000;
}
unsigned long player_2_CurrentMillis = millis();
if (player_2_CurrentMillis - player_2_PreviousMillis >= player_2_loopDelay) {
randomSeed(analogRead(0));
player_2_fileNumber = random(1, 38);
while (player_2_fileNumber == Player_2_lastPlayed) { // prvents playing same file as before
randomSeed(analogRead(0));
player_2_fileNumber = random(1, 38);
}
Serial.print("Player 2 file number = ");
Serial.println(player_2_fileNumber);
Player_2_lastPlayed = player_2_fileNumber;
Serial.println("attempting to play player 2");
Mp3Player2.play(player_2_fileNumber);
int x = digitalRead(BusyPin_2);
while (x == 1) {
x = digitalRead(BusyPin_2);
}
delay(250);
while (x == 0) {
Serial.print("Playing File # ");
Serial.println(player_2_fileNumber);
x = digitalRead(BusyPin_2);
}
if (x == 1) {
player_2_loopDelay = 0;
}
}
player_2_CurrentMillis = millis();
Serial.println("Player2 loop delay");
}
/* end of delay loop 2 */
/* player startups follow */
void startPlayer_1() {
Serial.println("Starting Player 1");
MySerial_1.begin(9600);
if (!Mp3Player1.begin(MySerial_1)) {
Serial.println("Player 1 Did Not Start");
while (true)
;
}
Mp3Player1.setTimeOut(500); // Set Serial communictaion time out 500ms
Mp3Player1.volume(30); // Set volume value (0~30).
Mp3Player1.EQ(DFPLAYER_EQ_BASS); // Set EQ to BASS (normal/pop/rock/jazz/classic/bass)
Mp3Player1.outputDevice(DFPLAYER_DEVICE_SD); // Set device we use SD as default
Mp3Player1.enableDAC();
Serial.println("Player 1 Online");
}
void startPlayer_2() {
Serial.println("Starting Player 2");
MySerial_2.begin(9600);
if (!Mp3Player2.begin(MySerial_2)) {
Serial.println("Player 2 Did Not Start");
while (true)
;
}
Mp3Player2.setTimeOut(500); // Set Serial communictaion time out 500ms
Mp3Player2.volume(30); // Set volume value (0~30).
Mp3Player2.EQ(DFPLAYER_EQ_BASS); // Set EQ to BASS (normal/pop/rock/jazz/classic/bass)
Mp3Player2.outputDevice(DFPLAYER_DEVICE_SD); // Set device we use SD as default
Mp3Player2.enableDAC();
Serial.println("Player 2 Online");
}
Here is an example of the serial monitor output when both players are started up. The sketch stops at line 67
Starting Player 1
Player 1 Online
Starting Player 2
Player 2 Online
Player1 loop delay
Player2 loop delay
Player1 loop delay
Player2 loop delay
Player1 loop delay
Player2 loop delay
Player1 loop delay
Player2 loop delay
Player1 loop delay
Player2 loop delay
Player 1 file number = 8
attempting to play player 1
When I start only one of the players, I get this on the serial monitor, and the sound plays
Starting Player 1
Player 1 Online
Player1 loop delay
Player2 loop delay
Player1 loop delay
Player2 loop delay
Player1 loop delay
Player2 loop delay
Player1 loop delay
Player2 loop delay
Player 1 file number = 5
attempting to play player 1
the int x = 1
Playing File # 5
Playing File # 5
Playing File # 5
That output is the result of lines 63 through 81
Serial.print("Player 1 file number = ");
Serial.println(player_1_fileNumber);
Player_1_lastPlayed = player_1_fileNumber;
Serial.println("attempting to play player 1");
Mp3Player1.play(player_1_fileNumber);
int x = digitalRead(BusyPin_1);
Serial.print("the int x = ");
Serial.println(x);
while (x == 1) {
x = digitalRead(BusyPin_1);
}
delay(250);
while (x == 0) {
Serial.print("Playing File # ");
Serial.println(player_1_fileNumber);
x = digitalRead(BusyPin_1);
}
Has anyone run into this issue, and if so, how did you fix it?
Thank you in advance,
Gary