This is part of a larger project, which I ended up scaling back to make it a bit more manageable. So I decided to focus on getting my code to do the following:
Button will trigger the Mp3 to play song and turn on lights. However, my issue is that I am not sure where to nest my light routines so they are not activating outside of the triggers.
The code is as follows:
// Tue 8 Aug 2023; fixed mp3 audio pop
#include <DFRobotDFPlayerMini.h>
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"
#define BUTTON_PIN 9 // mine wired to 0V, so goes Low when pressed
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 = 10; // 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
////////////////////////////////////////////////////////////////
void setup()
{
///////////////////////Button Trigger//////////////////////////
pinMode (BUTTON_PIN, INPUT_PULLUP); // Avoids need for resistor
///////////////////////////////////////////////////////////////
///////////////////////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);
}
}
///////////// End Music Functions/////////////////////////////
////////////////////Fairy Lights//////////////////////////////
pinMode(led, OUTPUT); //declare pin 10 to be an output
//////////////////End Fairy Light/////////////////////////////
}
void loop()
{
buttonState = digitalRead(BUTTON_PIN);
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
////////////////////////////////////////////////////
/////////////////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);
/////////////////////////////////////////////////////////////////
if (!player.readState()) {} //only resets the song when button is flipped
}
previousButtonState = buttonState;
}
I was able to find this code:
if (!player.readState()) {}
Which seems to be a step in the right direction, as it supposedly reads the current state of the MP3 player. Ideally I would just set the lights to operate only as the song plays and all of the routines to shutdown or turn on based on the state of the button. Does anyone have any experience with boolean flags with button states? Someone on another thread suggested I should try this, but unfortunately I'm still floundering ![]()