How do I make this sketch play random files?

Hello folks, I'm kinda new to arduino and couldn't get to understand this sketch, how could I set it to play a random mp3 file when reset is pressed?

#include <SPI.h>
#include <Adafruit_VS1053.h>
#include <SD.h>

// define the pins used
#define CLK 13 // SPI Clock, shared with SD card
#define MISO 12 // Input data, from VS1053/SD card
#define MOSI 11 // Output data, to VS1053/SD card
// Connect CLK, MISO and MOSI to hardware SPI pins.
// See SPI - Arduino Reference "Connections"

// These can be any pins:
#define RESET 8 // VS1053 reset pin (output)
#define CS 9 // VS1053 chip select pin (output)
#define DCS 6 // VS1053 Data/command select pin (output)
#define CARDCS 10 // Card chip select pin arduino
#define DREQ 7 // VS1053 Data request, ideally an Interrupt pin

Adafruit_VS1053_FilePlayer musicPlayer = Adafruit_VS1053_FilePlayer(RESET, CS, DCS, DREQ, CARDCS);

void setup() {
Serial.begin(9600);
Serial.println("Adafruit VS1053 Simple Test");

musicPlayer.begin(); // initialise the music player
SD.begin(CARDCS); // initialise the SD card

musicPlayer.dumpRegs();

// Set volume for left, right channels. lower numbers == louder volume!
musicPlayer.setVolume(00,00);

// musicPlayer.useInterrupt(VS1053_FILEPLAYER_TIMER0_ INT); // timer int
musicPlayer.useInterrupt(VS1053_FILEPLAYER_PIN_INT ); // DREQ int

Serial.println("Calling playFullFile");
// Play one file, don't return until complete
musicPlayer.playFullFile("s1.mp3");
//musicPlayer.playFullFile("s2.mp3");

Serial.println("setup done");
// Play another file in the background, REQUIRES interrupts!
// musicPlayer.startPlayingFile("s1.mp3");
}

void loop() {
// File is playing in the background
if (! musicPlayer.playingMusic)
Serial.println("Done playing music");

delay(1000);
}

Thank you all!

The code below will place a random number from 0 to 99 into the mp3 name. Note: it will always be two digits, so rename your mp3 files to have two digits representing the number.

/* These are global, place it at header of file*/
#define MIN_NUMBER 0
#define MAX_NUMBER 99 //Can't be higher than 99
char myFile[8] = "s00.mp3";

/* INSIDE setup() */
randomSeed(analogRead(A0)); //Needed to get random numbers. Needs to be an Analog In Pin.

/* INSIDE the function that plays the file */
myFile[1] = random(MIN_NUMBER/10,MAX_NUMBER/10) + '0'; // Convert a SINGLE random number into ASCII char and stores it at second position of myFile
myFile[2] = random(MIN_NUMBER%10,MAX_NUMBER%10) + '0'; // Convert a SINGLE random number into ASCII char and stores it at third position of myFile
musicPlayer.playFullFile(myFile);

Will try to figure were to put those codes tomorrow, thanks

giova014:
The code below will place a random number from 0 to 99 into the mp3 name. Note: it will always be two digits, so rename your mp3 files to have two digits representing the number.

Sorry, I did not understand the analog pin part, what should I do with it? Attach it to my sd card?

Sorry, I did not understand the analog pin part, what should I do with it? Attach it to my sd card?

The idea is to read a random voltage from an unused, unconnected analogue pin floating at an uncertain voltage and to use the value read seed the "random" number generator to avoid it always starting at the same value and producing the same sequence of numbers.

UKHeliBob:
The idea is to read a random voltage from an unused, unconnected analogue pin floating at an uncertain voltage and to use the value read seed the "random" number generator to avoid it always starting at the same value and producing the same sequence of numbers.

So, do I need to plug a 5v pin into an analog one? Thanks

bogoio:
So, do I need to plug a 5v pin into an analog one? Thanks

He said "unused, unconnected". Does that answer your question?

aarg:
He said "unused, unconnected". Does that answer your question?

Sorry, I'm really new to all of this, everything is a matter of doubt to me at this point, thanks

You were right to ask as you were not sure, but I did choose my words carefully in my reply.

UKHeliBob:
You were right to ask as you were not sure, but I did choose my words carefully in my reply.

No problem.
Going back to my sketch, just another layman doubt.
This is probably the function that tell my arduino to play the file

Serial.println("Calling playFullFile");
// Play one file, don't return until complete
musicPlayer.playFullFile("s1.mp3");
//musicPlayer.playFullFile("s2.mp3");

should I substitute it for this?

myFile[1] = random(MIN_NUMBER/10,MAX_NUMBER/10) + '0'; // Convert a SINGLE random number into ASCII char and stores it at second position of myFile
myFile[2] = random(MIN_NUMBER%10,MAX_NUMBER%10) + '0'; // Convert a SINGLE random number into ASCII char and stores it at third position of myFile
musicPlayer.playFullFile(myFile);

A very grateful thank you

should I substitute it for this?

Why are you dividing MIN_NUMBER and MAX_NUMBER by 10? It would make more sense to simply define MIN_NUMBER and MAX_NUMBER correctly.

Have you renamed the files on the SD card as suggested?

PaulS:
Why are you dividing MIN_NUMBER and MAX_NUMBER by 10? It would make more sense to simply define MIN_NUMBER and MAX_NUMBER correctly.

Have you renamed the files on the SD card as suggested?

I don't know, I was just following what my friend said up in the discussion.
So the correct would be:

myFile[1] = random(MIN_NUMBER,MAX_NUMBER) + '0'; // Convert a SINGLE random number into ASCII char and stores it at second position of myFile
myFile[2] = random(MIN_NUMBER,MAX_NUMBER) + '0'; // Convert a SINGLE random number into ASCII char and stores it at third position of myFile
musicPlayer.playFullFile(myFile);

Why there is this 0 after the min and max numbers? Should I eliminate them too? Thanks

Your file name is a string (array of chars with null terminator).
The random() returns a integer.
If you see the ASCII table, the number 0 is the character 32.
The + '0' is to convert from integer to char.

bogoio:
I don't know, I was just following what my friend said up in the discussion.
So the correct would be:

myFile[1] = random(MIN_NUMBER,MAX_NUMBER) + '0'; // Convert a SINGLE random number into ASCII char and stores it at second position of myFile
myFile[2] = random(MIN_NUMBER,MAX_NUMBER) + '0'; // Convert a SINGLE random number into ASCII char and stores it at third position of myFile
musicPlayer.playFullFile(myFile);

Why there is this 0 after the min and max numbers? Should I eliminate them too? Thanks

This:

myFile[1] = random(MIN_NUMBER/10,MAX_NUMBER/10) + '0'; // Convert a SINGLE random number into ASCII char and stores it at second position of myFile
myFile[2] = random(MIN_NUMBER%10,MAX_NUMBER%10) + '0'; // Convert a SINGLE random number into ASCII char and stores it at third position of myFile
musicPlayer.playFullFile(myFile);

The /10 and %10 is to get the Most and Less signifcant digit from the numbers, respectively.
There are other ways, but I thought in this at the time.

Thank you for your attention Giova! Should I substitute

Serial.println("Calling playFullFile");
// Play one file, don't return until complete
musicPlayer.playFullFile("s1.mp3");
//musicPlayer.playFullFile("s2.mp3");

should I substitute it for this?

myFile[1] = random(MIN_NUMBER/10,MAX_NUMBER/10) + '0'; // Convert a SINGLE random number into ASCII char and stores it at second position of myFile
myFile[2] = random(MIN_NUMBER%10,MAX_NUMBER%10) + '0'; // Convert a SINGLE random number into ASCII char and stores it at third position of myFile
musicPlayer.playFullFile(myFile);

should I substitute it for this?

Why not try it first by printing myFile and seeing what it looks like ?

bogoio:
Thank you for your attention Giova! Should I substitute

Serial.println("Calling playFullFile");
// Play one file, don't return until complete
musicPlayer.playFullFile("s1.mp3");
//musicPlayer.playFullFile("s2.mp3");

should I substitute it for this?

myFile[1] = random(MIN_NUMBER/10,MAX_NUMBER/10) + '0'; // Convert a SINGLE random number into ASCII char and stores it at second position of myFile
myFile[2] = random(MIN_NUMBER%10,MAX_NUMBER%10) + '0'; // Convert a SINGLE random number into ASCII char and stores it at third position of myFile
musicPlayer.playFullFile(myFile);

Yes, but try this suggestion first:

UKHeliBob:
Why not try it first by printing myFile and seeing what it looks like ?

Got it playing random files!
Here is my sketch, it's very simple, I dunno why some other players have a much more complicated code.

#include <SPI.h>
#include <Adafruit_VS1053.h>
#include <SD.h>

// define the pins used
#define CLK 1 // SPI Clock, shared with SD card
#define MISO 3 // Input data, from VS1053/SD card
#define MOSI 2 // Output data, to VS1053/SD card
#define RESET 8 // VS1053 reset pin (output)
#define CS 9 // VS1053 chip select pin (output)
#define DCS 6 // VS1053 Data/command select pin (output)
#define CARDCS 10 // Card chip select pin arduino
#define DREQ 7 // VS1053 Data request, ideally an Interrupt pin
#define MIN_NUMBER 0
#define MAX_NUMBER 5 //Can't be higher than 99
char myFile[8] = "0";

Adafruit_VS1053_FilePlayer musicPlayer = Adafruit_VS1053_FilePlayer(RESET, CS, DCS, DREQ, CARDCS);

void setup() {
Serial.begin(9600);
musicPlayer.begin(); // initialise the music player
SD.begin(CARDCS); // initialise the SD card
musicPlayer.dumpRegs();
musicPlayer.setVolume(00,00);
// musicPlayer.useInterrupt(VS1053_FILEPLAYER_TIMER0_ INT); // timer int
musicPlayer.useInterrupt(VS1053_FILEPLAYER_PIN_INT ); // DREQ int
randomSeed(analogRead(A0));
}
void loop() {
if (! musicPlayer.playingMusic)
myFile[1] = random(MIN_NUMBER/10,MAX_NUMBER/10) + '0'; // Convert a SINGLE random number into ASCII char and stores it at second position of myFile
myFile[2] = random(MIN_NUMBER%10,MAX_NUMBER%10) + '0'; // Convert a SINGLE random number into ASCII char and stores it at third position of myFile
musicPlayer.playFullFile(myFile);
Serial.println("Done playing music");
Serial.println(myFile);
delay(1000);
}

I told the Arduino to do a print of myFile, then I realized it was printing random 3 digit numbers, all I had to do was rename my mp3 to 3 digits numbers. I have little idea of what I've done here, I only got success by try and error, guess this the way to start learning.

Would it be lot different to add a PIR motion detector than to add a push button to this sketch? Thanks