Advertisement
Guest User

Untitled

a guest
May 20th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.03 KB | None | 0 0
  1. // Code based on example and library for DFPlayer
  2. // https://wiki.dfrobot.com/DFPlayer_Mini_SKU_DFR0299#Connection_Diagram
  3.  
  4. /****************************************************
  5. Example created 2016-12-07
  6. By [Angelo qiao](Angelo.qiao@dfrobot.com)
  7.  
  8. GNU Lesser General Public License.
  9. See <http://www.gnu.org/licenses/> for details.
  10. All above must be included in any redistribution
  11. ****************************************************/
  12.  
  13.  
  14. #include "Arduino.h"
  15. #include "SoftwareSerial.h"
  16. #include "DFRobotDFPlayerMini.h"
  17. #include <AltSoftSerial.h>
  18.  
  19. // High / low threshold of analog read value that triggers music fragments to play.
  20. unsigned int ldr_threshold = 300;
  21. int Opdroogtijd = 5*60*1000;
  22.  
  23. // Define pins of LDRs.
  24. #define LDR_ONE A0
  25. #define LDR_TWO A1
  26. #define LDR_THREE A2
  27. #define LDR_FOUR A4
  28. #define LDR_FIVE A5
  29.  
  30. unsigned long time_at_playing_sound_one = 0;
  31. unsigned long time_at_playing_sound_two = 0;
  32. unsigned long time_at_playing_sound_three = 0;
  33. unsigned long time_at_playing_sound_four = 0;
  34. unsigned long time_at_playing_sound_five = 0;
  35.  
  36.  
  37. AltSoftSerial altSerial;
  38. SoftwareSerial AltSoftSerial (21, 20); // Setting pins 0 and 1 (RX, TX) for serial communication.
  39.  
  40. DFRobotDFPlayerMini myDFPlayer;
  41. void printDetail(uint8_t type, int value);
  42.  
  43. void setup()
  44. {
  45. pinMode(LDR_ONE,INPUT); // One pin per LDR
  46. pinMode(LDR_TWO,INPUT);
  47. pinMode(LDR_THREE,INPUT);
  48. pinMode(LDR_FOUR,INPUT);
  49. pinMode(LDR_FIVE,INPUT);
  50. pinMode(13, OUTPUT);
  51. altSerial.begin(9600);
  52.  
  53. Serial.begin(115200);
  54.  
  55. /* Serial.println();
  56. Serial.println(F("DFRobot DFPlayer Mini Demo"));
  57. Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)"));
  58. */
  59. myDFPlayer.begin(altSerial);
  60. // if (!myDFPlayer.begin(altSerial)) { //Use softwareSerial to communicate with mp3.
  61. // // digitalWrite(13, HIGH);
  62. // Serial.println(F("Unable to begin:"));
  63. // Serial.println(F("1.Please recheck the connection!"));
  64. // Serial.println(F("2.Please insert the SD card!"));
  65. // while(true);
  66. // }
  67. Serial.println(F("DFPlayer Mini online."));
  68.  
  69.  
  70. myDFPlayer.setTimeOut(500); //Set serial communictaion time out 500ms
  71.  
  72. // Set volume
  73. myDFPlayer.volume(29); //Set volume value (0~30).
  74.  
  75. // Set EQ
  76. myDFPlayer.EQ(DFPLAYER_EQ_NORMAL);
  77.  
  78.  
  79. //Set SD as default
  80. myDFPlayer.outputDevice(DFPLAYER_DEVICE_SD);
  81.  
  82.  
  83. myDFPlayer.playFolder(1, 1); //play specific mp3 in SD:/15/004.mp3; Folder Name(1~99); File Name(1~255)
  84.  
  85. /*// Read imformation
  86. Serial.println(myDFPlayer.readState()); //read mp3 state
  87. Serial.println(myDFPlayer.readVolume()); //read current volume
  88. Serial.println(myDFPlayer.readEQ()); //read EQ setting
  89. Serial.println(myDFPlayer.readFileCounts()); //read all file counts in SD card
  90. Serial.println(myDFPlayer.readCurrentFileNumber()); //read current play file number
  91. Serial.println(myDFPlayer.readFileCountsInFolder(3)); //read fill counts in folder SD:/03
  92. */}
  93.  
  94. void loop()
  95. {
  96. // static unsigned long timer = millis();
  97.  
  98. // READ SENSOR VALUES OF LDRS
  99. /*
  100. int sensorValueA0 = analogRead(LDR_ONE);
  101. Serial.println(sensorValueA0);
  102.  
  103. int sensorValueA1 = analogRead(LDR_TWO);
  104. Serial.println(sensorValueA1);
  105.  
  106. int sensorValueA2 = analogRead(LDR_THREE);
  107. Serial.println(sensorValueA2);
  108.  
  109. int sensorValueA3 = analogRead(LDR_FOUR);
  110. Serial.println(sensorValueA3);
  111.  
  112. int sensorValueA4 = analogRead(LDR_FIVE);
  113. Serial.println(sensorValueA4);
  114. */
  115. int TEST = millis()-time_at_playing_sound_one;
  116. Serial.println(TEST);
  117.  
  118. if (analogRead(LDR_ONE) < ldr_threshold && ((millis()-time_at_playing_sound_one ) > Opdroogtijd) || time_at_playing_sound_one == 0)) {
  119. // Serial.println(F("No touchy pin 14"));
  120. myDFPlayer.play(1);
  121. time_at_playing_sound_one = millis();
  122. }
  123.  
  124. if (analogRead(LDR_TWO) < ldr_threshold && ((millis()-time_at_playing_sound_two ) > Opdroogtijd) || time_at_playing_sound_two == 0)){
  125. // Serial.println(F("No touchy pin 15"));
  126. myDFPlayer.play(2);
  127. }
  128.  
  129. if (analogRead(LDR_THREE) < ldr_threshold && ((millis()-time_at_playing_sound_three ) > Opdroogtijd) || time_at_playing_sound_three == 0))
  130. // Serial.println(F("No touchy pin 16"));
  131. myDFPlayer.play(3);
  132. }
  133.  
  134. if (analogRead(LDR_FOUR) < ldr_threshold && ((millis()-time_at_playing_sound_four ) > Opdroogtijd) || time_at_playing_sound_four == 0))
  135. // Serial.println(F("No touchy pin 16"));
  136. myDFPlayer.play(4);
  137. }
  138.  
  139. if (analogRead(LDR_FIVE) < ldr_threshold && ((millis()-time_at_playing_sound_five ) > Opdroogtijd) || time_at_playing_sound_five == 0))
  140. // Serial.println(F("No touchy pin 16"));
  141. myDFPlayer.play(5);
  142. }
  143. }
  144.  
  145. void printDetail(uint8_t type, int value){
  146. switch (type) {
  147.  
  148. case TimeOut:
  149. Serial.println(F("Time Out!"));
  150. break;
  151. case WrongStack:
  152. Serial.println(F("Stack Wrong!"));
  153. break;
  154. case DFPlayerCardInserted:
  155. Serial.println(F("Card Inserted!"));
  156. break;
  157. case DFPlayerCardRemoved:
  158. Serial.println(F("Card Removed!"));
  159. break;
  160. case DFPlayerCardOnline:
  161. Serial.println(F("Card Online!"));
  162. break;
  163. case DFPlayerPlayFinished:
  164. Serial.print(F("Number:"));
  165. Serial.print(value);
  166. Serial.println(F(" Play Finished!"));
  167. break;
  168. case DFPlayerError:
  169. Serial.print(F("DFPlayerError:"));
  170. switch (value) {
  171. case Busy:
  172. Serial.println(F("Card not found"));
  173. break;
  174. case Sleeping:
  175. Serial.println(F("Sleeping"));
  176. break;
  177. case SerialWrongStack:
  178. Serial.println(F("Get Wrong Stack"));
  179. break;
  180. case CheckSumNotMatch:
  181. Serial.println(F("Check Sum Not Match"));
  182. break;
  183. case FileIndexOut:
  184. Serial.println(F("File Index Out of Bound"));
  185. break;
  186. case FileMismatch:
  187. Serial.println(F("Cannot Find File"));
  188. break;
  189. case Advertise:
  190. Serial.println(F("In Advertise"));
  191. break;
  192. default:
  193. break;
  194. }
  195. break;
  196. default:
  197. break;
  198. }
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement