I have a project that is using the Busy pin on my DF Player Mini to start a light sequence based on a whether the mp3 is playing. At first the approach seemed pretty straightforward, but after implementing the wiring and DigitalRead and my IF statements, I havent seen any responses.
Full Code:
// Tue 16 Aug 2023; Added Busy pin input for mp3 player
#include <DFRobotDFPlayerMini.h>
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"
#define BUTTON_PIN 4
#define BUSY_PIN 8
bool buttonState = 1; // Normally high, goes low when pressed
bool previousButtonState = 1; // Because we will test when CHANGED
///////////////Create the Player object/////////////////////////////
DFRobotDFPlayerMini player;
static const uint8_t PIN_MP3_TX = 2; // UNO Tx connects module's RX
static const uint8_t PIN_MP3_RX = 3; // UNO Rx connects module's TX
SoftwareSerial softwareSerial(PIN_MP3_RX, PIN_MP3_TX);
//////////////////////////////////////////////////////////////////
/////////////////Create Fairy Light Variables////////////////////
int led = 11; // the PWM pin the LED is attached to
int brightness = 3; // how bright the LED is
int fadeAmount = 2; // how many points to fade the LED by
////////////////////////////////////////////////////////////////
int busyState = 0;
int prevBusyState = 0;
void setup()
{
///////////////////////Button Trigger//////////////////////////
pinMode (BUTTON_PIN, INPUT_PULLUP); // Avoids need for resistor
///////////////////////////////////////////////////////////////
pinMode(BUSY_PIN, INPUT_PULLUP); //check to see if song is still playing
///////////////////////Music Functions/////////////////////////
// Init USB serial port for debugging
Serial.begin(115200);
Serial.println();
// Init serial port for DFPlayer Mini
softwareSerial.begin(9600);
delay(1000);
// Start communication with DFPlayer Mini
if (!player.begin(softwareSerial, true, false)) { //Use softwareSerial to communicate with mp3.
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) {
delay(0);
}
}
busyState = digitalRead(BUSY_PIN);
prevBusyState = busyState;
///////////// End Music Functions/////////////////////////////
////////////////////Fairy Lights//////////////////////////////
pinMode(led, OUTPUT); //declare pin 11 to be an output
//////////////////End Fairy Light/////////////////////////////
}
void loop()
{
buttonState = digitalRead(BUTTON_PIN);
busyState = digitalRead(BUSY_PIN);
if (busyState != prevBusyState) {
if (busyState == LOW) {
Serial.print ("song is playing. lights are go");
/////////////////Fairy Light Fading Routine////////////////////////
// set the brightness of pin 10:
analogWrite(led, brightness);
// change the brightness for next time through the loop:
brightness = brightness + fadeAmount;
// reverse the direction of the fading at the ends of the fade:
if (brightness <= 1 || brightness >= 240) {
fadeAmount = -fadeAmount;
}
// wait for 30 milliseconds to see the dimming effect
delay(45);
/////////////////////////////////////////////////////////////////
}
prevBusyState = busyState;
}
if (buttonState == LOW && previousButtonState == HIGH)
{
/////////////////////Play Song///////////////////////
// The following NOT inside the previous if()
// Set volume to maximum (0 to 30).
player.volume(23);
// Play the first MP3 file on the SD card
player.play(1); // i.e. usually file 0001.mp3
////////////////////////////////////////////////////
//if (!player.readState()) {} //only resets the song when button is flipped
}
previousButtonState = buttonState;
}
I created my busy state variables
int busyState = 0;
int prevBusyState = 0;
Getting Data from Board
pinMode(BUSY_PIN, INPUT_PULLUP); //check to see if song is still playing
I Initialized my Busy States in my Setup
busyState = digitalRead(BUSY_PIN);
prevBusyState = busyState;
And in my loop, when a song is playing, the Busy state should trigger this function via the following code:
buttonState = digitalRead(BUTTON_PIN);
busyState = digitalRead(BUSY_PIN);
if (busyState != prevBusyState) {
if (busyState == LOW) {
Serial.print ("song is playing. lights are go");
/////////////////Fairy Light Fading Routine////////////////////////
// set the brightness of pin 10:
analogWrite(led, brightness);
// change the brightness for next time through the loop:
brightness = brightness + fadeAmount;
// reverse the direction of the fading at the ends of the fade:
if (brightness <= 1 || brightness >= 240) {
fadeAmount = -fadeAmount;
}
// wait for 30 milliseconds to see the dimming effect
delay(45);
/////////////////////////////////////////////////////////////////
}
prevBusyState = busyState;
}
According to the DF Player Mini docs, the Busy pin reads as "LOW" when a song is playing. So thats why I have it set as "==LOW".
if (busyState == LOW) {
Serial.print ("song is playing. lights are go");
Right now, the only thing I'm getting out of my code is an mp3 file playing whenever I hit my button and resets whenever I hit the button. No light sequence. So we're 50% of the way there. Not a total failure!