Theudas

Untitled

Apr 28th, 2017
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.29 KB | None | 0 0
  1. #include "Arduino.h"
  2. #include "SoftwareSerial.h"
  3. #include "DFRobotDFPlayerMini.h"
  4.  
  5. SoftwareSerial mySoftwareSerial(D1, D2); // RX, TX
  6. DFRobotDFPlayerMini myDFPlayer;
  7. void printDetail(uint8_t type, int value);
  8. // use isOpen to only play one sound after opening
  9. bool isOpen = false;
  10.  
  11.  
  12. void setup()
  13. {
  14.   mySoftwareSerial.begin(9600);
  15.   Serial.begin(115200);
  16.   pinMode(D6, OUTPUT);
  17.   pinMode(D5, INPUT);
  18.  
  19.   Serial.println();
  20.   Serial.println(F("DFRobot DFPlayer Mini Demo"));
  21.   Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)"));
  22.  
  23.   if (!myDFPlayer.begin(mySoftwareSerial)) {  //Use softwareSerial to communicate with mp3.
  24.     Serial.println(F("Unable to begin:"));
  25.     Serial.println(F("1.Please recheck the connection!"));
  26.     Serial.println(F("2.Please insert the SD card!"));
  27.     while(true);
  28.   }
  29.   Serial.println(F("DFPlayer Mini online."));
  30.  
  31.   myDFPlayer.setTimeOut(500); //Set serial communictaion time out 500ms
  32.  
  33.   //----Set volume----
  34.   myDFPlayer.volume(20);  //Set volume value (0~30).
  35.  
  36.   //----Set different EQ----
  37.   myDFPlayer.EQ(DFPLAYER_EQ_NORMAL);
  38.  
  39.   //----Set device we use SD as default----
  40.   myDFPlayer.outputDevice(DFPLAYER_DEVICE_SD);
  41.  
  42.  
  43.   myDFPlayer.randomAll(); //Random play all the mp3.
  44.  
  45. }
  46.  
  47. void loop()
  48. {
  49.   static unsigned long timer = millis();
  50.  
  51.   if (millis() - timer > 100) {
  52.     timer = millis();
  53.     digitalWrite(D6, HIGH);
  54.     delay(10);
  55.       if(digitalRead(D5)){
  56.        
  57.         isOpen = false;
  58.       }else{
  59.         if(!isOpen){
  60.           myDFPlayer.next();
  61.         }
  62.         isOpen = true;
  63.       }
  64.        Serial.println(digitalRead(D5));
  65.     digitalWrite(D6, LOW);
  66.    
  67.   }
  68.  
  69.   if (myDFPlayer.available()) {
  70.     printDetail(myDFPlayer.readType(), myDFPlayer.read()); //Print the detail message from DFPlayer to handle different errors and states.
  71.   }
  72. }
  73.  
  74. void printDetail(uint8_t type, int value){
  75.   switch (type) {
  76.     case TimeOut:
  77.       Serial.println(F("Time Out!"));
  78.       break;
  79.     case WrongStack:
  80.       Serial.println(F("Stack Wrong!"));
  81.       break;
  82.     case DFPlayerCardInserted:
  83.       Serial.println(F("Card Inserted!"));
  84.       break;
  85.     case DFPlayerCardRemoved:
  86.       Serial.println(F("Card Removed!"));
  87.       break;
  88.     case DFPlayerCardOnline:
  89.       Serial.println(F("Card Online!"));
  90.       break;
  91.     case DFPlayerPlayFinished:
  92.       Serial.print(F("Number:"));
  93.       Serial.print(value);
  94.       Serial.println(F(" Play Finished!"));
  95.       break;
  96.     case DFPlayerError:
  97.       Serial.print(F("DFPlayerError:"));
  98.       switch (value) {
  99.         case Busy:
  100.           Serial.println(F("Card not found"));
  101.           break;
  102.         case Sleeping:
  103.           Serial.println(F("Sleeping"));
  104.           break;
  105.         case SerialWrongStack:
  106.           Serial.println(F("Get Wrong Stack"));
  107.           break;
  108.         case CheckSumNotMatch:
  109.           Serial.println(F("Check Sum Not Match"));
  110.           break;
  111.         case FileIndexOut:
  112.           Serial.println(F("File Index Out of Bound"));
  113.           break;
  114.         case FileMismatch:
  115.           Serial.println(F("Cannot Find File"));
  116.           break;
  117.         case Advertise:
  118.           Serial.println(F("In Advertise"));
  119.           break;
  120.         default:
  121.           break;
  122.       }
  123.       break;
  124.     default:
  125.       break;
  126.   }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment