I can't relate my ultrasonic sensor HC-SR04 with my DF Player Mini

I have the code of the DFPlayer Mini. In it I save 4 audios. Depending of the distance (registered by the ultarsonic sensor) I would like to reproduce one of other audios.
Exemple:
1 Audio---- 5 cm of distance
2 Audio-----8 cm of distance
3 Audio-----10 cm distance
4 Audio----- 12 cm distance
Thanks a lot for your help, I love this forum all good people.

COde of df player

#include <SoftwareSerial.h>
#include <DFPlayer_Mini_Mp3.h>
 
SoftwareSerial DFPlayerSerial(10, 11); // RX, TX
 
/*
mp3_play();      //start play
mp3_play(5);   //play "mp3/0005.mp3"
mp3_pause();
mp3_stop();
mp3_next();      
mp3_prev();
 
mp3_set_volume(uint16_t volume);   //0~30
mp3_set_EQ();   //0~5
void mp3_single_loop(boolean state);   //set single loop
void mp3_random_play();
*/
 
void setup()
{
   Serial.begin(9600);
   DFPlayerSerial.begin(9600);
   mp3_set_serial(DFPlayerSerial);
   mp3_set_volume(15);
}
 
void loop()
{
   mp3_play(1);
   delay(6000);
   mp3_next();
   delay(6000);
   mp3_prev();
   delay(6000);
   mp3_play(4);
   delay(6000);
}

And here the code of the ultrasonic sensor

#include <NewPing.h>

const int UltrasonicPin = 5;
const int MaxDistance = 200;

NewPing sonar(UltrasonicPin, UltrasonicPin, MaxDistance);

void setup() {
  Serial.begin(9600);
}

void loop() {
  delay(50);                      // esperar 50ms entre pings (29ms como minimo)
  Serial.print(sonar.ping_cm()); // obtener el valor en cm (0 = fuera de rango)
  Serial.println("cm");
}

Thanks a lot if someone could help me I would be very grateful, I tried things but The mixing and my purpose doesnt work.

what did you try ?

I dont have any idea of how to do it. I dont know how to start. Thanks for your time

pepo177:
I dont have any idea of how to do it. I dont know how to start. Thanks for your time

you said " I tried things but The mixing and my purpose doesnt work."

Show us your attempt and explain the logic of your algorithm. that would be a good way to start exploring

3_play(5);   //play "mp3/0005.mp3"
mp3_pause();
mp3_stop();
mp3_next();     
mp3_prev();

mp3_set_volume(uint16_t volume);   //0~30
mp3_set_EQ();   //0~5
void mp3_single_loop(boolean state);   //set single loop
void mp3_random_play();
*/
#include <NewPing.h>

const int UltrasonicPin = 5;
const int MaxDistance = 200;

NewPing sonar(UltrasonicPin, UltrasonicPin, MaxDistance);

void setup()
{
  Serial.begin(9600);
  Serial.begin(9600);
  DFPlayerSerial.begin(9600);
  mp3_set_serial(DFPlayerSerial);
  mp3_set_volume(15);
 
}

void loop()
{
   delay(50);                      // esperar 50ms entre pings (29ms como minimo)
 Serial.print(sonar.ping_cm()); // obtener el valor en cm (0 = fuera de rango)
 Serial.println("cm");

  mp3_play(1);
  delay(6000);
  mp3_next();
  delay(6000);
  mp3_prev();
  delay(6000);
  mp3_play(4);
  delay(6000);

  if(distance=1; reproduce audio.1);
  if(distance=2; reproduce audio.2);
  if(distance=3; reproduce audio.3);
  if(distance=4; reproduce audio.4);
  if(distance=5; reproduce audio.5);
}

you need to properly install the libraries of course and import them in the code.

I'm not sure which DFPlayer_Mini_Mp3.h you are using, but this one is deprecated and they suggest using DFRobotDFPlayerMini

There is also DFPlayerMini_Fast that feels better.

here is a high level structure to start playing with

#include <NewPing.h> // https://bitbucket.org/teckel12/arduino-new-ping/downloads/
const byte UltrasonicPin = 5;
const unsigned int MaxDistance = 200;
NewPing sonar(UltrasonicPin, UltrasonicPin, MaxDistance);

#include <SoftwareSerial.h>
#include <DFPlayerMini_Fast.h> // https://github.com/PowerBroker2/DFPlayerMini_Fast
SoftwareSerial DFPlayerSerial(10, 11); // RX, TX
DFPlayerMini_Fast MP3Player;

void setup()
{
  Serial.begin(9600);
  DFPlayerSerial.begin(9600);
  MP3Player.begin(DFPlayerSerial);
  MP3Player.volume(15);
}

void loop()
{
  delay(50);                      // esperar 50ms entre pings (29ms como minimo)
  int distance = sonar.ping_cm(); // obtener el valor en cm (0 = fuera de rango)
  Serial.print(distance); Serial.println(F("cm"));

  if (distance < 10) {
    // distance less than 10cm
    Serial.println("Playing track 1 for 5 sec");
    MP3Player.play(1);
    delay(5000);
    MP3Player.pause();
  } else if (distance < 20) {
    // distance between 10cm and 20cm
    ...
  } else if (distance < 50) {
    // distance between 20cm and 50cm
    ...
  } else {
    // distance more than 50cm
    ...
  }
}

typed here, totally untested.

how I could save the tracks on the df player sd card

I change the ... for writing the execution of the tracks

#include <NewPing.h> // https://bitbucket.org/teckel12/arduino-new-ping/downloads/
const byte UltrasonicPin = 5;
const unsigned int MaxDistance = 200;
NewPing sonar(UltrasonicPin, UltrasonicPin, MaxDistance);

#include <SoftwareSerial.h>
#include <DFPlayerMini_Fast.h> // https://github.com/PowerBroker2/DFPlayerMini_Fast
SoftwareSerial DFPlayerSerial(10, 11); // RX, TX
DFPlayerMini_Fast MP3Player;

void setup()
{
  Serial.begin(9600);
  DFPlayerSerial.begin(9600);
  MP3Player.begin(DFPlayerSerial);
  MP3Player.volume(15);
}

void loop()
{
  delay(50);                      // esperar 50ms entre pings (29ms como minimo)
  int distance = sonar.ping_cm(); // obtener el valor en cm (0 = fuera de rango)
  Serial.print(distance); Serial.println(F("cm"));

  if (distance < 10) {
    // distance less than 10cm
    Serial.println("Playing track 1 for 5 sec");
    MP3Player.play(1);
    delay(5000);
    MP3Player.pause();
  } else if (distance < 20) {
    // distance between 10cm and 20cm
    reproduce audio track 1
    
  } else if (distance < 50) {
    // distance between 20cm and 50cm
    
  } else {
    // distance more than 50cm
    
  }
}

pepo177:
how I could save the tracks on the df player sd card

Sorry but this is a forum for people willing to learn and putting efforts into this. if you don't know how to write a file on a SD card, you need to go do another project...

So do your research and also practice with Arduino's tutorial to understand how things work and come back with documented question, showing your work. it's your project, not mine...

not helping too much, anybody else more optimist please? thanks for your time

What's the problem now?
What have you tried in the last couple of hours since you last posted code?

pepo177:
not helping too much, anybody else more optimist please? thanks for your time

I'm an optimist.

here is more information for you for your SD card question.