Hi Guys,
I have an issue with building long strings and printing them to Serial.
To explain the code below real quick. I am getting a bunch of different sensor values and storing them all into strings, I am then adding all those strings together to create one long string value that I can send where ever I need to.
The Problem:
When I run the sketch and check the Serial monitor, the string is built and prints as it is supposed to, but from the second loop and forever after - the Serial prints out just half of the string from "$C" on wards.
Below is a minimal sketch with test values to show this problem
//These Strings are updated along the course of the loop and then inserted into the string that is made in buildString().
String ECReading = "TestECReading";
String RainReading = "TestRainReading";
String VoltageReading = "TestVoltageReading";
String GSMSignal = "$GSMS";
String NodeID = "$NID";
String TimeDate = "$TD";
String ProbeID = "$ID";
// These String Values are updated at different intervals by getting the values via Serial. They too are also thrown in the main report string in buildString().
String temperatureReading = "-13.48605,- 7.884103,-10.37,- 13.05851,-8.596266,-13.48605,- 7.884103,-10.37,- 13.05851,-8.596266,-13.48605,- 7.884103,-10.37,- 13.05851,-8.596266";
String moistureReading = "-13.48605,- 7.884103,-10.37,- 13.05851,-8.596266,-13.48605,- 7.884103,-10.37,- 13.05851,-8.596266,-13.48605,- 7.884103,-10.37,- 13.05851,-8.596266";
String reportString = "";
void setup(){
Serial.begin(9600);
}
void buildString(){
reportString = String("!!") + "," + "PS" + "," + "0" + "," + ProbeID + "-" + NodeID + "_" + TimeDate + "," + "$A" + "," + moistureReading + "," + "$B" + "," + temperatureReading + "," + "$C" + "," + ECReading + "," + "$D" + "," + RainReading + "," + VoltageReading + "," + GSMSignal + "," + "!";
Serial.println(reportString);
reportString = ""; // clear for the next String build
}
void loop(){
buildString();
delay(5000);
}
I KNOW this is NOT an efficient way to do this, and its partly the problem as to why I am getting this issue. I know I am meant to be using char arrays but I am not sure how they work for my situation (some values not being const strings). If anyone has an examples that would work best for me, that would be so great!
Otherwise, could anyone show me where I am going wrong and how to make this more efficient?
Thanks!
Regards,
Bren