Hi All!
Apologies in advance for all my errors, I am very new to this, so bear with me please ![]()
I am trying to play a randomly chosen single sound file with the DFPlayer Mini connected to an arduino Nano (Old bootloader) when I push a button. (by single I mean that I have a pool of files on the SD Card (no folders), it chooses one of them, plays it once, then stops playing all together)
I have modified the code I found here and I am not using any library because – weirdly enough – the SD Card does not get recognised when using libraries, bu works fine with Hex commands (is that how it is called?)
For now what happens is that when I upload my code it starts playing all the sound files, one after the other, in order (not random), and as a loop (repeats all), and as soon as I push the button it stops playing, until I press the reset button on the arduino and everything starts again from the beginning... I feel just like in groundhog day :o
Anyone of you seeing where my error(s) are in the code, and maybe how to fix them? I would be so ethernally thankful!!!!
#include "SoftwareSerial.h"
SoftwareSerial mySerial(10, 11);
# define Start_Byte 0x7E
# define Version_Byte 0xFF
# define Command_Length 0x06
# define End_Byte 0xEF
// # define
# define Acknowledge 0x00 //Returns info with command 0x41 [0x01: info, 0x00: no info]
#define ACTIVATED LOW // Button
int button = 4;
boolean isPlaying = false;
void setup () {
 pinMode(button, INPUT);
 digitalWrite(button,HIGH);
 mySerial.begin (9600);
 delay(1000);
 execute_CMD(0x3F,0,0); // send initialization parameters
 delay(500);
 setVolume(30); // sets the volume 0-30
 delay(500);
 execute_CMD(0x08,0,3); // set the playback mode (3=random)
 delay(500);
 execute_CMD(0x11,0,1); // repeat play (1=start repeat 0=stop repeat)
 isPlaying = true;
}
void loop ()
{
 if (digitalRead(button) == ACTIVATED)
  play();
}
void pause()
{
 execute_CMD(0x0E,0,0); // Pause
 delay(500);
}
void play()
{
 execute_CMD(0x0D,0,1); // Playback
 delay(500);
 execute_CMD(0x11,0,0); // Repeat play stop
 delay(500);
Â
}
void setVolume(int volume)
{
 execute_CMD(0x06, 0, volume); // specify volume (0-30)
 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]);
}
}
Thank you!!!