Hi ArduinoFellas,
I have a question about code optimization:
For testing what would be the most efficient code, I changed only a section of my test sketch as depicted below, compiled and take note about the compiler results on memory usage, as follows:
if (RTC.read(tm)) {
digitalWrite(OnBoardLED, HIGH); // turn the onboard LED on
// Calendar Day
Day = tm.Day;
// Calendar Month
Month = tm.Month;
// Calendar Year
Year = (tmYearToCalendar(tm.Year) - 2000); // Prints only last 2-digit year
// Calendar Hours
Hours = tm.Hour;
// Calendar Minutes
Minutes = tm.Minute;
// Calendar Seconds
// Seconds = tm.Second;
// Temperature
Temperature = DHT11.temperature, 1;
// Relative Humidity
Hum = DHT11.humidity, 0;
// Barometric pressure
Baro = 1061.2;
// Dew Point
DP = dewPoint(DHT11.temperature, DHT11.humidity);
// Dew Point Fast
// DP = dewPointFast(DHT11.temperature, DHT11.humidity);
DP = ((int)(DP * 100)/100.0);
// Print to serial port if need.
// 'slash' used as Date separator, 'semicolon' used as Field separator
if (PrintSerial == true){
DummyString = DigitToString(Day) + slash + DigitToString(Month) + slash + DigitToString(Year) + semicolon + DigitToString(Hours) + colons + DigitToString(Minutes) + semicolon + DigitToString(Temperature) + semicolon + DigitToString(Hum) + semicolon + Baro + semicolon + DP + semicolon + EEPROMAddress + Asterix;
Serial.println(DummyString); // Prints and ends line with CR + LF
}
The compiler says the following:
// Sketch uses 13,168 bytes (42%) of program storage space. Maximum is 30,720 bytes.
// Global variables use 672 bytes (32%) of dynamic memory, leaving 1,376 bytes for local variables.
// Maximum is 2,048 bytes.
if (RTC.read(tm)) {
digitalWrite(OnBoardLED, HIGH); // turn the onboard LED on
// Calendar Day
Day = tm.Day;
// Calendar Month
Month = tm.Month;
// Calendar Year
Year = (tmYearToCalendar(tm.Year) - 2000); // Prints only last 2-digit year
// Calendar Hours
Hours = tm.Hour;
// Calendar Minutes
Minutes = tm.Minute;
// Calendar Seconds
// Seconds = tm.Second;
// Temperature
Temperature = DHT11.temperature, 1;
// Relative Humidity
Hum = DHT11.humidity, 0;
// Barometric pressure
Baro = 1061.2;
// Dew Point
DP = dewPoint(DHT11.temperature, DHT11.humidity);
// Dew Point Fast
// DP = dewPointFast(DHT11.temperature, DHT11.humidity);
DP = ((int)(DP * 100)/100.0);
// Print to serial port if need.
// 'slash' used as Date separator, 'semicolon' used as Field separator
if (PrintSerial == true){
Serial.println(DigitToString(Day));
Serial.println(slash);
Serial.println(DigitToString(Month));
Serial.println(slash);
Serial.println(DigitToString(Year));
Serial.println(semicolon);
Serial.println(DigitToString(Hours));
Serial.println(colons);
Serial.println(DigitToString(Minutes));
Serial.println(semicolon);
Serial.println(DigitToString(Temperature));
Serial.println(semicolon);
Serial.println(DigitToString(Hum));
Serial.println(semicolon);
Serial.println(Baro);
Serial.println(semicolon);
Serial.println(DP);
Serial.println(semicolon);
Serial.println(EEPROMAddress);
Serial.println(Asterix);
Serial.println(DummyString); // Prints and ends line with CR + LF
}
Compiler results:
// Sketch uses 12,410 bytes (40%) of program storage space. Maximum is 30,720 bytes.
// Global variables use 684 bytes (33%) of dynamic memory, leaving 1,364 bytes for local variables.
// Maximum is 2,048 bytes.
For some reason, the code with multiple Serial.println() callings use less program storage than the other with a single Serial.println() call, could somebody explain me why I got this odd(?) behavior?
Thanks in advance