DFMiniMP3.h Random track?

I have a sketch that uses inputs from 2 Arduino nanos’ and some switches to Play specific tracks on a DFplayer mini. these act as horns on game table.

After completing the project, I was thinking it might be nice to add some ambient crowd noises/chants while play is underway

However, I don’t want to just put in a “random track” as I don’t want it to include the horns into to the play rotation.

AND I would like it to start playing random tracks at the start of the game, and at the end of the game stop. I would like random from a large collection so it does not get to repetitive and annoying.

Right now tracks 0001,0002,0003,0004 are in a folder named MP3 as needed and are tracks for 2 horns, a start-up noise and a end game horn.

I have one input (ledPin3 = 5;) that Indicates by going HIGH, the start and end of each Game By playing track 4 and lighting 2 LEDs.

QUESTION IS:
Is it possible maybe using something like “void playFolderTrack16(uint8_t folder, uint16_t track)” to have the Dfplayer mini play random tracks from a SEPARATE FOLDER at random Starting and stoping random play every time pin 5 goes High, WITHOUT disrupting any of the other functions/horns?
(Or even from the same MP3 folder but excluding tracks 1,2,3,and 4?)

I understand the the Random play would be interrupted whenever the other goal horns sound, and thats fine with me and the horns are more important.

// sd:/mp3/0001.mp3
// sd:/mp3/0002.mp3
// sd:/mp3/0003.mp3

const int buttonPin2 = 4; // Home IR
const int buttonPin3 = 2; // Away IR
const int buttonPin4 = 3; // End Game Horn from Scoreboard
const int ledPin1 = 8; // Home Led
const int ledPin2 = 12; // Away Led
const int ledPin3 = 5; // Horn status “End Game LED 1”
const int ledPin4 = 6; // Horn status “End Game LED 2”
int buttonState2 = 0; // variable for reading the pushbutton status
int buttonState3 = 0; // variable for reading the pushbutton status
int buttonState4 = 0; // variable for reading the pushbutton status

#include <SoftwareSerial.h>
#include <DFMiniMp3.h>

// implement a notification class,
// its member methods will get called
//
class Mp3Notify
{
public:
static void PrintlnSourceAction(DfMp3_PlaySources source, const char* action)
{
if (source & DfMp3_PlaySources_Sd)
{
Serial.print("SD Card, ");
}
if (source & DfMp3_PlaySources_Usb)
{
Serial.print("USB Disk, ");
}
if (source & DfMp3_PlaySources_Flash)
{
Serial.print("Flash, ");
}
Serial.println(action);
}
static void OnError(uint16_t errorCode)
{
// see DfMp3_Error for code meaning
Serial.println();
Serial.print("Com Error ");
Serial.println(errorCode);
}
static void OnPlayFinished(DfMp3_PlaySources source, uint16_t track)
{
Serial.print(“Play finished for #”);
Serial.println(track);
}
static void OnPlaySourceOnline(DfMp3_PlaySources source)
{
PrintlnSourceAction(source, “online”);
}
static void OnPlaySourceInserted(DfMp3_PlaySources source)
{
PrintlnSourceAction(source, “inserted”);
}
static void OnPlaySourceRemoved(DfMp3_PlaySources source)
{
PrintlnSourceAction(source, “removed”);
}
};

// instance a DFMiniMp3 object,
// defined with the above notification class and the hardware serial class
//
//DFMiniMp3<HardwareSerial, Mp3Notify> mp3(Serial1);

// Some arduino boards only have one hardware serial port, so a software serial port is needed instead.
// comment out the above definition and uncomment these lines
SoftwareSerial secondarySerial(10, 11); // RX, TX
DFMiniMp3<SoftwareSerial, Mp3Notify> mp3(secondarySerial);

void setup()
{

// initialize the LED pin as an output:
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin2, INPUT_PULLUP);
pinMode(buttonPin3, INPUT_PULLUP);
pinMode(buttonPin4, INPUT);

Serial.begin(9600);

Serial.println(“initializing…”);

mp3.begin();

//Set Speaker Volume start 30
uint16_t volume = mp3.getVolume();
Serial.print("volume ");
Serial.println(volume);
mp3.setVolume(30);

uint16_t count = mp3.getTotalTrackCount(DfMp3_PlaySource_Sd);
Serial.print("files ");
Serial.println(count);

Serial.println(“starting…”);

//play startup track
Serial.println(“track 3”);
mp3.playMp3FolderTrack(3); // sd:/mp3/0003.mp3
delay(5000);

}

void waitMilliseconds(uint16_t msWait)
{
uint32_t start = millis();

while ((millis() - start) < msWait)
{
// calling mp3.loop() periodically allows for notifications
// to be handled without interrupts
mp3.loop();
delay(1);

}
}

void loop()
{

//read the pushbutton value into a variable
int sensorVal = digitalRead(2);
//print out the value of the pushbutton
Serial.println(sensorVal);

// read the state of the pushbutton value:
buttonState2 = digitalRead(buttonPin2);
buttonState3 = digitalRead(buttonPin3);
buttonState4 = digitalRead(buttonPin4);

// HOME check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState2 == LOW) {
//turn LED on:
digitalWrite(ledPin1, HIGH);
// play track 1:
Serial.println(“track 1”);
mp3.playMp3FolderTrack(1); // sd:/mp3/0001.mp3
delay(9000);
} else {
// turn LED off:
digitalWrite(ledPin1, LOW);
}

// AWAY check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState3 == LOW) {
// turn LED on:
digitalWrite(ledPin2, HIGH);
// play track 2:
Serial.println(“track 2”);
mp3.playMp3FolderTrack(2); // sd:/mp3/0002.mp3
delay(9000);
} else {
// turn LED off:
digitalWrite(ledPin2, LOW);
}

// End Game horn via. Scoreboard clock.
if (buttonState4 == HIGH) {
// turn LED on:
digitalWrite(ledPin3, HIGH);
digitalWrite(ledPin4, HIGH);
// play track 2:
Serial.println(“track 4”);
mp3.playMp3FolderTrack(4); // sd:/mp3/0004.mp3
delay(9000);
} else {
// turn LED off:
digitalWrite(ledPin3, LOW);
digitalWrite(ledPin4, LOW);
}
}

You program is already retrieving the total number of tracks you have on the player. If you know that tracks 1-4 are "reserved", just use the random() function to give you a number between 5 and that count to pick the track.

int randomTrack = random(5, count+1);  // need +1 to make it inclusive
 mp3.playMp3FolderTrack(randomTrack;

blh64:
You program is already retrieving the total number of tracks you have on the player. If you know that tracks 1-4 are "reserved", just use the random() function to give you a number between 5 and that count to pick the track.

int randomTrack = random(5, count+1);  // need +1 to make it inclusive

mp3.playMp3FolderTrack(randomTrack;

I think I know what your saying, makes since.
but as far as the number count. say I have 99 tracks.

would it be as you had written? or:

int randomTrack = random(5, 99+1); // need +1 to make it inclusive
mp3.playMp3FolderTrack(randomTrack;

and would I need a ")" after track?

Sorry just trying to get a firm grasp on the code.

yes, the function needs a closing ')'.

Your code already figures out how many tracks are on the SD card, why hardcode a number?

blh64:
yes, the function needs a closing ')'.

Your code already figures out how many tracks are on the SD card, why hardcode a number?

Ok I see! I'll give it a go.

Thank you!