Concatenating a string and a int or word variable

I am very new to C++ and Arduino. I am trying to combine a string and the value held in a int or word variable into another variable and send this to a functionthat paints the Serial Monitor or and external LCD.

Here are a couple of declarations made earlier in my sketch

char LCDMessage[25] = " "; word L_dist = 0; //or int L_dist = 0 may be -255 to 255 Here is an example of a function that calls the other function that paints the LCD. Below setting the motor direction and speed it paints the LCD or Serial Monitor; mostly for debugging

void goFwdFast(){ // Forward full speed strcpy(LCDMessage, "Forward fast"); LCDPrintScreen(); analogWrite(rt_enable_pin, 255); digitalWrite(rt_forward_pin, HIGH); digitalWrite(rt_reverse_pin, LOW); analogWrite(lt_enable_pin, 255); digitalWrite(lt_forward_pin, HIGH); digitalWrite(lt_reverse_pin, LOW); }

Here is the function that paints the LCD. If PIN3 is high it paints the LCD or Serial Monitor, else it clears the screen.

void LCDPrintScreen(){ if ( digitalRead(debugPin) == HIGH ){ Serial.print("?f"); //clear the screen - this only works with an external LCD screen. In the Serial Monitor you get the ?f Serial.print(LCDMessage); } else { Serial.print("?f"); //clear the screen - this only works with an external LCD screen. In the Serial Monitor you get the ?f }


This stuff works fine. I can load LCDMessage with most any string up to 25 characters and it work just as expected. Well, now I want to put two pieces of data in the LCDMessage variable. The first piece with be a piece of test like below

strcpy(LCDMessage, "The distance is ");

And then add the information in another variable that contains the that distance, like below

L_dist = 169;

a pseudo code example would be something like

strcpy(LCDMessage, "The distance is " + L_dist)

These two values combined, paint the LCD with

The distance is 169

So all I want is a simple way to combined these two value together to send it to my function within the variable LCDMessage.

'LCDmessage' will hold at most a 24 character string, not 25.

The functions you want may include 'itoa' and 'strcat'.

When posting code, please use the # icon on the editor's toolbar.


I tried strcat but still get an error. Perhaps I am not using it correctly.

   strcpy(LCDMessage,"Left ");
   strcat(LCDMessage, char(L_dist));

I get the following error messages:

L298_encoder_testing:325: error: invalid conversion from 'char' to 'const char*' L298_encoder_testing:325: error: initializing argument 2 of 'char* strcat(char*, const char*)'

Your thoguhts.


You need to convert "L_dist" to a string (maybe use "itoa" and a small buffer) before using "strcat".