Help with improving project - DFPlayer + PIR Sensor

Hi!

I am working on a pretty simple project where a PIR Sensor detects movement and a DFPlayer plays random tracks from an SD-card. My children built a robot-head and I want to surprise them with it talking to them when they walk past it.

I got the code working and it plays random tracks for a couple of seconds and then waits for new movement, and then plays a new random track.

I have a couple of issues I find hard to resolve and would like for some help with.

  1. Sometimes it starts playing tracks for longer than the 3 seconds. This seems weird since it should stop after 3 seconds.
  2. Since it plays random tracks, sometimes it chooses to play the same track a couple of times in a row, which is a bit annoying. Would there be any way to remedy that?
  3. Lastly. Sometimes it doesn't play any track at all. I've seen that the Mac can create hidden files that the DFPlayer interprets as mp3-files and tries to play them, but I have deleted those from the sd-card. Could there be any other reason it doesn't play any track?

I really appreciate all the help I can get.

Here's the code:

#if !defined(UBRR1H)
#include <DFPlayerMini_Fast.h>
#include "SoftwareSerial.h"
SoftwareSerial mySerial(10, 11); //RX, TX
#endif

DFPlayerMini_Fast myMP3;

//Time for sensor calibration
int calibrationTime = 30;       
 
//low impulse time from sensor 
long unsigned int lowIn;        
 
//time until motion has ended 
long unsigned int pause = 1000; 
 
boolean lockLow = true;
boolean takeLowTime; 
 
int pirPin = 12;    //PIR Sensor 
int ledPin = 13;    //Led 
 
 
/////////////////////////////
//SETUP
void setup(){
  Serial.begin(9600);
  pinMode(pirPin, INPUT);
  pinMode(ledPin, OUTPUT);
  digitalWrite(pirPin, LOW);

  #if !defined(UBRR1H)
  mySerial.begin(9600);
  myMP3.begin(mySerial);
#else
  Serial.begin(9600);
  myMP3.begin(Serial1);
#endif

 Serial.println("Volume set");
  myMP3.volume(30);
  delay(20);
 
  //Calibrating the sensor set to 30seconds 
  Serial.print("calibrating sensor ");
    for(int i = 0; i < calibrationTime; i++){
      Serial.print(".");
      delay(1000);
      }
    Serial.println("calibrated");
    Serial.println("SENSOR ACTIVE");
    delay(50);
  }
 
////////////////////////////
//LOOP
void loop(){
 int randomInt = random(1,9);
 
     if(digitalRead(pirPin) == HIGH){
       digitalWrite(ledPin, HIGH);   //the led lights up when the sensor is activated 
       if(lockLow){ 

       
         //wait until sensor is LOW before continue 
         lockLow = false;           
         Serial.println("---");
         Serial.print("motion detected at ");
         Serial.print(millis()/1000);
         Serial.println(" sec");
         myMP3.play(randomInt);
         delay(3000);
         }        
         takeLowTime = true;
       }
 
     if(digitalRead(pirPin) == LOW){      
       digitalWrite(ledPin, LOW);  //led shows the PIR state 
 
       if(takeLowTime){
        lowIn = millis();          //saving time from PIR activation until end 
        takeLowTime = false;       //and that it starts from LOW 
        }
       //if the sensor stays LOW for more than the pause
       //we assume that no more motion is going to happen
       if(!lockLow && millis() - lowIn > pause){ 
           //makes sure this block of code is only executed again after
           //a new motion sequence has been detected
           lockLow = true;                       
           Serial.print("motion ended at ");      //output
           Serial.print((millis() - pause)/1000);
           Serial.println(" sec");
           myMP3.sleep();
           delay(50);
           }
       }
  }
  1. Since it plays random tracks, sometimes it chooses to play the same track a couple of times in a row, which is a bit annoying. Would there be any way to remedy that?

Save the track number played. When you need the next one use a while loop to get a random number until it does not match the previous one

How many recorded tracks do you have ?

Good idea! thanks!

I have about 10 tracks I guess - haven't decided yet though

I have about 10 tracks I guess

How many tracks have you got at the moment bearing in mind

 int randomInt = random(1,9);

Also

         myMP3.play(randomInt);

Is the file played indexed from 0 or 1 because the random() call above will only return numbers between 1 and 8

The files are indexed from 0001.mp3 to 0009.mp3

I didn't know that. But there wouldn't be any other way of randomizing them would there?

theswedishmaker:
The files are indexed from 0001.mp3 to 0009.mp3

I didn't know that. But there wouldn't be any other way of randomizing them would there?

So what file does it play if you use say

myMP3.play(1);

I assume that it is 0001.mp3 and that the library is happy to translate a 1 into 0001. Is that right ?

I am not suggesting randomising them another way, but you could put the filenames in an array and use the random number to select one of them. That way the filenames could be anything (within reason) and would not have to be numbered

If I use:

myMP3.play(1);

It will play 0001.mp3

That's the way the DFPlayer library says to name them, but perhaps they can be named anything, I haven't tried.

An array might work, but saving the track played might be a good option as well. I'll have to try that to see if it can be done.

If the player and you are happy with the numeric filenames then the array does not have much to offer. Saving the number of the file to avoid repetition does though

As a matter of interest, can you play a file with an explicit name, say happy.mp3 ?

I´m not sure, but I sure will check it as soon as I get back home. :slight_smile:

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