/* MemFrag_ex2. by Matthew Ford Copyright(c)2021 Forward Computing and Control Pty. Ltd. This example code is in the public domain. www.forward.com.au/pfod/ArduinoProgramming/ArduinoStrings/index.html */ String string1; String string2; String string3; void setup() { Serial.begin(9600); for (int i = 10; i > 0; i--) { Serial.print(i); Serial.print(' '); delay(500); } Serial.println(); Serial.println(F("Memory Fragementation Example 2")); string1.reserve(32); string2.reserve(32); if (!string3.reserve(32)) { // check the last largest reserve while (1) { // stop here and print repeating msg Serial.println(F("Strings out-of-memory")); delay(3000); // repeat msg every 3 sec } } } void printDegC(float value, String& result) { String title = F("This is the temperature in the boiler room "); String units = F("degC"); formatResult(title, units, value, result); } void formatResult(const String& title, const String& units, float value, String& result) { result = title; result += String(value, 1); //temp to 1 decimal only result += units; } void loop() { float temp = 27.35; printDegC(temp, string2); Serial.println(string2); Serial.println(F(" -- loop() returns --")); Serial.println(); }