Uno Ram Usage ?

Hi,

Am fairly new to C++ and while coding a project find the RAM is almost used up, yet the global and local variables I specified seem low by comparison and am only using 4 common libraries.

Searching around seems like nested functions can cause high usage but not doing that, its all quiet simple sequential code.

Do these figures look typical for C++, do the libraries/C++ normally use up so much ram or is something wrong ?

thanks.

Libraries - Wire, RTClib, LiquidCystal, EEprom

Globals - Floats 30, Bytes 70, Int 10, Locals Bytes 10 - estimate about 330 bytes in total ?

from Studio 7 and A 1.65

Binary sketch size: 23,624 bytes (used 73% of a 32,256 byte maximum) (0.81 secs)
Minimum Memory Usage: 1726 bytes (84% of a 2048 byte maximum)

You are probably wasting your RAM on strings constants that need RAM when not put to PROGMEM.

But without seeing you code, that's just a guess.

Globals - Floats 30, Bytes 70, Int 10, Locals Bytes 10 - estimate about 330 bytes in total ?

(30 * 4) + 70 + (10 * 2) + 10 = ?

This is the Programming section; it's a reasonable expectation to see code.

Sorry Guys, just thought I might have missed a very general point about C++ and Ram useage as its all new to me. ( old Pic Assembly coder)

My full code attached, please excuse/ don’t laught too much at its simplicity as finding it hard to convert to the C++ way.

form.ino.ino (48.8 KB)

Everything that looks like thislcd.print("Tank Controller ");should look like thislcd.print(F("Tank Controller "));

You’ve got a lot of code for adding leading zeroes, it seems, like this

		if (ssON_hh < 10) {
			lcd.print (F("0"));
			lcd.print (ssON_hh,DEC);
		}
		else{
			lcd.print (ssON_hh);
		}

Wouldn’t it be better to factor it all into a single function?

void printLeadingZero (int x)
{
  if (x < 10)
  {
    lcd.print ('0');
  }
  lcd.print (x);
}

You could even pass in the device, so it would work for Serial or LCD.

Sketch as posted: 1695 bytes RAM
Sketch with F() for Serial: 1523 bytes RAM
Sketch with F() for lcd: 997 bytes RAM*

  • this is attached

form.ino.ino (50.4 KB)

I got it to just over 700 bytes of RAM.
The code could be a lot smaller with some factoring.

You'll save even more RAM if you make the I/O pins "const byte", as they should be.

Many thanks guys,

That does seem to cure the problem, though was clearly not aware of the Strings/Progmem issue.

Could not find an explanation of that F parameter in the lcd.print section though it is mentioned in the Serial.print "You can pass flash-memory based strings to Serial.print() by wrapping them with F()." but think the point about the RAM space is easily missed by the beginer.

Did find this link with gives a fuller explanation.

Thanks also for the heads up about the leading zeros in its own function and I/O pins though expect there are a few other areas I could similarly improve, once I learn a lot more ! :slight_smile:

cheers

with all the tips above, you may want to simplify your code using sprintf()

instead of:

   lcd.setCursor (0,0);
 if(now.hour() <10)
 lcd.print('0');
 lcd.print(now.hour(), DEC);
 lcd.print(":");
 if(now.minute() <10)
 lcd.print('0');
 lcd.print(now.minute(), DEC);
 lcd.print(" ");
 float tst1 = 0;
 lcd.print (tempC, DEC);

try something like:

  lcd.setCursor (0,0);
  char myMessage[12] = "";
  sprintf(myMessage"%02d:%02d %2dC", now.hour(), now.minute(), (int)tempC);
  lcd.print(myMessage);

it uses a bit of overhead but it is easy to read

Using sprintf adds around 1.5k code...

P.S. in the code I posted above, I forgot to change some println("..."),
after changing them I got to 859 bytes .

Whandall:
P.S. in the code I posted above, I forgot to change some println("..."),
after changing them I got to 859 bytes .

Yes, thanks, just been checking it out and noticing your changes and the lcd.write and then seeing the WString.h code when trying out lcd output variations.

learnt a lot !

cheers

BulldogLowell:
with all the tips above, you may want to simplify your code using sprintf()

Of course, you really meant sprint_P :wink: