How to combine string and int variables into a string?

Hi there,

I have a series of files on my SD card that I want played in order each time the code loops. The files are named HELLO1.wav HELLO2.wav etc. I have so far got

 if (HELLO = 5) {
        HELLO = 1;
      } // there are 4 HELLO files. The loop adds 1 each time so if the number is greater than the number of files, it resets to the first file.
      Wire.beginTransmission(9); // I am transmitting to another arduino with the Adafruit Music Maker shield 
      String HELLOcommand = '/HELLO' + HELLO + '.wav'; // so HELLO is an int defined as starting at 1. This HELLOcommand is supposed to combine /HELLO and the int and .wav to give, for example. /HELLO1.wav as the filename to play. This is where the code is getting stuck.

This is giving me an error Conversion from int to String is ambiguous

Doesn't seem very ambiguous to me! I am more used to coding in Excel, and Visual Basic, which I know is different but they share some similarities. I would just get the result I want by using & usually, but here I am stuck. Any advice? Thanks!!


(post deleted by author)

why not just use a if statement to output the desired wave file to play with the condition of the hello int matching instead of using a string? its more lines of code but it should do the thing ... with the stuff

1 Like

Single quotes defines characters not strings. Double quotes define strings. If you use those you get a more sensible error.

invalid operands of types 'const char' and 'const char [5]' to binary 'operator+'*

Which translates to: An integer is not a string!

Convert the integer to a string first and then concatenate the strings like this:

String str = "/hello" + String(hello) + ".wav";

OP, What's the difference between a " and a '?

those code tag thingies use em.

Does Wire.write know about Strings?

get a more sensible error, lol!

Lots of ways to do it, I use itoa a lot.
char Txt[20]={};
int yourINT=1;
Lots of ways to do it, I use itoa a lot.
strcpy(Txt, "/hello");
itoa(yourINT, Txt + strlen(Txt), 10);
strcat(Txt, ".wav");

I don't know what that means?

Thank you that was really helpful and is now working!

The next issue (haha) is for some reason I am now getting a Wire.write error:

no matching function for call to 'TwoWire::write(String)'

try the one I gave you as a separate program and see if it works. add a Serial.print(Txt);

Code tags?

See reply #9

But what is the issue? I don't understand. You weren't pointing out an issue you were just saying it's an "oopsie". That isn't helpful.

That's all you wrote, I don't know? I don't know what you mean?

What does that mean

(post deleted by author)

Why don't you just tell me? Why are you being cryptic? You already have the knowledge, and you are clearly prepared to spend time writing a reply, why don't you use that time to explain what the problem is and then just write the solution, instead of dropping cryptic hints?

It means you missed reading some stuff that you were directed to read when you signed up for the site

You will learn a lot more by doing your own research.

1 Like