2 DF Player Module with 2 cores of ESP32

As mentioned, I am allotting the following code for 2 DF Players to get operated with the 2 built in cores of ESP32 unit

4 of the GPIOs are defined as input

Rest in code below

#include <DFPlayerMini_Fast.h>
#include <SoftwareSerial.h>

#define LED1 2
#define LED2 15

const byte tap1 = 32;
const byte tap2 = 33;
const byte tap3 = 27;
const byte tap4 = 14;

const byte ledPin [] = {2,15};

TaskHandle_t Task1, Task2;
SemaphoreHandle_t baton;

// SoftwareSerial mySerial1(3, 1); // RX, TX

#define RXD2 16
#define TXD2 17
DFPlayerMini_Fast player1;

SoftwareSerial mySerial(18, 19); // RX, TX
DFPlayerMini_Fast player;

void Buttons(const byte pin, const byte ledPin){
  if(!digitalRead(pin)){
    digitalWrite(ledPin, HIGH);
  }else{
    digitalWrite(ledPin, LOW);
  }
}

void codeForTask1( void * parameter )
{

  for (;;) {
  // Buttons(tap1, LED2);
  // Buttons(tap2, LED2);

  if(!digitalRead(tap1))
  {
    // digitalWrite(ledPin,1);
    player.play(1);
    // Serial.print(player);
    while(!digitalRead(tap1));
  }
  if(!digitalRead(tap2))
  {
    // digitalWrite(ledPin,1);
    player.play(2);
    // Serial.print(player);
    while(!digitalRead(tap2));
  }

  }
  // delay(100);
}

void codeForTask2( void * parameter )
{
  for (;;) {
    // Buttons(tap3, LED1);
    // Buttons(tap4, LED1);
    if(!digitalRead(tap3))
  {
    // digitalWrite(ledPin,1);
    player1.play(1);
    // Serial.print(player);
    while(!digitalRead(tap3));
  }
  if(!digitalRead(tap4))
  {
    // digitalWrite(ledPin,1);
    player1.play(2);
    // Serial.print(player);
    while(!digitalRead(tap4));
  }
  }
  // delay(100);
}

// the setup function runs once when you press reset or power the board
void setup() {
  Serial.begin(9600);

  Serial2.begin(9600, SERIAL_8N1, RXD2, TXD2);
    if(player1.begin(Serial2, false))
      {
        // player1.volume(22);
        // player1.stop();
      }

  mySerial.begin(9600);
    if(player.begin(mySerial, false))
      {
        // player.volume(22);
        // player.stop();
      }

    pinMode(tap1, INPUT);
    pinMode(tap2, INPUT);
    pinMode(tap3, INPUT);
    pinMode(tap4, INPUT);

  baton = xSemaphoreCreateMutex();

  xTaskCreatePinnedToCore(
    codeForTask1,
    "Task1",
    1000,
    NULL,
    1,
    &Task1,
    1);

  // delay(500);  // needed to start-up task1
  //delay(100);

  xTaskCreatePinnedToCore(
    codeForTask2,
    "Task2",
    1000,
    NULL,
    1,
    &Task2,
    0);

    delay(100);

    pinMode(LED1, OUTPUT);
    pinMode(LED2, OUTPUT);
    
}


void loop() {
    delay(1);
}

Now I am operating a DF Player with 2 of the inputs with the core 0 and rest 2 inputs for second DF Player via core 1

Now when I Start the operation to play files pressing buttons randomly for both the core at same time, its working fine for some 5 seconds approx

After that suddenly the system stops responding for 1 or 2 seconds and then the original process retrieves

Again same 5 seconds play and then 1 or 2 seconds gap

and it continues . . . . .

While the process getting out of order for a second, I noticed that, it seems like both the cores are interrupting or cancelling each other or say fighting with each other

So is this an issue which is related to my code above or it's the DF player or the memory card unit ????

Please Comment and help me in solving

Thanks !!! :smiling_face: :blush:

How is it wired?
Is there one single SD card reader?
If so the first thought is that the tasks compete about using the SD card.
You need to check up the operative regarding sharing resources.

1 Like

Ya simply a single jumper wire from pin 17 to RX pin of DF Player, i.e. the pin just below the Vcc pin of the DF Player

Yes only a single card can be installed and eject on the top head of DF player.
There is no separate sd card reader for the purpose !!

Sir I am unable to get this point
Sorry, would you please elaborate a bit :blush:

You need a wire between their GND for it to work.

I don't know those multi core controllers but I know multi task system requirements.
When 2 different tasks request the same resource there must be some kind of scheduling as they can't access the same device at the same time.
The 2 tasks must have a mechanism to prevent simultaneous access.
It sounds like the tasks collide and then times out.

You mean to say that the ESP32 and DF Player would be on the same ground or something else ????

Their grounds are connected together !!!

Good! You didn't tell that in the verbal schematics. The disadvantage of using words instead of schematics, misunderstandings causing time delaying messages.

Ya ya I got the point
Actually I comnected all that stuff as per theory and rules needed

Only thing is that break in the process of plays

I sèe
So anything I can do to get that perfect mechanism ?

I have no knowledge to answer that question.
One idea could be Google on "2 cores sharing resource on ESP32", "2 cores, 1 ESP32".
The second tip is to wait for a knowing helper to step in. They usually do rather soon.

Please tell us why you think you need two separate cores for two (9600 baud) DFPlayers, and why you think you need software serial for that. Don't ask me, because I don't know.
Leo..

I applied Serial2 for both the players but at a time only one can operate and also it starts operating both the players with any of the buttons pressed

With other baud rates the system is not responding any of the way
I tried with several other changes but unable to make it

As I noticed, the Serial2 is the smoothest of all and allowing a continues play but only when a single core on which it is defined, is active !!

Ya Sir going that way and surfing through many sources

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