Use random number in filename for VS_1053 playFullFile results in errors …

Hello folks,

I did not think, this would be that hard. I want a mp3 file to be played, when a touch on GPIO happens. Let’s say I have 9 mp3 files, track001.mp3 to track009.mp3 …

  if (musicPlayer.GPIO_digitalRead(6) == HIGH) {
    int i = random(1, 10);

    musicPlayer.playFullFile("track00" + i + ".mp3");
 
    if (Serial.available()) {
      Serial.println("Playing track00" + i + ".mp3");
    }

Unfortunately, this results in:
error: invalid operands of types 'const char*' and 'const char [5]' to binary 'operator+'
invalid operands of types 'const char*' and 'const char [5]' to binary 'operator+'

Could anyone please tell me, what I would have to use instead of int or how I could “cast” this int variable in the brackets between "track00" and ".mp3"?

I’m really stuck on this.

Kindest regards and thanks very much!

You could use the String class, or sprintf, or itoa and strcat.

void setup() {
  Serial.begin(115200);
  char fName[13];
  sprintf_P(fName, PSTR("track%03d.mp3"), 42);
  Serial.print(F("The name is '"));
  Serial.print(fName);
  Serial.println(F("'"));
}
void loop() {}
The name is 'track042.mp3'

You really want to send the string to the serial monitor only when something is pending on its input?

    if (Serial.available()) {
      Serial.println("Playing track00" + i + ".mp3");
    }

AWOL:
You could use the String class, or sprintf, or itoa and strcat.

Would you have examples for these? … so I know the difference in the future.

Whandall:

void setup() {

Serial.begin(115200);
 char fName[13];
 sprintf_P(fName, PSTR("track%03d.mp3"), 42);
 Serial.print(F("The name is '"));
 Serial.print(fName);
 Serial.println(F("'"));
}
void loop() {}





The name is 'track042.mp3'

Wow, that looks way more complex, than I thought …

Whandall:
You really want to send the string to the serial monitor only when something is pending on its input?

    if (Serial.available()) {

Serial.println("Playing track00" + i + ".mp3");
   }

To be honest, I understood this as “print only to the Serial monitor, if it was opened” …

fooness:
To be honest, I understood this as “print only to the Serial monitor, if it was opened” …

I thought so. :wink:

My code looks a little complicated, because the format string is in PROGMEM and because it is a working example.
If you like to have the format in RAM, its a little simpler (but wasting RAM)

  sprintf(fName,"track%03d.mp3", 42);

If you want to assemble a char you need a buffer and functions filling it with the computed result.
sprintf is the very versatile C-function to build a complex string in one step. Using sprintf raises the code size, because it is a rather complicated function.

You could use the String class that only hides the complexity a bit, but I would rather not take that way.

You don't get something for nothing.

fooness:
Would you have examples for these? … so I know the difference in the future.

I'm not going to give a String example, because I don't want to encourage you to use it.

 char fName[13] = "track00";
  char buf [2];
  strcat (fName, itoa (singleDigitTrackNumber, buf, 10));
  strcat (fName, ".mp3");

sprintf is a bit bloated, but makes handling the leading zeroes a little easier.

I see, so I try to remember not to use String in Arduino and I’m thanking you both very much!