0

The method sprintf() is not valid on Arduino. How else can I convert a string to char*?

 if(triggerNumber == 4)
 { currenttrack = 5;
 }
  sprintf(trackName, "track%03d.mp3", currenttrack);

  playMP3(trackName); //Go play XX.mp3
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • Possible duplicate of http://stackoverflow.com/a/7391187/3093378 – vsoftco Jul 29 '14 at 12:37
  • This link might help, although it is C++, it has close relationship to Arduino programming: [link](http://stackoverflow.com/questions/9309961/how-to-convert-string-to-char-in-c) – James Jul 29 '14 at 12:40
  • In standard C++ `sprintf` is not a *method* (it is not a *member* function). It is just a standard *function*. – Basile Starynkevitch Jul 29 '14 at 13:01

3 Answers3

1

I believe snprintf will work if you include stdio.h

snprintf(trackName, sizeof(trackName), "track%03d.mp3", currenttrack);

Montdidier
  • 1,181
  • 1
  • 8
  • 21
1

There is a String class in Arduino that you can use. In your case

String trackName = "track";
track += currentTrack;
if (currentTrack < 9) {
    track += currentTrack;
}
else {
    track += "0";
    track += currentTrack;
}
track += ".mp3";
playMP3(trackName);

There are other String manipulation methods in the String class that may be useful. See http://arduino.cc/en/Reference/StringObject

0

you can simply use .c_str().

Returns a pointer to an array that contains a null-terminated sequence of characters (i.e., a C-string) representing the current value of the string object.

http://www.cplusplus.com/reference/string/string/c_str/