I've read that the Arduino String is bad as it causes memory fragmentation, so I try to avoid them mostly. But I still have some occurences where I'm not quite sure if they are ok after all.
The first is in print statements:
I have a function which prints a success or error message to the serial port, like so: Serial.println("Success");
. Is this ok or should I (for example) pre define the String using #define SUCCESS "SUCCESS"
?
Which leads me to the next case, defines:
I'm using MQTT and have the topics defined like above, e.g. #define TOPIC_COLOR "light/color"
. Is this fine?
A third one is with ArduinoJSON:
I'm using Strings as keys, like so lights["red"] = redVal;
. This call is made at multiple places so I assume the string would be created multiple times? A workaround/improvement I've thought of is using an enum defining the properties and using that as key.
Thanks for all comments, tips and explanations!
String
class.String
objects that problems occur. I don't know what ArduinoJSON does with string indices, though. On smaller MCUs (AVR) you should useF("stringliteral")
to reduce RAM usage whenever possible, though.