Getting a memory warning...

Hi :slight_smile:

I'm getting a warning in the compiler window saying I've used 75% of the dynamic memory of the Uno. I'm only halfway of my project so I'm likely to run out of memory soon.

I'm using a lot of LCD related code, is that using a lot of space?

I'm working in two tabs as that keeps it a bit cleaner (for me anyway).

How can I check, besides removing chunks of code, what is eating all the memory?

Thanks!

Dynamic memory (RAM), or program memory (flash)?

Post the code and we'll tell you.

Are you printing string literals (ie, Serial.print("This is a string literal"))? If so, this will waste dynamic memory, because despite being a constant, it still has to get copied to RAM when you use it like that. Use the F() macro - that will print it direct from flash so you don't have to store it in memory. There it much discussion on these forums and elsewhere about the F() macro, as the issue with printing strings is something that people often encounter very early on.

Thanks for the reply:

My complier is Dutch :slight_smile: Is says: 47% flash, 75% dynamic.

The are serial.prints in there for debugging purpose, wasn’t aware those use that much mem.

The code…

ETA: code is too long to post here… I’ll add it to this post as an attachment.

Screens.ino (2.19 KB)

TimeLapse16_001.ino (15.2 KB)

All those places where you do lcd.print(“constant string”), change to lcd.print(F(“constant string”)) - like I noted above.

The way you have it now, each of those strings has to exist in dynamic memory, and that’s where all your RAM is going.

Ok - will do that now and see what that does. Thank you very much already :slight_smile:

ETA:

Wow, went from:

15.444 [47%] flash / 1529 [74%] dynamic

to

15.542 [48%] flash / 1191 [58%] dynamic

My complier is Dutch

No, it isn't. The compiler "speaks" C, C++, Jave, C#, etc. The messages that come from a human language file. THAT file is what is Dutch, for you.

  void Alert01()
  {
      lcd.setCursor (0,0); lcd.print(" opening        ");
      lcd.setCursor (0,1); lcd.print(" settings...    ");
      delay(1500);
  }


// ### ALERT 2
  void Alert02()
  {
      lcd.setCursor (0,0); lcd.print(" leaving        ");
      lcd.setCursor (0,1); lcd.print(" settings...    ");
      delay(1500);
  }


// ### ALERT 3
  void Alert03()
  {
      lcd.setCursor (0,0); lcd.print(" ready to start ");
      lcd.setCursor (0,1); lcd.print("Funcie nog maken");
      delay(1500);
  }

ONE function with two arguments would result in a lot less code.

    case STATUSA: 
      lcd.setCursor (0,0); lcd.print(" 1              ");
      lcd.setCursor (0,1); lcd.print(" ... een dus    "); 
      break;
    case STATUSB: 
      lcd.setCursor (0,0); lcd.print(" 2              ");
      lcd.setCursor (0,1); lcd.print(" ...twee dus    "); 
      break;
    case STATUSC:
      lcd.setCursor (0,0); lcd.print(" 3              ");
      lcd.setCursor (0,1); lcd.print(" ...drie dus    "); 
      break;
    case STATUSD:
      lcd.setCursor (0,0); lcd.print("PRESS BUTTON TO ");
      lcd.setCursor (0,1); lcd.print("OPEN SETTINGS   "); 
      break;

That SAME code gets repeated here. Call that ONE function, instead.

String menu[] = {"4", "1__Rotation", "056Active", "3__Watches", "4__Item4",   // Layer 0
                 "3", "100Rotations", "105Direction", "109Speed",             // Layer 1
                 "3", "256Active", "2__Item2.2", "2__Item2.3",                 // Layer 2
                 "4", "4__Watch 1", "3__Watch 2", "3__Watch 3", "3__Watch 4", // Layer 3
                 "2", "4__Item3.1.1", "4__Item3.1.2"
                };                          // Layer 4

It's useless to piss away resources wrapping strings in String objects, when the wrapped string will never change. Keep the strings in PROGMEM to save even more SRAM.

Bit of a late reply from me, sorry, missed the alert.

I\ve removed most of the alerts already and will put them in a function id I decide I want them back.

Putting the array in PROGMEM seems to be a bit tricky (for me). The menu-part of the sketch is not mine but after reading up about PROGMEMand the massive amounts of memory that can be saved I will surely try to figure that out!

Thanks!