#include <Arduino.h> #include <SoftwareSerial.h> #include <DFRobotDFPlayerMini.h>
DFRobotDFPlayerMini mp3(8,9);
int left_trigPin = 7;
int left_echoPin = 6;
int right_trigPin = 2;
int right_echoPin = 3;
int center_trigPin = 4;
int center_echoPin = 5;
void setup() {
pinMode(left_trigPin,OUTPUT);
pinMode(left_echoPin,INPUT);
pinMode(right_trigPin,OUTPUT);
pinMode(right_echoPin,INPUT);
pinMode(center_trigPin,OUTPUT);
pinMode(center_echoPin,INPUT);
Serial.begin(115200);
mp3.begin(9600);
mp3.reset();
mp3.setVolume(50);
mp3.setLoopMode(MP3_LOOP_NONE);
}
void loop(){
left();
right();
center();
}
void left(){
delay(500);// reading will be taken after ....miliseconds
Serial.println("\n");
int duration, distance;
digitalWrite (left_trigPin, HIGH);
delayMicroseconds (10);
digitalWrite (left_trigPin, LOW);
duration = pulseIn (left_echoPin, HIGH);
distance = (duration/2) / 29.1;
//distance= duration*0.034/2;
if (distance < 30) { // Change the number for long or short distances.
Serial.print("Left Distance");
Serial.print(distance);
mp3.playFileByIndexNumber(2);
}
}
void right(){
delay(500);// reading will be taken after ....miliseconds
Serial.println("\n");
int duration, distance;
digitalWrite (right_trigPin, HIGH);
delayMicroseconds (10);
digitalWrite (right_trigPin, LOW);
duration = pulseIn (right_echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance <30) { // Change the number for long or short distances.
Serial.print("Right Distance");
Serial.print(distance);
mp3.playFileByIndexNumber(3);
}
}
void center(){
delay(500);// reading will be taken after ....miliseconds
Serial.println("\n");
int duration, distance;
digitalWrite (center_trigPin, HIGH);
delayMicroseconds (10);
digitalWrite (center_trigPin, LOW);
duration = pulseIn (center_echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance <30) { // Change the number for long or short distances.
Serial.print("Center Distance");
Serial.print(distance);
mp3.playFileByIndexNumber(1);
}
}
Please follow the advice given in the link below when posting code. Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination
I had been using this code to combined with arduino uno, 3*us sensor, and df player for object detection to get a output in audio format. simultaneously i am getting the error "Mp3 scope not found"
In file included from C:\Users\91701\Documents\Arduino\sketch_aug28c\sketch_aug28c.ino:3:0:
C:\Program Files (x86)\Arduino\libraries\DFRobotDFPlayerMini-1.0.5/DFRobotDFPlayerMini.h:44:23: error: expected unqualified-id before numeric constant #define DFPlayerError 6
^
C:\Users\91701\Documents\Arduino\sketch_aug28c\sketch_aug28c.ino:4:1: note: in expansion of macro 'DFPlayerError'
DFPlayerError mp3(8,9);
^~~~~
C:\Users\91701\Documents\Arduino\sketch_aug28c\sketch_aug28c.ino: In function 'void setup()':
sketch_aug28c:19:3: error: 'mp3' was not declared in this scope
mp3.begin(9600);
^~~
sketch_aug28c:22:19: error: 'MP3_LOOP_NONE' was not declared in this scope
mp3.setLoopMode(MP3_LOOP_NONE);
^~~~~
C:\Users\91701\Documents\Arduino\sketch_aug28c\sketch_aug28c.ino: In function 'void left()':
sketch_aug28c:42:6: error: 'mp3' was not declared in this scope
mp3.playFileByIndexNumber(2);
^~~
C:\Users\91701\Documents\Arduino\sketch_aug28c\sketch_aug28c.ino: In function 'void right()':
sketch_aug28c:57:5: error: 'mp3' was not declared in this scope
mp3.playFileByIndexNumber(3);
^~~
C:\Users\91701\Documents\Arduino\sketch_aug28c\sketch_aug28c.ino: In function 'void center()':
sketch_aug28c:72:5: error: 'mp3' was not declared in this scope
mp3.playFileByIndexNumber(1);
^~~
exit status 1
'mp3' was not declared in this scope
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
#include <Arduino.h> #include <SoftwareSerial.h> #include <DFRobotDFPlayerMini.h>
SoftwareSerial mySoftwareSerial(10, 11); // RX, TX
DFRobotDFPlayerMini myDFPlayer;
int left_trigPin = 7;
int left_echoPin = 6;
int right_trigPin = 2;
int right_echoPin = 3;
int center_trigPin = 4;
int center_echoPin = 5;
void setup() {
pinMode(left_trigPin, OUTPUT);
pinMode(left_echoPin, INPUT);
pinMode(right_trigPin, OUTPUT);
pinMode(right_echoPin, INPUT);
pinMode(center_trigPin, OUTPUT);
pinMode(center_echoPin, INPUT);
Serial.begin(115200);
mySoftwareSerial.begin(9600);
Serial.begin(115200);
Serial.println();
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) {
delay(0); // Code to compatible with ESP8266 watch dog.
}
}
Serial.println(F("DFPlayer Mini online."));
myDFPlayer.volume(10); //myDFPlayer.setLoopMode(myDFPlayer_LOOP_NONE);
}
void loop() {
left();
right();
center();
}
void left() {
delay(500);// reading will be taken after ....miliseconds
Serial.println("\n");
int duration, distance;
digitalWrite (left_trigPin, HIGH);
delayMicroseconds (10);
digitalWrite (left_trigPin, LOW);
duration = pulseIn (left_echoPin, HIGH);
distance = (duration / 2) / 29.1;
//distance= duration*0.034/2;
if (distance < 30) { // Change the number for long or short distances.
Serial.print("Left Distance");
Serial.print(distance);
myDFPlayer.play(2);
}
}
void right() {
delay(500);// reading will be taken after ....miliseconds
Serial.println("\n");
int duration, distance;
digitalWrite (right_trigPin, HIGH);
delayMicroseconds (10);
digitalWrite (right_trigPin, LOW);
duration = pulseIn (right_echoPin, HIGH);
distance = (duration / 2) / 29.1;
if (distance < 30) { // Change the number for long or short distances.
Serial.print("Right Distance");
Serial.print(distance);
myDFPlayer.play(3);
}
}
void center() {
delay(500);// reading will be taken after ....miliseconds
Serial.println("\n");
int duration, distance;
digitalWrite (center_trigPin, HIGH);
delayMicroseconds (10);
digitalWrite (center_trigPin, LOW);
duration = pulseIn (center_echoPin, HIGH);
distance = (duration / 2) / 29.1;
if (distance < 30) { // Change the number for long or short distances.
Serial.print("Center Distance");
Serial.print(distance);
myDFPlayer.play(1);
}
}
I use the above code ...i.,e the three ultra sonic sensor on object detection, the df player mini module will play th mp3 audio file in sd card by speaker...the code seems got verified by arduino software but i dont get an audio output on object detection...(we use us sensor Hc-SRO4,dfplayer mini, ardrino UNO, 5 watt speaker...
kindly any one help me out with this ie,. is there any technical error!
Can you get the DFRobotDFPlayerMini library GetStarted example to play the first three seconds of each of your songs? That would prove that the DFPlayerMini was wired correctly and working and that your song files are in the right format.