Play random Wav files on Arduino Uno

Hey,
I want my arduino uno to play one of 10 Wav files as soon as it starts. Not always the same, but always a random one. I use an SD card module with the TMRpcm code stuff, which also works very well. I would be happy if someone has a solution!

char toPlay[11];     //  string array for file to play 00.WAV to 99999.WAV

// then pass this function a random in the range of the files you have
makeName(randNumber,0);  // generate file name in global array toPlay

void makeName(int number, int depth){  // generates a file name 0.WAV to 9999.WAV suppressing leading zeros
  if(number > 9) {
    makeName(number / 10, ++depth); // recursion
    depth--;
    number = number % 10;   // only have to deal with the next significant digit of the number
     }
  toPlay[indexToWrite] = (number & 0xf) | 0x30;
  indexToWrite++;
  if(depth > 0) return; // return if we have more levels of recursion to go
  else {  // finish off the string with the wave extesion
     toPlay[indexToWrite] = '.';
     toPlay[1+indexToWrite] = 'W';
     toPlay[2+indexToWrite] = 'A';
     toPlay[3+indexToWrite] = 'V';
     toPlay[4+indexToWrite] = '\0'; // terminator
     indexToWrite = 0; // reset pointer for next time we enter
  }
}

(By the way, I've already tried this code, but it didn't work ...)

Lots of love
Nici <3

By the way, I've already tried this code, but it didn't work

Please post all your code so others can try it.

I seem to recognise that code.

want my arduino uno to play one of 10 Wav files as soon as it starts

Then why use a code that generates:-

a file name 0.WAV to 9999.WAV

Most of the time it will be generating file names you don't have on your SD card.

Yeah, I already looked though the internet for answers and I tried your code.

I removed the random generator part, but here is my code:

#include <SD.h>                       // need to include the SD library
//#define SD_ChipSelectPin 53         // example uses hardware SS pin 53 on Mega2560
#define SD_ChipSelectPin 10            // using digital pin 10 on arduino uno 328, can use other pins
#include <TMRpcm.h>                   // also need to include this library...
#include <SPI.h>

TMRpcm tmrpcm;


void setup()
{
tmrpcm.speakerPin=9;            //5,6,11 or 46 on Mega, 9 on Uno, Nano, etc
                                //Complimentary Output or Dual Speakers:
                                //pinMode(10,OUTPUT); Pin pairs: 9,10 Mega: 5-2,6-7,11-12,46-45 
Serial.begin(9600);
if(!SD.begin(SD_ChipSelectPin))
{
  Serial.println("SD fail");
  return;
}
tmrpcm.setVolume(6);
tmrpcm.play("1.wav");

And I used your code bc I haven't found anything else and I have no clue how to generate random stuff. I hope u know what I mean...

I still have no clue how to achieve my goal...:confused:

I have no clue how to generate random stuff

See this:-

For just a single digit file name, get a number between 0 and 9 , and then turn it into an ASCII character 0 to 9 and then concatenate it to the file name array.

Like this:-

(number & 0xf) | 0x30;

In fact that is what that function of mine actually does.

So put it all together and you get:-

#include <SD.h>                       // need to include the SD library
//#define SD_ChipSelectPin 53         // example uses hardware SS pin 53 on Mega2560
#define SD_ChipSelectPin 10            // using digital pin 10 on arduino uno 328, can use other pins
#include <TMRpcm.h>                   // also need to include this library...
#include <SPI.h>

TMRpcm tmrpcm;
char toPlay[11];     //  string array for file to play 00.WAV to 99999.WAV
int indexToWrite = 0; // place to write in toPlay array

void setup()
{
tmrpcm.speakerPin=9;            //5,6,11 or 46 on Mega, 9 on Uno, Nano, etc
                                //Complimentary Output or Dual Speakers:
                                //pinMode(10,OUTPUT); Pin pairs: 9,10 Mega: 5-2,6-7,11-12,46-45
Serial.begin(9600);
if(!SD.begin(SD_ChipSelectPin))
{
  Serial.println("SD fail");
  return;
}

// randomSeed() will then shuffle the random function.
randomSeed(analogRead(0));
int randNumber = random(9); // random number between 0 and 9
makeName(randNumber, 0); // generate file name
tmrpcm.setVolume(6);
tmrpcm.play(toPlay);
}

void loop(){
  // fill this with what you want to do after the start
}

void makeName(int number, int depth){  // generates a file name 0.WAV to 9999.WAV suppressing leading zeros
  if(number > 9) {
    makeName(number / 10, ++depth); // recursion
    depth--;
    number = number % 10;   // only have to deal with the next significant digit of the number
     }
  toPlay[indexToWrite] = (number & 0xf) | 0x30;
  indexToWrite++;
  if(depth > 0) return; // return if we have more levels of recursion to go
  else {  // finish off the string with the wave extesion
     toPlay[indexToWrite] = '.';
     toPlay[1+indexToWrite] = 'W';
     toPlay[2+indexToWrite] = 'A';
     toPlay[3+indexToWrite] = 'V';
     toPlay[4+indexToWrite] = '\0'; // terminator
     indexToWrite = 0; // reset pointer for next time we enter
  }
}