English
EspaƱol
PCBWAY PCB service

PCBWAY PCB service

PCBONLINE PCB service






DFplayer mp3 arduino code


Download the .zip file below. Unzip it and open it in Arduino IDE. Compile and upload. Remember, you need the DFplayer library below as well.







Dwonlaod the DFplayer library here:




 /*
  DFplayer test example
  http://www.electronoobs.com
  http://www.youtube.com/c/ELECTRONOOBS
 */
 
#include <SoftwareSerial.h>
#include <DFMiniMp3.h>


int sw1 = 3;
int sw2 = 4;
int sw3 = 5;


class Mp3Notify
{
public:
  static void OnError(uint16_t errorCode)
  {
    // see DfMp3_Error for code meaning
    Serial.println();
    Serial.print("Com Error ");
    Serial.println(errorCode);
  }

  static void OnPlayFinished(uint16_t globalTrack)
  {
    Serial.println();
    Serial.print("Play finished for #");
    Serial.println(globalTrack);   
  }

  static void OnCardOnline(uint16_t code)
  {
    Serial.println();
    Serial.print("Card online ");
    Serial.println(code);     
  }

  static void OnCardInserted(uint16_t code)
  {
    Serial.println();
    Serial.print("Card inserted ");
    Serial.println(code); 
  }

  static void OnCardRemoved(uint16_t code)
  {
    Serial.println();
    Serial.print("Card removed ");
    Serial.println(code);  
  }
};


DFMiniMp3<HardwareSerial, Mp3Notify> mp3(Serial);





void setup() 
{
  //3 push buttons with pulldown
  pinMode(sw1,INPUT);
  pinMode(sw2,INPUT);
  pinMode(sw3,INPUT);
  
  Serial.begin(115200);  
  mp3.begin();
  uint16_t volume = mp3.getVolume();
  mp3.setVolume(30);  
  uint16_t count = mp3.getTotalTrackCount();
}





void waitMilliseconds(uint16_t msWait)
{
  uint32_t start = millis();
  
  while ((millis() - start) < msWait)
  {
    // calling mp3.loop() periodically allows for notifications 
    // to be handled without interrupts
    mp3.loop(); 
    delay(1);
  }
}

void loop() 
{

   if(digitalRead(sw1))
  {    
      mp3.playMp3FolderTrack(1);  // Play track 0001
      delay(1000); 
  }
  
  if(digitalRead(sw2))
  {    
      mp3.playMp3FolderTrack(2);  // Play track 0002
      delay(1000); 
  }
  
  if(digitalRead(sw3))
  {    
      mp3.playMp3FolderTrack(3);  // Play track 0003
      delay(1000); 
  }
}//end of void