Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Include the NewPing library
- #include <NewPing.h>
- #include "Arduino.h"
- #include "SoftwareSerial.h"
- #include "DFRobotDFPlayerMini.h"
- //SoftwareSerial mySoftwareSerial(3,11); // RX, TX
- SoftwareSerial mySoftwareSerial(3,11); // RX, TX
- DFRobotDFPlayerMini myDFPlayer;
- void printDetail(uint8_t type, int value);
- int maxCount = 8;
- int countIndex = maxCount;
- int lastTime = 0;
- int maxWaitingTime = 1000;
- int waitingTime = maxWaitingTime;
- int frontDist;
- int underDist;
- bool passedDoor = false;
- int falseFrames = 0;
- int maxFalseFrames = 3000;
- int mean;
- // This simple code allow you to send data from Arduino to Unity3D.
- // uncomment "SERIAL_USB" if you're using non ARM CPU (Arduino Uno, Arduino Mega, ..)
- #define SERIAL_USB
- //Make new sonar sensors
- NewPing underSonar(5,6);
- NewPing frontSonar(10,9);
- void setup() {
- // put your setup code here, to run once:
- mySoftwareSerial.begin(9600);
- Serial.begin(9600);
- //Serial.println(F("DFRobot DFPlayer Mini Demo"));
- //Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)"));
- //if (!myDFPlayer.begin(mySoftwareSerial)) { //Use softwareSerial to communicate with mp3.
- //Serial.println(F("Unable to begin:"));
- //Serial.println(F("1.Please recheck the connection!"));
- //Serial.println(F("2.Please insert the SD card!"));
- //while(true);
- //}
- //Serial.println(F("DFPlayer Mini online."));
- myDFPlayer.setTimeOut(500); //Set serial communictaion time out 500ms
- myDFPlayer.volume(30); //Set volume value (0~30).
- }
- void loop() {
- delay(20);
- //Make a variable and assign a value to it
- underDist = underSonar.ping_cm();
- frontDist = frontSonar.ping_cm();
- if(underDist < 100 && underDist > 0){
- passedDoor = true;
- }
- // check if a person is detected in front
- if(passedDoor == true && countIndex <7 && countIndex != 0){
- playHappy();
- passedDoor = false;
- }
- if (frontDist < 100 && frontDist > 10) {
- falseFrames = 0;
- int currTime = millis();
- if(currTime >= lastTime + waitingTime){
- //Serial.println(countIndex);
- myDFPlayer.play(countIndex);
- countIndex = countIndex - 1;
- lastTime = currTime;
- waitingTime = waitingTime - 100;
- if(countIndex < 0){
- if(passedDoor == false){
- playMean();
- }
- Reset();
- }
- }
- }else{
- falseFrames = falseFrames + 1;
- if(falseFrames > maxFalseFrames){
- falseFrames = 0;
- Reset();
- //Serial.println("reset falseFrames");
- }
- }
- }
- void playHappy(){
- //Serial.println("HAPPY RAMSAY");
- int happy = random(25,28);
- myDFPlayer.play(happy);
- Serial.println("0");
- passedDoor = false;
- Reset();
- delay(2500);
- }
- void playMean() {
- //Serial.println("MEAN RAMSAY");
- int test = random (0,10);
- if (test > 3){
- mean = random(9,24);
- } else{
- mean = 17;
- }
- myDFPlayer.play(mean);
- Reset();
- if (mean == 17) {
- Serial.println("2");
- } else {
- Serial.println("1");
- }
- delay(2500);
- }
- void Reset(){
- //Serial.println("resetting count down");
- countIndex = maxCount;
- waitingTime = maxWaitingTime;
- passedDoor = false;
- }
- void printDetail(uint8_t type, int value){
- if(false){
- switch (type) {
- case TimeOut:
- //Serial.println(F("Time Out!"));
- break;
- case WrongStack:
- //Serial.println(F("Stack Wrong!"));
- break;
- case DFPlayerCardInserted:
- //Serial.println(F("Card Inserted!"));
- break;
- case DFPlayerCardRemoved:
- //Serial.println(F("Card Removed!"));
- break;
- case DFPlayerCardOnline:
- //Serial.println(F("Card Online!"));
- break;
- case DFPlayerPlayFinished:
- //Serial.print(F("Number:"));
- //Serial.print(value);
- //Serial.println(F(" Play Finished!"));
- break;
- case DFPlayerError:
- //Serial.print(F("DFPlayerError:"));
- switch (value) {
- case Busy:
- //Serial.println(F("Card not found"));
- break;
- case Sleeping:
- //Serial.println(F("Sleeping"));
- break;
- case SerialWrongStack:
- //Serial.println(F("Get Wrong Stack"));
- break;
- case CheckSumNotMatch:
- //Serial.println(F("Check Sum Not Match"));
- break;
- case FileIndexOut:
- //Serial.println(F("File Index Out of Bound"));
- break;
- case FileMismatch:
- //Serial.println(F("Cannot Find File"));
- break;
- case Advertise:
- //Serial.println(F("In Advertise"));
- break;
- default:
- break;
- }
- break;
- default:
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement