Advertisement
JonD1988

DFPlayer_test

Dec 30th, 2021
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.   DFplayer test example
  3.   http://www.electronoobs.com
  4.   http://www.youtube.com/c/ELECTRONOOBS
  5.   Sketch Obtained from http://electronoobs.com/eng_arduino_tut22_code4.php
  6.  */
  7.  
  8. #include <SoftwareSerial.h>
  9. #include <DFMiniMp3.h>
  10.  
  11.  
  12. int sw1 = 3;
  13. int sw2 = 4;
  14. int sw3 = 5;
  15.  
  16.  
  17. class Mp3Notify
  18. {
  19. public:
  20.   static void OnError(uint16_t errorCode)
  21.   {
  22.     // see DfMp3_Error for code meaning
  23.     Serial.println();
  24.     Serial.print("Com Error ");
  25.     Serial.println(errorCode);
  26.   }
  27.  
  28.   static void OnPlayFinished(uint16_t globalTrack)
  29.   {
  30.     Serial.println();
  31.     Serial.print("Play finished for #");
  32.     Serial.println(globalTrack);  
  33.   }
  34.  
  35.   static void OnCardOnline(uint16_t code)
  36.   {
  37.     Serial.println();
  38.     Serial.print("Card online ");
  39.     Serial.println(code);    
  40.   }
  41.  
  42.   static void OnCardInserted(uint16_t code)
  43.   {
  44.     Serial.println();
  45.     Serial.print("Card inserted ");
  46.     Serial.println(code);
  47.   }
  48.  
  49.   static void OnCardRemoved(uint16_t code)
  50.   {
  51.     Serial.println();
  52.     Serial.print("Card removed ");
  53.     Serial.println(code);  
  54.   }
  55. };
  56.  
  57.  
  58. DFMiniMp3<HardwareSerial, Mp3Notify> mp3(Serial);
  59.  
  60.  
  61.  
  62.  
  63.  
  64. void setup()
  65. {
  66.   //3 push buttons with pulldown
  67.   pinMode(sw1,INPUT);
  68.   pinMode(sw2,INPUT);
  69.   pinMode(sw3,INPUT);
  70.  
  71.   Serial.begin(115200);  
  72.   mp3.begin();
  73.   uint16_t volume = mp3.getVolume();
  74.   mp3.setVolume(30);  
  75.   uint16_t count = mp3.getTotalTrackCount();
  76. }
  77.  
  78.  
  79.  
  80.  
  81.  
  82. void waitMilliseconds(uint16_t msWait)
  83. {
  84.   uint32_t start = millis();
  85.  
  86.   while ((millis() - start) < msWait)
  87.   {
  88.     // calling mp3.loop() periodically allows for notifications
  89.     // to be handled without interrupts
  90.     mp3.loop();
  91.     delay(1);
  92.   }
  93. }
  94.  
  95. void loop()
  96. {
  97.  
  98.    if(digitalRead(sw1))
  99.   {    
  100.       mp3.playMp3FolderTrack(1);  // Play track 0001
  101.       delay(1000);
  102.   }
  103.  
  104.   if(digitalRead(sw2))
  105.   {    
  106.       mp3.playMp3FolderTrack(2);  // Play track 0002
  107.       delay(1000);
  108.   }
  109.  
  110.   if(digitalRead(sw3))
  111.   {    
  112.       mp3.playMp3FolderTrack(3);  // Play track 0003
  113.       delay(1000);
  114.   }
  115. }//end of void
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement