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.
- Sometimes it starts playing tracks for longer than the 3 seconds. This seems weird since it should stop after 3 seconds.
- 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?
- 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);
}
}
}