ultra sonic hc-sr04 sensor to trigger dfplayer - keeps looping

Hello Forum,

I'm trying to get an ultra sonic sensor to trigger 3 dfplayers. The problem is that once the ultrasonic sensor has been triggered the dfplayers starts to play in a loop.

I just need the track to be played once until the sensor is being triggered again.

Could someone perhaps see if I need to change something in the code?

Your help would be much appreciated.
Thank you.

#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"


// defines pins numbers
const int trigPin = 9;
const int echoPin = 13;

// defines variables
long duration;
int distance;

// Use pins 11 and 10 to communicate with DFPlayer Mini
static const uint8_t PIN_MP3_TX = 11; // Connects to module's RX 
static const uint8_t PIN_MP3_RX = 10; // Connects to module's TX 
SoftwareSerial softwareSerial(PIN_MP3_RX, PIN_MP3_TX);

// Create the Player object
DFRobotDFPlayerMini player;

void setup() {

pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input

  // Init USB serial port for debugging
  Serial.begin(9600);
  // Init serial port for DFPlayer Mini
  softwareSerial.begin(9600);

  // Start communication with DFPlayer Mini
  if (player.begin(softwareSerial)) {
   Serial.println("OK");

    // Set volume to maximum (0 to 30).
    player.volume(30);
    // Play the first MP3 file on the SD card
    //player.play(1);
  } else {
    Serial.println("Connecting to DFPlayer Mini failed!");
  }
   // player.volume(30); //(30 max, 15 min)
    // Play the first MP3 file on the SD card
    //player.play(1);
   // player.loop(1);
}

void loop() {

// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.034/2;

// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);

    //change distances here
    if (distance > 1 && distance <= 8) { //distance in centimeter, fx between 1 - 8 cm
      for(int i = 0; i < 50; i++){
      player.play(1);
      delay(10000);
      player.stop();
      }
      Serial.println("track 1 playing");
      //distance = get_ultrasonic_distance();
      delay(5000); //delay in milli seconds - controls how long the relay is on for
    }

    else {
      //player.play(1);
      Serial.println("off");
      //distance = get_ultrasonic_distance();
       player.stop();
    }
}


long  get_ultrasonic_distance()
{
    long distance_ = 0;
    distance_ = duration*0.034/2;
    if ((distance_ != 0))
    {
    return distance_;
    }
    else return 0;
}

Start by using a seperate power supply for the speakers, the Arduino a Power Supply it is NOT. You might also post a schematic, not this frizzy thing. Learning how to draw one and read it will be a big help to you in the future. You should also do a tutorial on Ohm's Law, and you will understand why you get no volume.

I just need the track to be played once until the sensor is being triggered again.

Please explain more about what you want to happen. What do you mean by "triggered"? This type of sensor is triggered by the Arduino, not by an object in front of the sensor. When triggered, the sensor measures the distance to the object and gives a pulse back to the Arduino. The length of the pulse tells the Arduino the distance between the object and the sensor. So if there is a stationary object in front of the sensor, then every time the arduino triggers the sensor, it will get the same reading, and your code will continue to play the sound.

Perhaps what you want is that if an object comes within a certain distance of the sensor, the sound is played once only, even if the object remains within that distance? Only if the object moves outside that certain distance, and then it or another object comes within the distance will the sound be played again?

If I'm right, then what you need to do is use a new global variable to record the previous distance fron the sensor. Then you can write an if statement that checks if the new distance just measured is below the threshold and the previous distance was above it, then play the sound. After that has been done, your code can update the variable with the newly measured distance.

What the . . .?

fishmarcel:
. . .The problem is that once the ultrasonic sensor has been triggered the dfplayers starts to play in a loop.

I just need the track to be played once until the sensor is being triggered again. . . .

. . .

for(int i = 0; i < 50; i++){
       player.play(1);
       delay(10000);
       player.stop() ;
      }
. . .

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