I want to use 2 distance sensors and a DFPlayer Mini to play MP3 files when a distance is crossed.
Everything in the serial goes weird and nothing works if I combine the code. Separating the MP3 code and sensor code does work.
It should countdown from 8 to 1 and then print text. This does work without the DFPLayer.
if I comment the 'myDFPlayer.play(1);' the countdown does work
if its uncommented the countdown and player both break
I was trying around with different pins, maybe it was caused by timers, but that didn't help
//Include the NewPing library
#include <NewPing.h>
#include "Arduino.h"
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"
SoftwareSerial mySoftwareSerial(3,11); // RX, TX
DFRobotDFPlayerMini myDFPlayer;
void printDetail(uint8_t type, int value);
//int frontPin = 13;
//int underPin = 12;
int maxCount = 8;
int countIndex = maxCount;
int lastTime = 0;
int maxWaitingTime = 1000;
int waitingTime = maxWaitingTime;
bool spotted = false;
int frontDist = 0;
int underDist = 0;
bool passedDoor = false;
int falseFrames = 0;
int maxFalseFrames = 2000;
// This simple code allow you to send data from Arduino to Unity3D.
// uncomment "NATIVE_USB" if you're using ARM CPU (Arduino DUE, Arduino M0, ..)
//#define NATIVE_USB
// 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:
Serial.begin(9600);
}
void loop() {
//Make a variable and assign a value to it
underDist = underSonar.ping_cm();
frontDist = frontSonar.ping_cm();
//spotted = digitalRead(frontPin);
//under = digitalRead(underPin);
if(underDist < 100 && underDist > 0){
passedDoor = true;
}
//Serial.println("front: " + front);
//Serial.println("under: " + under);
// check if a person is detected in front
if (frontDist < 100 && frontDist > 10) {
falseFrames = 0;
int currTime = millis();
if(currTime >= lastTime + waitingTime){
Serial.println(countIndex);
myDFPlayer.play(1);
countIndex = countIndex - 1;
lastTime = currTime;
waitingTime = waitingTime - 100;
if(passedDoor == true){
Serial.println("HAPPY RAMSAY");
Reset();
}
if(countIndex < 0){
if(passedDoor == false){
Serial.println("MEAN RAMSAY");
}
Reset();
}
}
}else{
falseFrames = falseFrames + 1;
if(falseFrames > maxFalseFrames){
falseFrames = 0;
Reset();
Serial.println("reset falseFrames");
}
}
//delay(100);
}
void Reset(){
//Serial.println("resetting count down");
countIndex = maxCount;
waitingTime = maxWaitingTime;
passedDoor = false;
}
If you need any more info let me know