DFPlayer Mini

I have a DFPlayer mini and the goal is to have one button that plays audio clips, one clip per press of the button. However, when I press the button it just loops through the entire audio library on the sd card. I am not very good with code, and don't know where I'm going wrong. Any help would be appreciated.

Any code would be appreciated

mdg60:
I am not very good with code, and don't know where I'm going wrong. Any help would be appreciated.

you are not very good at reading the forum rules and recommendations either :grin: :cold_sweat:

Sorry, forgot to include the code. I think this is the relevant part:

void loop () {

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

Here is the entire code, borrowed from a project. I had originally wanted to include other buttons, but for simplicity only want the one play button which will cycle through the tracks in order.

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

void setup () {

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

mySerial.begin (9600);
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();
}
}
}

void playFirst()
{
execute_CMD(0x3F, 0, 0);
delay(500);
setVolume(20);
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 correct your post above and add code tags around your code:
[code]`` [color=blue]// your code is here[/color] ``[/code].

It should look like this:// your code is here
(Also press ctrl-T (PC) or cmd-T (Mac) in the IDE before copying to indent your code properly)

———
You should not check just the state of the button to decide to move to the next song - your loop spins thousands time a second so when you detect a press you can switch but then don’t do anything until the button has been released

Just great
I just crafted a brilliant post / hit preview / and it all disappeared.
forget it.
TR

wingman2:
Just great
I just crafted a brilliant post / hit preview / and it all disappeared.
forget it.
TR

Did you look in your drafts ?

Trying again. I'll be brief.

Hey mdg60,
In your void setup section try commenting out these 2 lines. (see code section below)
Because, lower down. the playFirst thing tells it to play repeatedly. It's a 1 and 0 thing.
Yeah, I'm not very good at serial communication either.
: - )
Have fun,
TR

// playFirst();[color=#222222][/color]
// isPlaying = true;

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