DF Player, Need pause button and volume knob

Hello All!

I am currently working on a project of a music box. I have a DF Player that I am using attached to a speaker. I want pins 2,3, and 4 to be a previous track, pause, and next track. I'm also trying to have a volume knob by using a potentiometer by using pin A0. I'm having an issue where when I implement the code to control the volume, the buttons don't work anymore. Any help? Thank you!

#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"
SoftwareSerial mySerial(10, 11);
DFRobotDFPlayerMini myMP3;
# define Start_Byte 0x7E
# define Version_Byte 0xFF
# define Command_Length 0x06
# define End_Byte 0xEF
# define Acknowledge 0x00 //Returns info with command 0x41 [0x01: info, 0x00: no info]

# define ACTIVATED LOW

int buttonNext = 2;
int buttonPause = 3;
int buttonPrevious = 4;
boolean isPlaying = false;

const byte potPin = A0;

byte volumeLevel = 0;

void setup () {

pinMode(buttonPause, INPUT);
digitalWrite(buttonPause,HIGH);
pinMode(buttonNext, INPUT);
digitalWrite(buttonNext,HIGH);
pinMode(buttonPrevious, INPUT);
digitalWrite(buttonPrevious,HIGH);

Serial.begin(9600);
mySerial.begin (9600);
myMP3.begin(mySerial);
myMP3.loop(1);
delay(1000);
playFirst();
isPlaying = true;


}



void loop () { 

 if (digitalRead(buttonPause) == ACTIVATED)
  {
    if(isPlaying)
    {
      pause();
      isPlaying = false;
    }else
    {
      isPlaying = true;
      play();
    }
  }


 if (digitalRead(buttonNext) == ACTIVATED)
  {
    if(isPlaying)
    {
      playNext();
    }
  }

   if (digitalRead(buttonPrevious) == ACTIVATED)
  {
    if(isPlaying)
    {
      playPrevious();
    }
  }
  {
  volumeLevel = map(analogRead(potPin), 0, 1023, 0, 30);   //scale the pot value and volume level
  myMP3.volume(volumeLevel);
  Serial.println(volumeLevel);
  }
}

void playFirst()
{
  execute_CMD(0x3F, 0, 0);
  delay(500);
  setVolume(25);
  delay(500);
  execute_CMD(0x11,0,1); 
  delay(500);
}

void pause()
{
  execute_CMD(0x0E,0,0);
  delay(500);
}

void play()
{
  execute_CMD(0x0D,0,1); 
  delay(500);
}

void playNext()
{
  execute_CMD(0x01,0,1);
  delay(500);
}

void playPrevious()
{
  execute_CMD(0x02,0,1);
  delay(500);
}

void setVolume(int volume)
{
  execute_CMD(0x06, 0, volume); // Set the volume (0x00~0x30)
  delay(2000);
}

void execute_CMD(byte CMD, byte Par1, byte Par2)
// Excecute the command and parameters
{
// Calculate the checksum (2 bytes)
word checksum = -(Version_Byte + Command_Length + CMD + Acknowledge + Par1 + Par2);
// Build the command line
byte Command_line[10] = { Start_Byte, Version_Byte, Command_Length, CMD, Acknowledge,
Par1, Par2, highByte(checksum), lowByte(checksum), End_Byte};
//Send the command line to the module
for (byte k=0; k<10; k++)
{
mySerial.write( Command_line[k]);
}
}

Please show us your schematic.

And tell us what Arduino board?

Does at least the track specified in setup() play correctly?

It’s an interesting alternative to use a pot for volume control. But was that your sole reason, rather than the familiar two buttons?

void setVolume(int volume)
{
  execute_CMD(0x06, 0, volume); // Set the volume (0x00~0x30)
  delay(2000);
}

delay(2000) will certainly bung things up for a bit, responsivity-wise, yes?

Thanks for the reply!

Using an Arduino Uno

The image below shows how the DF Player and Speaker are wired. I also have pins 2, 3 & 4 wire to the red side of a button while also having the buttons black side go to GND.

Lastly I have a potentiometer wire so that VCC is going to 5V, GND to GND, and the middle pin going to A0.

The choice to do this is more so just the components that I have on hand. I'm trying to have a knob for volume control and buttons for other features

Compared your wiring with my diagram below:


and looks Ok.

But, although you haven't answered my question ("Does at least the track specified in setup() play correctly?"), I assume it does. So I'll take a closer look

And we really need to see a real full schematic, buttons included!

Here's the wiring for the buttons and potentiometer
And apologies, the track specified in setup is played correctly

OK, I have that on a breadboard and it's correctly handling play, pause, next and previous. Didn't have 10k preset at hand but a hasty test with A0 grounded gave interesting fading vol over a minute or so. Will pursue after lunch.

EDIT: Seems OK with the three connections I made to A0: 5V, Gnd, 3.3V, in lieu of geting a pot from my shed. Puzzled why I got that fading effect earlier.

Are you still getting the behaviour you described?

Also, can you describe exactly what you want your sketch to do, in setup() and in loop().