I'm currently writing and testing a professional Arduino application, I'm aware of the dangers of using the String class, so I began to implement C-style strings in some places, but it's just way harder using them to receive data from a Software Serial port that's variable in size and needs some manipulation.
I've upgraded from an Arduino Uno to a Leonardo so my current minimum SRAM usage is 1325 bytes (51%)
Here's the relevant part of my code:
String serialContents;
char emailChar[SIZEOF_ARRAY][SIZEOF_CHAR];
void setup(){
serialContents.reserve(70);
SerialOne.begin(9600);
}
void loop{
if (SerialOne.available() > 0) //something is there to be read
{
serialContents = SerialOne.readStringUntil('\n'); //make a string of the characters coming on serial
}
if (serialContents.length() >= 60)
{
serialContents = ""; //ignore serial data
}
else{
serialContents = cutString(serialContents); //some String manipulation, no adding only cutting
serialContents.replace("@", "%40");
serialContents.toCharArray(emailChar[count], 31);
serialContents = ""; //String reset
}
So my question is, does something like this make my code unstable? Does the reserve() function help at all?
Edit: Here's the cutString() function
String cutString(String request) {
int points = request.indexOf(':');
if (request.substring(0, points) == "CHECKOUT")
{
modoPedido[count] = 'o'; //char array
}
else if (request.substring(0, points) == "CHECKIN")
{
modoPedido[count] = 'i';
}
request = request.substring(points + 1);
return request;
}