I am always being advised not to use String class variables and to use char arrays instead.
I did notice earlier when I changed a String variable to a char variable[], there did not seem to be much difference in memory usage.
In any case I did a little experiment and in my sketch changed 2 String class variables to char arrays.
The posted code are the differences between the 2 sketches.
Original code (extract):
...
String cid;
String sysRef;
...
void setup()
{
...
cid = Ref1;
cid += "-";
cid += Ref2;
...
sysRef = cid;
sysRef += "_";
sysRef += Ref3;
sysRef += ":";
sysRef += Ref4;
...
void loop() {
// put your main code here, to run repeatedly:
}
Sketch uses 26,736 bytes (82%) of program storage space. Maximum is 32,256 bytes.
Global variables use 1,600 bytes (78%) of dynamic memory, leaving 448 bytes for local variables. Maximum is 2,048 bytes.
Revised code...
...
char cid[10];
char sysRef[16];
...
void setup()
{
...
sprintf(cid, ("%s-%s"), Ref1, Ref2);
...
sprintf(sysRef, ("%s_%s:%s"), cid, Ref3, Ref4);
...
void loop() {
// put your main code here, to run repeatedly:
}
Sketch uses 28,036 bytes (86%) of program storage space. Maximum is 32,256 bytes.
Global variables use 1,616 bytes (78%) of dynamic memory, leaving 432 bytes for local variables. Maximum is 2,048 bytes.
So I see my use of String class variables uses less memory and I wonder why when everyone tells me it should be the opposite.
So the first question is, is there a more efficient way to derive cid and sysRef other than using sprintf?
FWIW it should be noted that the sketch does contain other String class variables.