Playing random MP3 files

Hi.
I've managed to get my Arduino doing more or less what I need thanks to pylon.

The last thing I now need to do is send a command to a Vmusic2 module to play a random sample numbered between 1 and 50. ie, 1.mp3 2.mp3 3.mp3.. etc..

The code I currently have is this.

thisMP3 = random(1, 50);
  Serial.print("VPF thisMP3.mp3"); //Play Random MP3
  Serial.print("\r");

I need to send the command 'VPF' to tell it to play the sample, followed by the random number between 1 and 50, and .mp3 at the end.
It's not working though as I guess I the arduino is actually sending the text 'VPF thisMP3.mp3' to the module, rather than the integer value that 'thismp3' is meant to represent.

Is there a way to somehow create a string that begins with VPF, a space, the random number and .mp3 so I can send that to the module?

sorry if I've explained badly what is probably a simple problem :slight_smile:

One easy way is to use sprintf - you'll find plenty of examples in the forums.

thisMP3 = random(1, 50);
  Serial.print( F( "VPF " ) );
  Serial.print( thisMP3 );
  Serial.print( F( ".mp3" ) ); //Play Random MP3
  Serial.write('\r');

Using the F-macro is optional (but this is a good time to learn about it). The last change (print("\r")) is also optional.

Excellent stuff.. that works perfectly!! Many thanks!!

I'm so new to these things, but this is fantastic. I can now work on some of the easier bits of the code. :slight_smile:

Thanks again!
Glyn.