Cant start a function with my DF Player Mini

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 :face_with_diagonal_mouth:

Did you try putting any code inside the braces, like

if (!player.readState()) {
// your code here
}

yeah, that was my first attempt and when the song ended there was nothing from the lights. Honestly, its not great responses from the get-go.

As the code is now, the fade routine is not running, the lights just turn on. Pretty much the only thing that responds as it should is the mp3 that will play with the switch and will reset when I flip it again.

See below:

You seem to have not xet understood how your code works.
And as long as your knowledge about programming stays on the momentary level you will be dependant on the grace of forum users to again and again and again modify the code for you.
You should make a fundamentak decision:
A. stopping tinkering around with almost no knowledge and buy a ready to use product of the shelf

B. Learning to writing code

The if-condition checks for a state-CHANGE from high to low then executes start playing.
In the next iteration of loop there is NO state-change
Which means the code inside the if- ondition is NOT executed. Hence no fading.
Your code must be modified to use a boolean flag-variable that is set to true on the buttonpress and set to false if the mp3 has finished.
Fading is then executed ss long as the boolesn flag is true.
I guess your next question is "how do I code this?"
Answer:" by finally learning to write code.
You will get support on this forum. Make an own attempt to write the code and post your modified code with a specific question

@StefanL38 thanks for your reply. I appreciate you walking me through some fundamentals, however I don't think your comment about me quitting was really all that necessary.
I mean, I get it. New guys ask dumb questions, that's kind of what you sign up for when you poke around on these forums, no?

Regardless, my follow up question was not going to be "can you code this for me?". Again, I appreciate your reply and the time you took to look at my issue.

Ok very good if you want to learn I was astonished that you decided to work on such a project with your actuell knowledgelevel. I'm sure if you take 5 to 10 hours time to work through a basic tutorial this will save you dozens of hours finishing your project faster.

I recommend this tutorial

1 Like

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