Issues adding ".MP3" to the end of a randomly generated number

Hey all, I'm new to Arduino and coding in general and could use some help.

I'm using an Arduino with a Adafruit VS1053, I currently have 4 sound files on the micro SD just named with a number "1" or "2" (will eventually have around 20 files), however the VS1053 seems to load the files with ".MP3" on the end of it so the code doesn't work since it's just outputting the number alone (created from a random number generator here:
itoa(random(MIN_NUMBER,MAX_NUMBER),myFile,10);

I have tried just adding the ".MP3" part onto the end of it like this:
musicPlayer.playFullFile(myFile) + ".MP3";
Which didn't end up working, I also tried adding it here:
itoa(random(MIN_NUMBER,MAX_NUMBER),myFile + ".MP3",10);
Which just gives the following error:
invalid operands of types 'char [3]' and 'const char [5]' to binary 'operator+'

I'm a little lost how I can correctly add the ".MP3" to the end of the randomly generated number so that it can play the audio correctly using the musicPlayer.playFullFile.

I know this is probably quite a basic question and I apologize for that, but after multiple attempts to fix it on my own I'm running out of ideas. If someone could point me in the right direction I would appreciate it a lot! :grin: If you need any more information, just ask!

Here is the full code for reference :slight_smile:

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

// These are the pins used for the breakout example
#define BREAKOUT_RESET  9      // VS1053 reset pin (output)
#define BREAKOUT_CS     10     // VS1053 chip select pin (output)
#define BREAKOUT_DCS    8      // VS1053 Data/command select pin (output)
// These are the pins used for the music maker shield
#define SHIELD_RESET  -1      // VS1053 reset pin (unused!)
#define SHIELD_CS     7      // VS1053 chip select pin (output)
#define SHIELD_DCS    6      // VS1053 Data/command select pin (output)
int inPinLeft = 7;    // bumper input (left)
int inPinRight = 2;    // bumper input (right)
int valL = 0;
int valR = 0;

#define MIN_NUMBER 0
#define MAX_NUMBER 5 //Can't be higher than 99
char myFile[3] = "0";

// These are common pins between breakout and shield
#define CARDCS 4     // Card chip select pin
// DREQ should be an Int pin, see http://arduino.cc/en/Reference/attachInterrupt
#define DREQ 3       // VS1053 Data request, ideally an Interrupt pin

Adafruit_VS1053_FilePlayer musicPlayer = 
  // create breakout-example object!
  Adafruit_VS1053_FilePlayer(BREAKOUT_RESET, BREAKOUT_CS, BREAKOUT_DCS, DREQ, CARDCS);
  // create shield-example object!
  //Adafruit_VS1053_FilePlayer(SHIELD_RESET, SHIELD_CS, SHIELD_DCS, DREQ, CARDCS);


void setup() {
  Serial.begin(9600);
  Serial.println("Adafruit VS1053 Simple Test");
  randomSeed(analogRead(A0));
  pinMode(inPinLeft, INPUT);
  pinMode(inPinRight, INPUT);

  if (! musicPlayer.begin()) { // initialise the music player
     Serial.println(F("Couldn't find VS1053, do you have the right pins defined?"));
     while (1);
  }
  Serial.println(F("VS1053 found"));
  
   if (!SD.begin(CARDCS)) {
    Serial.println(F("SD failed, or not present"));
    while (1);  // don't do anything more
  }

  // list files
  printDirectory(SD.open("/"), 0);
  
  // Set volume for left, right channels. lower numbers == louder volume!
  musicPlayer.setVolume(0,0);

  // Timer interrupts are not suggested, better to use DREQ interrupt!
  //musicPlayer.useInterrupt(VS1053_FILEPLAYER_TIMER0_INT); // timer int

  // If DREQ is on an interrupt pin (on uno, #2 or #3) we can do background
  // audio playing
  musicPlayer.useInterrupt(VS1053_FILEPLAYER_PIN_INT);  // DREQ int 
}


void loop() {
  
  valL = digitalRead(inPinLeft);
  //Serial.println(valL);
  if (digitalRead(inPinLeft) == HIGH)
  {
    Serial.println(F("Left bumper pressed"));
  itoa(random(MIN_NUMBER,MAX_NUMBER),myFile,10);
  Serial.println(myFile);
  musicPlayer.playFullFile(myFile);
  delay(2000);
  }

  
  valR = digitalRead(inPinRight);
  //Serial.println(valL);
  if (digitalRead(inPinRight) == HIGH)
  {
  Serial.println(F("Right bumper pressed"));
  itoa(random(MIN_NUMBER,MAX_NUMBER),myFile,10);
  Serial.println(myFile);
  musicPlayer.playFullFile(myFile);
  delay(2000);
  }
}


/// File listing helper
void printDirectory(File dir, int numTabs) {
   while(true) {
     
     File entry =  dir.openNextFile();
     if (! entry) {
       // no more files
       //Serial.println("**nomorefiles**");
       break;
     }
     for (uint8_t i=0; i<numTabs; i++) {
       Serial.print('\t');
     }
     Serial.print(entry.name());
     if (entry.isDirectory()) {
       Serial.println("/");
       printDirectory(entry, numTabs+1);
     } else {
       // files have sizes, directories do not
       Serial.print("\t\t");
       Serial.println(entry.size(), DEC);
     }
     entry.close();
   }
}

Thanks in advance! :slight_smile:

You are working with strings (null terminated character arrays) so cannot use + to concatenate the strings. See the strcat() function.

A file name cannot start with a number.
Edit: Statement incorrect.

char myFile[7];  



sprintf(myFile, "%d.MP3", random(MIN_NUMBER,MAX_NUMBER));

you may want to check if the file exists if you don't want to maintain previous files

Thanks all for the quick advice!
@groundFungus I will definitely look into this more and try to understand it better.
@PaulRB It worked perfectly :grin: I will look more into the way you structured it and try to learn from it, thank you!
@gcjr That's a really good idea! I may try to look into this soon as well as some other small features I wanted to work on.

I know it's only a small portion of you here, but everyone on this forum is so helpful! It's an awesome feeling to finally have my first (and maybe slightly ambitious) Arduino project finally working and being in what I would consider a "completed" state. Thanks to everyone who sees this! :slight_smile: good luck in your future projects!

Where? It's ok in DOS, Windows and Linux I think.

I looked and find that I was mistaken. I must have confused the file naming with variable names. Corrected in my original post.

1 Like

Close!

itoa(random(MIN_NUMBER,MAX_NUMBER), myFile, 10);
strcat(myFile, ".MP3");
musicPlayer.playFullFile(myFile);
1 Like