Df player bluetooth

Hi everyone. I am trying to control a DF Player mini with a HC-05 Bluetooth module using an APP I made to build a robot with voice and moving eyes using 8x8 led matrix. However, after some time searching on internet I end up with a two SoftwareSerial connection code.
Despite it working well at the start, after pressing again the audio button it completely loses the connection with the Bluetooth. So I am not sure why I can connect yet not disconnect the Mp3 DF player serial communication from the arduino and the APP.

Thanks.

//Librerias
#include <SoftwareSerial.h>
#include "DFRobotDFPlayerMini.h"
#include "LedControl.h" // Libreria para controlar Matriz

//Configuracion del audio/////////////////////////////////////////////

// Use pins 2 and 3 to communicate with DFPlayer Mini
static const uint8_t MP3_TX = 11; // Connects to module's RX 
static const uint8_t MP3_RX = 10; // Connects to module's TX 
static const uint8_t TOOTH_TX = 1; // Connects to module's RX 
static const uint8_t TOOTH_RX = 0; // Connects to module's TX 

SoftwareSerial Mp3Serial(MP3_RX, MP3_TX);
SoftwareSerial TOOTHSerial(TOOTH_RX, TOOTH_TX);

// Create the Player object
DFRobotDFPlayerMini player;

int estado="s";
int AUDIO=LOW;
int LED=13;
int A=1;
unsigned long start_time=millis();
unsigned long current;

/*PLANO PARA EL MONTAJE de los ojos /////////////////////////////////////////
Pines: 7 = DIN  6 =CLK  5 = CS 
5V - GND 
*/

LedControl lc=LedControl(7, 5, 6, 2); //Pines + 2 dado a que son 2 Pantallas
#define demora 200 
byte neutro[8] = {B00111100,B01111110,B11111111,B11100111,B11100111,B11111111,B01111110,B00111100};
byte abajo[8]={B00111100,B01111110,B11111111,B10011111,B10011111,B11111111,B01111110,B00111100};
byte arriba[8]={B00111100,B01111110,B11111111,B11111001,B11111001,B11111111,B01111110,B00111100};
byte izquierda[8]={B00111100,B01100110,B11100111,B11111111,B11111111,B11111111,B01111110,B00111100};
byte derecha[8]={B00111100,B01111110,B11111111,B11111111,B11111111,B11100111,B01100110,B00111100};
byte mimir[8]={B00011000,B00111000,B01110000,B11100000,B11100000,B01110000,B00111000,B00011000};
byte linea[8]={B00011000,B00011000,B00011000,B00011000,B00011000,B00011000,B00011000,B00011000};
byte muerto[8]={B11000011,B11100111,B01111110,B00111100,B00111100,B01111110,B11100111,B11000011};
byte off[8]={B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000};
///////////////////////////////////////////////////////////////////////////////////////
void setup() {
  Serial.begin (9600);
  //Matriz 1  
  lc.shutdown(0,false);       // enciende la matriz
  lc.setIntensity(0,4);       // establece brillo
  lc.clearDisplay(0);         // Se limpia la matriz
//Matriz 2 
  lc.shutdown(1,false);       // enciende la matriz
  lc.setIntensity(1,4);       // establece brillo
  lc.clearDisplay(1);         // Se limpia la matriz
  pinMode(LED,OUTPUT);
  TOOTHSerial.begin  (9600); //Inicia SoftwareSerial para el modulo Bluettoh
  Mp3Serial.begin (9600);  //Inicia SoftwareSerial para el modulo DFPlayer Mini
  delay(10); 
  start_time=millis();
  player.begin(Mp3Serial);

}

void loop() {
  TOOTHSerial.listen();
  if(TOOTHSerial.available()>0){        // lee el mensaje enviado desde la APP bluetooth y almacena en variable estado
  estado = TOOTHSerial.read();  
   
  if (estado =='v'){
          Mp3Serial.listen();      //Pone a la escucha el modulo DFPlayer Mini
          int track = random(1,6);
          player.volume(20);
          player.play(track);
          Mp3Serial.stopListening();
          TOOTHSerial.listen();  
   }
   else if (estado =='a'){
       TOOTHSerial.listen();
       digitalWrite(13,HIGH);
       mostrar_ojo_arri (); 
       delay(demora);

   }
   else if (estado =='r'){
       TOOTHSerial.listen();
       digitalWrite(13,LOW);
       mostrar_ojo_abajo (); 
       delay(demora);
   }
   else if (estado =='i'){
       TOOTHSerial.listen();
       digitalWrite(13,LOW);
       mostrar_ojo_iz (); 
       delay(demora);
   }
   else if (estado =='d'){
       TOOTHSerial.listen();
       digitalWrite(13,LOW);
       mostrar_ojo_der (); 
       delay(demora);
   }
   else if (estado =='t'){
      TOOTHSerial.listen();
      a_mimir();
      delay(demora);
   }
   else if (estado =='s'){
      TOOTHSerial.listen();
      mostrar_ojo_centro (); 
      delay(demora);
   }
   else if(estado =='h'){
    serio();
    delay(demora); 
   }
   else if (estado =='l'){
      TOOTHSerial.listen();
      KO (); 
      delay(demora);
   }
   else{
    Mp3Serial.listen();
    player.stop();
    TOOTHSerial.listen();
   }
  }
}
//FUNCIONES PARA LOS OJOS////////////////////////////////////////////////////////////////////////////////////
void mostrar_ojo_centro() {
  for (int i = 0; i < 8; i++) {
    lc.setRow(0,i,neutro[i]);//Matriz 1 
    lc.setRow(1,i,neutro[i]);//Matriz 2
  }
}
void mostrar_ojo_abajo() {
  for (int i = 0; i < 8; i++) {
    lc.setRow(0,i,abajo[i]);//Matriz 1 
    lc.setRow(1,i,abajo[i]);//Matriz 2
  }
  }
void mostrar_ojo_arri() {
  for (int i = 0; i < 8; i++) {
    lc.setRow(0,i,arriba[i]);//Matriz 1 
    lc.setRow(1,i,arriba[i]);//Matriz 2
  }
}
void mostrar_ojo_iz() {
  for (int i = 0; i < 8; i++) {
    lc.setRow(0,i,izquierda[i]);//Matriz 1 
    lc.setRow(1,i,izquierda[i]);//Matriz 2
  }
}
void mostrar_ojo_der() {
  for (int i = 0; i < 8; i++) {
    lc.setRow(0,i,derecha[i]);//Matriz 1 
    lc.setRow(1,i,derecha[i]);//Matriz 2
  }
}
void a_mimir() {
  for (int i = 0; i < 8; i++) {
    lc.setRow(0,i,mimir[i]);//Matriz 1 
    lc.setRow(1,i,mimir[i]);//Matriz 2
  }
}
void serio() {
  for (int i = 0; i < 8; i++) {
    lc.setRow(0,i,linea[i]);//Matriz 1 
    lc.setRow(1,i,linea[i]);//Matriz 2
  }
}
void KO() {
  for (int i = 0; i < 8; i++) {
    lc.setRow(0,i,muerto[i]);//Matriz 1 
    lc.setRow(1,i,muerto[i]);//Matriz 2
  }
}
void parpadear() {
  for (int i = 0; i < 8; i++) {
    lc.setRow(0,i,off[i]);//Matriz 1 
    lc.setRow(1,i,off[i]);//Matriz 2
  }
}

Welcome to the forum

Using more than a single instance of SoftwareSerial is almost certain to fail under normal circumstances and guaranteed to fail if you use pins 0 and 1 on the majority of Arduino boards

Which Arduino board are you using ?

I am currently using an Arduino UNO board. And I just tried changing the Bluetooth to different pins and but still fails after the first track is played.

if you have more than a single instance of SoftwareSerial then I am not surprised

You could put one device on SoftwareSerial with pins of your choice and the other on Serial using pins 0 and 1. However, you will then not be able to use the Serial monitor and may need to disconnect pins 0 and 1 in order to upload sketches

@juanoh-05
Hi, using the SoftwareSerial on pins 0 & 1 is completely senseless in any circumstances. You must use standard Serial instead.

Maybe I've missed it but your code does not seem to include initialisation of the DFR Player module?

Yes every library is already installed. The main issue I have occurs after playing a song for the second time. Everything suddenly stops responding.

I don't have your hw and I'm not brave enough to use pins 0 & 1 for serial on my nano! But after stripping your code of everything except the DFR Player stuff, I ran the code below.

// Plays about 1 s of each random track

#include <SoftwareSerial.h>
#include "DFRobotDFPlayerMini.h"

// Use pins 8 and 9 to communicate with DFPlayer Mini
static const uint8_t MP3_TX = 9; // Connects to module's RX
static const uint8_t MP3_RX = 8; // Connects to module's TX
SoftwareSerial Mp3Serial(MP3_RX, MP3_TX);
// Create the Player object
DFRobotDFPlayerMini player;

///////////////////////////////////////////////////////////////////////////////////////
void setup()
{
  Mp3Serial.begin (9600);  //Inicia SoftwareSerial para el modulo DFPlayer Mini
  player.begin(Mp3Serial);
  Serial.print("end of setup");
}

void loop()
{
  int track = random(151, 156); // All longer than 1 s
  player.volume(20);
  player.play(track);
  Mp3Serial.stopListening();
}

I haven't used that stopListening statement before. It effectively seems to allow about 1 s of playing on each iteration. Just wondering if your tracks are audible during that second? BTW, why not a simple delay instead?

Have you tried a fresh fully formatted SD card? There may be unwanted tracks within the first six.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.