Hello. I'm working on a project that consists on:
A PIR sensor will activate DFplayer mini playback.
An LED should stay ON during playback, and off when no sound is being played.
The problem i'm facing is that the LED will stay ON for a second, playback continues but with the LED off.
I'm using DFPlayerMini_Fast.h library, and myDFPlayer.isPlaying() to detect playback, since I have been told, reading the busy pin status from the DFPlayer, says only if the sd card is being in use.
#include "SoftwareSerial.h"
#include "DFPlayerMini_Fast.h"
SoftwareSerial mySoftwareSerial(10, 11); // RX, TX
DFPlayerMini_Fast myDFPlayer;
void printDetail(uint8_t type, int value);
#define SENSORPIN 9
#define PAUSETIME 500
int randomInt;
#define LED 8 // led a pin 8
void setup() {
mySoftwareSerial.begin(9600);
Serial.begin(115200);
pinMode(SENSORPIN, INPUT);
Serial.println();
Serial.println(F("Initializing DFPlayer..."));
//Use softwareSerial to communicate with MP3
if (!myDFPlayer.begin(mySoftwareSerial)) {
Serial.println(F("Unable to begin:"));
Serial.println(F("1.Please recheck the connection!"));
Serial.println(F("2.Please insert the SD card!"));
while(true);
}
Serial.println(F("DFPlayer Mini online."));
//Set volume value (From 0 to 30)
myDFPlayer.volume(25);
}
void sensorActivated() {
// DFPlayerMini_Fast.h has a built-in function, "player.isPlaying()", that returns a boolean based on if the module is playing a song currently or not.
int pirSensor = digitalRead(SENSORPIN);
// if(pirSensor == HIGH && myDFPlayer.isPlaying()!=1)
if(pirSensor == HIGH && myDFPlayer.isPlaying()==0)
{
Serial.println("DFplayer status: ");
Serial.println(myDFPlayer.isPlaying());
Serial.println("Sensor Activated");
Serial.println("DFPlayer Working...");
randomInt = random(3, 8);
Serial.println(randomInt);
myDFPlayer.play(randomInt);
digitalWrite(LED, HIGH); // enciende led
}
if(myDFPlayer.isPlaying()==1)
{
digitalWrite(LED, LOW); // apaga led
Serial.println("Led OFF, player status: ");
//Looks like player status 1 means playback is finished.
Serial.println(myDFPlayer.isPlaying());
}
return;
}
void loop() {
sensorActivated();
delay(PAUSETIME);
}
This is my serial monitor output.
DFPlayer Working...
Random track being played:
5
Led OFF, player status:
1
Led OFF, player status:
1
Led OFF, player status:
1
DFplayer status:
0
Sensor Activated
DFPlayer Working...
Random track being played:
7
Led OFF, player status:
1
Led OFF, player status:
1
Led OFF, player status:
1