String.concat();

Try this ....

As far as I can tell, 'localstring' ends up as LLLL..... the expected 200 bytes long, whereas 'globalstring' ends up as GGGG.... maybe 50 bytes long, with the rest lost in space. Is "concat" only working within the local function?

Ideas?

#include <NewSoftSerial.h>
NewSoftSerial LCD(2, 3);

String globalstring = "";
char inByte;

void setup() {
LCD.begin(115200);
CONCATTEST();
}

void loop(){
//
}

void CONCATTEST(){
String localstring;
for (int y = 0;y<200;y++){
inByte = 'L';
localstring.concat(inByte);
}
LCD.print(localstring);

globalstring = "";
for (int y = 0;y<200;y++){
inByte = 'G';
globalstring.concat(inByte);
}
LCD.print(globalstring);
}

concat should work on every String. Sofar I see no problem in the code. How do you know the amount of L's and G's? What is connected to the serial port at 2/3 ? The serial LCD's I know of have no 200 positions.

A better way is to concat different chars to the string so you can check the output easier. First add 10 A's then 10 B's etc.
or 1234567891123456789212345678931234567894 etc (NOTE iso 0's I use 1 2 3 etc to count the 10's )

Thanks,

The number of L's and G's comes from viewing the LCD (I'm using a huge LCD which fits several hundred characters - connected to 2/3 yes - actually '3' only, since this is one way communication).

I use the LCD in the example only because my Arduino currenly is hard-wired to the LCD inside a box... but the LCD is not the issue - it's the CONCAT. Looks like I will have to redo my code to get around what seems like a compiler error.

I came upon the problem deep within the large sketch I'm working on, so the attached is just a snippet to isolate the problem.

I have to concatenate byte by byte (unless that officially doesn't work), since I receive those byte by byte over Serial input. (the trick with L and G, for-loops etc in the attached code is just to prove the point, i.e. to understand whether String.concat() is able to fill up a "global" string declared outside the actual funcion. Looks like the answer is "cannot" - too bad.

Without digging too deeply myself I suspect you are running out of RAM. String.concat takes a const refererence to a String object and the String class has a constructor that takes a char.

Because the object reference is const the compiler is free to construct implicit instances of String for you as needed on the stack, and you are doing it in a loop times 200. You can see what might happen to the stack...

To verify this is the problem you could replace your concats in the loop with a call to a function like this:

void myConcat(String& dest,char c) {
  dest.concat(c);
}

Aha!

Good input - that sounds very believable. I shall work around that and create some less RAM-demanding version of this.

Thanks!