Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /***************************************************
  2. DFPlayer - A Mini MP3 Player For Arduino
  3.  <https://www.dfrobot.com/product-1121.html>
  4.  
  5.  ***************************************************
  6.  This example shows the basic function of library for DFPlayer.
  7.  
  8.  Created 2016-12-07
  9.  By [Angelo qiao](Angelo.qiao@dfrobot.com)
  10.  
  11.  GNU Lesser General Public License.
  12.  See <http://www.gnu.org/licenses/> for details.
  13.  All above must be included in any redistribution
  14.  ****************************************************/
  15.  
  16. /***********Notice and Trouble shooting***************
  17.  1.Connection and Diagram can be found here
  18.  <https://www.dfrobot.com/wiki/index.php/DFPlayer_Mini_SKU:DFR0299#Connection_Diagram>
  19.  2.This code is tested on Arduino Uno, Leonardo, Mega boards.
  20.  ****************************************************/
  21.  
  22. #include "Arduino.h"
  23. #include "SoftwareSerial.h"
  24. #include "DFRobotDFPlayerMini.h"
  25.  
  26. SoftwareSerial mySoftwareSerial(10, 11); // RX, TX
  27. DFRobotDFPlayerMini myDFPlayer;
  28. void printDetail(uint8_t type, int value);
  29.  
  30. int buttonState;
  31. int lastButtonState = HIGH;
  32. unsigned long lastDebounceTime = 0;
  33.  
  34. int buttonState2;
  35. int lastButtonState2 = HIGH;
  36. unsigned long lastDebounceTime2 = 0;
  37.  
  38. int buttonState3;
  39. int lastButtonState3 = HIGH;
  40. unsigned long lastDebounceTime3 = 0;
  41.  
  42. int buttonState4;
  43. int lastButtonState4 = HIGH;
  44. unsigned long lastDebounceTime4 = 0;
  45.  
  46.  
  47.  
  48. void setup()
  49. {
  50.   mySoftwareSerial.begin(9600);
  51.   Serial.begin(115200);
  52.  
  53.   Serial.println();
  54.   Serial.println(F("DFRobot DFPlayer Mini Demo"));
  55.   Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)"));
  56.  
  57.   if (!myDFPlayer.begin(mySoftwareSerial)) {  //Use softwareSerial to communicate with mp3.
  58.     Serial.println(F("Unable to begin:"));
  59.     Serial.println(F("1.Please recheck the connection!"));
  60.     Serial.println(F("2.Please insert the SD card!"));
  61.     while(true){
  62.       delay(0); // Code to compatible with ESP8266 watch dog.
  63.     }
  64.   }
  65.   Serial.println(F("DFPlayer Mini online."));
  66.  
  67.   pinMode (5, INPUT_PULLUP);
  68.   pinMode (4, INPUT_PULLUP);
  69.   pinMode (3, INPUT_PULLUP);
  70.   pinMode (2, INPUT_PULLUP);
  71.   myDFPlayer.volume(10);  //Set volume value. From 0 to 30
  72.  
  73. }
  74.  
  75. void loop()
  76. {
  77.  int reading = digitalRead (5);
  78.  if (reading != lastButtonState){
  79.   lastDebounceTime = millis () ;
  80.   }
  81.   if ((millis() - lastDebounceTime) > 50){
  82.     if (reading !=lastButtonState){
  83.       buttonState = reading;
  84.       if (buttonState ==LOW) {
  85.         myDFPlayer.play(1);
  86.       }
  87.     }
  88.   }
  89.   lastButtonState = reading;
  90.  
  91.  int reading2 = digitalRead (4);
  92.  if (reading2 != lastButtonState2){
  93.   lastDebounceTime2 = millis () ;
  94.   }
  95.   if ((millis() - lastDebounceTime2) > 50){
  96.     if (reading2 !=lastButtonState2){
  97.       buttonState2 = reading2;
  98.       if (buttonState2 ==LOW) {
  99.         myDFPlayer.play(2);
  100.       }
  101.     }
  102.   }
  103.   lastButtonState2 = reading2;
  104.  
  105.  int reading3 = digitalRead (3);
  106.  if (reading3 != lastButtonState3){
  107.   lastDebounceTime3 = millis () ;
  108.   }
  109.   if ((millis() - lastDebounceTime3) > 50){
  110.     if (reading3 !=lastButtonState3){
  111.       buttonState3 = reading3;
  112.       if (buttonState3 ==LOW) {
  113.         myDFPlayer.play(3);
  114.       }
  115.     }
  116.   }
  117.   lastButtonState3 = reading3;
  118.  
  119.  int reading4 = digitalRead (2);
  120.  if (reading4 != lastButtonState4){
  121.   lastDebounceTime4 = millis () ;
  122.   }
  123.   if ((millis() - lastDebounceTime4) > 50){
  124.     if (reading4 !=lastButtonState4){
  125.       buttonState4 = reading4;
  126.       if (buttonState4 ==LOW) {
  127.         myDFPlayer.play(4);
  128.       }
  129.     }
  130.   }
  131.   lastButtonState4 = reading4;
  132.  
  133. }
  134.  
  135. void printDetail(uint8_t type, int value){
  136.   switch (type) {
  137.     case TimeOut:
  138.       Serial.println(F("Time Out!"));
  139.       break;
  140.     case WrongStack:
  141.       Serial.println(F("Stack Wrong!"));
  142.       break;
  143.     case DFPlayerCardInserted:
  144.       Serial.println(F("Card Inserted!"));
  145.       break;
  146.     case DFPlayerCardRemoved:
  147.       Serial.println(F("Card Removed!"));
  148.       break;
  149.     case DFPlayerCardOnline:
  150.       Serial.println(F("Card Online!"));
  151.       break;
  152.     case DFPlayerUSBInserted:
  153.       Serial.println("USB Inserted!");
  154.       break;
  155.     case DFPlayerUSBRemoved:
  156.       Serial.println("USB Removed!");
  157.       break;
  158.     case DFPlayerPlayFinished:
  159.       Serial.print(F("Number:"));
  160.       Serial.print(value);
  161.       Serial.println(F(" Play Finished!"));
  162.       break;
  163.     case DFPlayerError:
  164.       Serial.print(F("DFPlayerError:"));
  165.       switch (value) {
  166.         case Busy:
  167.           Serial.println(F("Card not found"));
  168.           break;
  169.         case Sleeping:
  170.           Serial.println(F("Sleeping"));
  171.           break;
  172.         case SerialWrongStack:
  173.           Serial.println(F("Get Wrong Stack"));
  174.           break;
  175.         case CheckSumNotMatch:
  176.           Serial.println(F("Check Sum Not Match"));
  177.           break;
  178.         case FileIndexOut:
  179.           Serial.println(F("File Index Out of Bound"));
  180.           break;
  181.         case FileMismatch:
  182.           Serial.println(F("Cannot Find File"));
  183.           break;
  184.         case Advertise:
  185.           Serial.println(F("In Advertise"));
  186.           break;
  187.         default:
  188.           break;
  189.       }
  190.       break;
  191.     default:
  192.       break;
  193.   }
  194.  
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement