Advertisement
Guest User

Untitled

a guest
Jan 28th, 2020
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.74 KB | None | 0 0
  1. //Include the NewPing library
  2. #include <NewPing.h>
  3. #include "Arduino.h"
  4. #include "SoftwareSerial.h"
  5. #include "DFRobotDFPlayerMini.h"
  6.  
  7. //SoftwareSerial mySoftwareSerial(3,11); // RX, TX
  8.  
  9. SoftwareSerial mySoftwareSerial(3,11); // RX, TX
  10. DFRobotDFPlayerMini myDFPlayer;
  11. void printDetail(uint8_t type, int value);
  12.  
  13. int maxCount = 8;
  14. int countIndex = maxCount;
  15. int lastTime = 0;
  16.  
  17. int maxWaitingTime = 1000;
  18. int waitingTime = maxWaitingTime;
  19.  
  20. int frontDist;
  21. int underDist;
  22. bool passedDoor = false;
  23.  
  24. int falseFrames = 0;
  25. int maxFalseFrames = 3000;
  26. int mean;
  27.  
  28. // This simple code allow you to send data from Arduino to Unity3D.
  29. // uncomment "SERIAL_USB" if you're using non ARM CPU (Arduino Uno, Arduino Mega, ..)
  30. #define SERIAL_USB
  31.  
  32.  
  33. //Make new sonar sensors
  34. NewPing underSonar(5,6);
  35. NewPing frontSonar(10,9);
  36.  
  37. void setup() {
  38. // put your setup code here, to run once:
  39.  
  40. mySoftwareSerial.begin(9600);
  41. Serial.begin(9600);
  42.  
  43. //Serial.println(F("DFRobot DFPlayer Mini Demo"));
  44. //Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)"));
  45.  
  46. //if (!myDFPlayer.begin(mySoftwareSerial)) { //Use softwareSerial to communicate with mp3.
  47. //Serial.println(F("Unable to begin:"));
  48. //Serial.println(F("1.Please recheck the connection!"));
  49. //Serial.println(F("2.Please insert the SD card!"));
  50. //while(true);
  51. //}
  52. //Serial.println(F("DFPlayer Mini online."));
  53.  
  54. myDFPlayer.setTimeOut(500); //Set serial communictaion time out 500ms
  55. myDFPlayer.volume(30); //Set volume value (0~30).
  56. }
  57.  
  58. void loop() {
  59. delay(20);
  60. //Make a variable and assign a value to it
  61. underDist = underSonar.ping_cm();
  62. frontDist = frontSonar.ping_cm();
  63.  
  64. if(underDist < 100 && underDist > 0){
  65. passedDoor = true;
  66. }
  67.  
  68. // check if a person is detected in front
  69. if(passedDoor == true && countIndex <7 && countIndex != 0){
  70. playHappy();
  71. passedDoor = false;
  72. }
  73.  
  74. if (frontDist < 100 && frontDist > 10) {
  75.  
  76.  
  77. falseFrames = 0;
  78.  
  79. int currTime = millis();
  80.  
  81. if(currTime >= lastTime + waitingTime){
  82. //Serial.println(countIndex);
  83. myDFPlayer.play(countIndex);
  84.  
  85. countIndex = countIndex - 1;
  86.  
  87. lastTime = currTime;
  88. waitingTime = waitingTime - 100;
  89.  
  90. if(countIndex < 0){
  91. if(passedDoor == false){
  92. playMean();
  93. }
  94. Reset();
  95. }
  96. }
  97. }else{
  98.  
  99. falseFrames = falseFrames + 1;
  100.  
  101. if(falseFrames > maxFalseFrames){
  102. falseFrames = 0;
  103. Reset();
  104.  
  105. //Serial.println("reset falseFrames");
  106. }
  107. }
  108. }
  109.  
  110. void playHappy(){
  111. //Serial.println("HAPPY RAMSAY");
  112. int happy = random(25,28);
  113. myDFPlayer.play(happy);
  114. Serial.println("0");
  115. passedDoor = false;
  116. Reset();
  117. delay(2500);
  118.  
  119. }
  120.  
  121. void playMean() {
  122. //Serial.println("MEAN RAMSAY");
  123. int test = random (0,10);
  124. if (test > 3){
  125. mean = random(9,24);
  126. } else{
  127. mean = 17;
  128. }
  129. myDFPlayer.play(mean);
  130. Reset();
  131. if (mean == 17) {
  132. Serial.println("2");
  133. } else {
  134. Serial.println("1");
  135. }
  136. delay(2500);
  137. }
  138.  
  139. void Reset(){
  140. //Serial.println("resetting count down");
  141. countIndex = maxCount;
  142. waitingTime = maxWaitingTime;
  143.  
  144. passedDoor = false;
  145. }
  146.  
  147. void printDetail(uint8_t type, int value){
  148. if(false){
  149. switch (type) {
  150. case TimeOut:
  151. //Serial.println(F("Time Out!"));
  152. break;
  153. case WrongStack:
  154. //Serial.println(F("Stack Wrong!"));
  155. break;
  156. case DFPlayerCardInserted:
  157. //Serial.println(F("Card Inserted!"));
  158. break;
  159. case DFPlayerCardRemoved:
  160. //Serial.println(F("Card Removed!"));
  161. break;
  162. case DFPlayerCardOnline:
  163. //Serial.println(F("Card Online!"));
  164. break;
  165. case DFPlayerPlayFinished:
  166. //Serial.print(F("Number:"));
  167. //Serial.print(value);
  168. //Serial.println(F(" Play Finished!"));
  169. break;
  170. case DFPlayerError:
  171. //Serial.print(F("DFPlayerError:"));
  172. switch (value) {
  173. case Busy:
  174. //Serial.println(F("Card not found"));
  175. break;
  176. case Sleeping:
  177. //Serial.println(F("Sleeping"));
  178. break;
  179. case SerialWrongStack:
  180. //Serial.println(F("Get Wrong Stack"));
  181. break;
  182. case CheckSumNotMatch:
  183. //Serial.println(F("Check Sum Not Match"));
  184. break;
  185. case FileIndexOut:
  186. //Serial.println(F("File Index Out of Bound"));
  187. break;
  188. case FileMismatch:
  189. //Serial.println(F("Cannot Find File"));
  190. break;
  191. case Advertise:
  192. //Serial.println(F("In Advertise"));
  193. break;
  194. default:
  195. break;
  196. }
  197. break;
  198. default:
  199. break;
  200. }
  201. }
  202.  
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement