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
3
  • Possible duplicate of stackoverflow.com/a/7391187/3093378 Commented Jul 29, 2014 at 12:37
  • This link might help, although it is C++, it has close relationship to Arduino programming: link Commented Jul 29, 2014 at 12:40
  • In standard C++ sprintf is not a method (it is not a member function). It is just a standard function. Commented Jul 29, 2014 at 13:01

3 Answers 3

1

I believe snprintf will work if you include stdio.h

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

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.