Play MP3 audio file while taking measurement

Hi All,

i'm trying to implement a DFMini MP3 play with an ultrasonic sensor.
For my purspose, i need to play MP3 file while keep taking measurement, and sto to play if the distance is major of a certain value. With this code, i take a measure every 10 seconds, then i play 10 seconds of the MP3 file, else i'll pause the mp3.
instead of pin 0-1 i used 10-11. I tried to use a while (distance < x) loop, but it didn't work.
IT's possible to take a measure every 0.5 second and keep play the audio file UNTIL the distance is < 30 cm? I need to reproduce the audio continuously and simultaneously take measure.
Thanks.

931fa2c2d49f140862acf175e3e58f73

#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"
int trig = 2;
int echo = 3;


SoftwareSerial mySoftwareSerial(10,11);
DFRobotDFPlayerMini myDFPlayer;

void setup () {

Serial.begin (9600);

pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
Serial.println("Setting up software serial");
mySoftwareSerial.begin(9600);

Serial.println("check dfplayer");
if (!myDFPlayer.begin(mySoftwareSerial)) { //Use softwareSerial to communicate with mp3.
Serial.println(F("DFPlayer error."));
//while (true);
}
Serial.println("Setting up software serial —- done");

//—-Mp3 play—-
Serial.println("Setup mp3 player settings");
myDFPlayer.volume(40);
myDFPlayer.setTimeOut(200);
}
 
 
//
void loop () {  
  int duration , distance;
  digitalWrite(trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig, LOW);
  duration = pulseIn(echo, HIGH);
  distance = (duration / 2) / 21.9;
  Serial.print(distance);
  Serial.print("cm");
  delay(10000); //calcolo della distanza ogni tot secondi
  
  if (distance < 30)
  {      
  myDFPlayer.play(0001);
   delay(500);
  }
  else
  {
myDFPlayer.pause();
Serial.println("pausa");
delay(500);
}
}
 

Watch out. The pulseIn() function returns an 'unsigned long' value, not 'int'.

What does the sketch do wrong if you remove your delay(10000); ?

i'll try to remove the delay!
Even if the pulseIn() function returns an unsigned long, seems that the code is working and the distance is correct, but i'll try to change it.
My problem is thats seems impossible to reproduce the audio and take the measure continuously and simultaneously , because the audio is reproduced only in the time between two measurement..

I think that is because you keep telling it to "play(0001)" again. You should tell it to play when the range goes from >= 30 to < 30. There is no need to tell it to play the track when it is already playing.

Something like this should start the playing when it sees an echo off something less than 30cm away and stop when there is no echo within 30cm.

#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"
int trig = 2;
int echo = 3;


SoftwareSerial mySoftwareSerial(10, 11);
DFRobotDFPlayerMini myDFPlayer;

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

  pinMode(trig, OUTPUT);
  pinMode(echo, INPUT);
  
  Serial.println("Setting up software serial");
  mySoftwareSerial.begin(9600);

  Serial.println("check dfplayer");
  if (!myDFPlayer.begin(mySoftwareSerial))   //Use softwareSerial to communicate with mp3.
  {
    Serial.println(F("DFPlayer error."));
    while (true);
  }
  Serial.println("Setting up software serial —- done");

  //—-Mp3 play—-
  Serial.println("Setup mp3 player settings");
  myDFPlayer.volume(40);
  myDFPlayer.setTimeOut(200);
}


//
void loop ()
{
  // If you re-trigger the sonar too soon you 
  // might receive echoes from the previous trigger
  delay(30);

  // Send a sonar ping
  digitalWrite(trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig, LOW);

  // Measure the echo pulse which represents the
  // echo time.  0 = no echo
  unsigned long  duration = pulseIn(echo, HIGH, 30000UL);
  int distance = (duration / 2) / 21.9;
  Serial.print(distance);
  Serial.println("cm");

  // Is there something in range?
  bool isInRange = distance != 0 && distance < 30;
  
  static bool wasInRange = false;  // 'static' sticks around, like a global

  if (isInRange != wasInRange)
  {
    // The state has changed
    wasInRange = isInRange;

    if (isInRange)
    {
      // Just entered range so start playing
      myDFPlayer.play(0001); // WARNING: 0001 == 1 but 0010 == 8 because the leadin 0 means OCTAL.
    }
    else
    {
      // Just left range
      myDFPlayer.pause();
      Serial.println("pause");
    }
  }
}

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.