Go Down

Topic: DFPlayer Mini Loops First Track but won't Loop Second Track? (Read 381 times) previous topic - next topic

GorillaDetective

Hi All,

I am using a Teensy 3.2 connected to DFPlayer Mini using Hardware Serial. I can get the player to loop the first track on the SD card no problem. I then pause the track and try to loop the second track after a button press. The second track does not play at all and the LED on the player does not light up at all. Also, I tried using the readFileCounts(DFPLAYER_DEVICE_SD) function to read how many files are on the SD card and it returns -1 even though there are 2 tracks properly labeled as 0001.mp3 and 0002.mp3 on the card. I made sure the SD card is formatted in FAT32 properly and the tracks were named on the PC before being added to the SD card and they were added in the order they are numbered. I am sure I am missing something small but I can't see if yet. Any help would be appreciated!

The relevant code snippets are most likely:

These functions:
Code: [Select]

void LoopTrack(int track) {
if(isPlaying == false){
  Serial.println("Do we get here?");
  myDFPlayer.loop(track);  //Loop the requested mp3 
  isPlaying= true;
  } 
}
void Pause() {
if(isPlaying == true ){
      myDFPlayer.pause();  //pause the mp3
      isPlaying = false;
    }
}


and then this is where I try to loop the second track:

Code: [Select]

void CountDown() {
  LoopTrack(2);
  if (millis() - previousMillis >= LedDelayTimeIdle) {
    previousMillis = millis();
    // Turn all three LEDs on one at a time and leave them on
    for (byte i = 0; i < NrLeds; i++) {
      digitalWrite(LedPins[i], nextLed > i);
    }
    nextLed++;
    //Flash all three LEDs every 500 milliseconds 3 times
    if (nextLed > NrLeds + 1) {
      mode = 3;
    }
  }
}


Complete code below:
Code: [Select]

#include "Arduino.h"
#include "Bounce2.h"  // Include the Bounce2 library to handle button debouuncing
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"
#define HWSERIAL Serial1

//DFPlayer Settings
//SoftwareSerial mySerial(0, 1); //RX, TX
DFRobotDFPlayerMini myDFPlayer;
void printDetail(uint8_t type, int value);

//pin definitions
const byte ButtonPin = 21;
const byte LedPins[] = {2, 3, 4};
const byte NrLeds = sizeof(LedPins);
const byte RedLedPin = 20;

//other constants
const unsigned int LedDelayTimeIdle = 1000;  // 1 second delay between our LED flashes in our Idle and Arm functions
const unsigned int LedDelayTimeFlash = 500;  // 1/2 second delay between our LED flashes in our FlashAllLights Function
const unsigned int LedDelayTimeRead = 5;

//variables
Bounce modeButton = Bounce();    // Instantiate a Bounce object
unsigned long previousMillis = -LedDelayTimeIdle; // Forces the time trigger to start at startup
byte nextLed;  //Variable to help us keep track of what LED we are in our loops
byte mode;  //Variable to keep track of what mode we are in based on button presses
boolean isPlaying = false;

void setup() {
  //mySerial.begin(9600);
  myDFPlayer.begin(Serial1);
  HWSERIAL.begin(9600);
  Serial.begin(115200);
  Serial.println("Are we setting up?");
  Serial.println();
  Serial.println(F("DFRobot DFPlayer Mini Demo"));
  Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)"));
 
  if (!myDFPlayer.begin(Serial1)) {  //Use softwareSerial to communicate with mp3.
    Serial.println(F("Unable to begin:"));
    Serial.println(F("1.Please recheck the connection!"));
    Serial.println(F("2.Please insert the SD card!"));
    while(true);{
      delay(0); // Code to compatible with ESP8266 watch dog.
    }
  }
  Serial.println(F("DFPlayer Mini online."));
  myDFPlayer.volume(10);  //Set volume value (0~30).
  myDFPlayer.EQ(DFPLAYER_EQ_NORMAL);
  myDFPlayer.outputDevice(DFPLAYER_DEVICE_SD);
  //Set ledPins to output
  for (byte i = 0; i < NrLeds; i++) {
    pinMode(LedPins[i], OUTPUT);
  }
  pinMode(RedLedPin, OUTPUT);
  digitalWrite(RedLedPin, HIGH);
  modeButton.attach(ButtonPin, INPUT);  // Attach the debouncer to the button pin
  modeButton.interval(50);     // Set the button debouncer interval in ms
}

void loop() {
  if (myDFPlayer.available()) {
    printDetail(myDFPlayer.readType(), myDFPlayer.read()); //Print the detail message from DFPlayer to handle different errors and states.
  }
 
  //Serial.println("Are we working?");
  // Checks the current amount of button presses and moves to the correct state based on that
  CheckMode();
  //Constantly checking if the button has been pressed during the Arm or Idle mode only
  if (mode == 0 || mode == 1) {
    UpdateButton();
  }
}



void Idle() {
  //modeButton.update();
  UpdateButton();
  // Our starting state. Leds cycle at 1 second intervals
  digitalWrite(8, HIGH); // Turn on the red LED and keep it on
  if (millis() - previousMillis >= LedDelayTimeIdle) {
    previousMillis = millis();
    //only make the nextLed light up
    for (byte i = 0; i < NrLeds; i++) {
      digitalWrite(LedPins[i], i == nextLed);
    }
    //goto next led
    nextLed++;
    if (nextLed >= NrLeds) {
      nextLed = 0;
    }
  }
}

void Arm() {
  LoopTrack(1);
  // Turn the first two LEDs on and leave them on
  for (byte i = 0; i < 2; i++) {
    digitalWrite(LedPins[i], HIGH);
  }
  //Check for 1 second delay and then turn third LED on and off
  if (millis() - previousMillis >= LedDelayTimeIdle) {
    previousMillis = millis();
    digitalWrite(LedPins[2], !digitalRead(LedPins[2]));
  }
}

void CountDown() {
  LoopTrack(2);
  if (millis() - previousMillis >= LedDelayTimeIdle) {
    previousMillis = millis();
    // Turn all three LEDs on one at a time and leave them on
    for (byte i = 0; i < NrLeds; i++) {
      digitalWrite(LedPins[i], nextLed > i);
    }
    nextLed++;
    //Flash all three LEDs every 500 milliseconds 3 times
    if (nextLed > NrLeds + 1) {
      mode = 3;
    }
  }
}

void FlashAllLights() {
  //Check if enough time has passed to blink all three LEDs
  if (millis() - previousMillis >= LedDelayTimeFlash) {
    previousMillis = millis();
    for (byte i = 0; i < NrLeds; i++) {
      digitalWrite(LedPins[i], !digitalRead(LedPins[i]));
      digitalWrite(RedLedPin, !digitalRead(RedLedPin));
      if (nextLed >= 10) {
        mode = 0;
      }
    }
    nextLed++;
  }
}

void UpdateButton() {
  //Check if the button has been pressed and increment the mode variable by one for each time the button is pressed
  modeButton.update();
  if (modeButton.rose()) {
    Pause();
    mode++;
  }
}

void LoopTrack(int track) {
if(isPlaying == false){
  Serial.println("Do we get here?");
  myDFPlayer.loop(track);  //Loop the requested mp3 
  isPlaying= true;
  } 
}
void Pause() {
if(isPlaying == true ){
      myDFPlayer.pause();  //pause the mp3
      isPlaying = false;
    }
}

void CheckMode() {
  // Checks the current amount of button presses and moves to the correct state based on that
  if (mode == 0) {
    Idle(); 
  } else if (mode == 1) {
    Arm();
    //Reset loop counting variables for the next time we call the CountDown function
    nextLed = 0;
  } else if (mode == 2) {
    Serial.println(isPlaying);
    Serial.println(myDFPlayer.readFileCounts(DFPLAYER_DEVICE_SD));
    CountDown();
  } else if (mode == 3)  {
    FlashAllLights();
  }
}

void printDetail(uint8_t type, int value){
  switch (type) {
    case TimeOut:
      Serial.println(F("Time Out!"));
      break;
    case WrongStack:
      Serial.println(F("Stack Wrong!"));
      break;
    case DFPlayerCardInserted:
      Serial.println(F("Card Inserted!"));
      break;
    case DFPlayerCardRemoved:
      Serial.println(F("Card Removed!"));
      break;
    case DFPlayerCardOnline:
      Serial.println(F("Card Online!"));
      break;
    case DFPlayerUSBInserted:
      Serial.println("USB Inserted!");
      break;
    case DFPlayerUSBRemoved:
      Serial.println("USB Removed!");
      break;
    case DFPlayerPlayFinished:
      Serial.print(F("Number:"));
      Serial.print(value);
      Serial.println(F(" Play Finished!"));
      break;
    case DFPlayerError:
      Serial.print(F("DFPlayerError:"));
      switch (value) {
        case Busy:
          Serial.println(F("Card not found"));
          break;
        case Sleeping:
          Serial.println(F("Sleeping"));
          break;
        case SerialWrongStack:
          Serial.println(F("Get Wrong Stack"));
          break;
        case CheckSumNotMatch:
          Serial.println(F("Check Sum Not Match"));
          break;
        case FileIndexOut:
          Serial.println(F("File Index Out of Bound"));
          break;
        case FileMismatch:
          Serial.println(F("Cannot Find File"));
          break;
        case Advertise:
          Serial.println(F("In Advertise"));
          break;
        default:
          break;
      }
      break;
    default:
      break;
  }
}

Power_Broker

Not sure what's causing the prob, but it might help to use a more simple and lightweight DFPlayer library. I'd suggest trying this one. The only prob you might have is that you can't use myDFPlayer.readFileCounts().
"The desire that guides me in all I do is the desire to harness the forces of nature to the service of mankind."
   - Nikola Tesla

Go Up