Could I save more SRAM ?

Hi !

(Context in the following lines. Faster version in the bottom of this post)

I’m using an Arduino Uno R3 on a self-made PCB with a GSM Click, a Bluetooth module and a few sensors. IDE version is 1.5.6-r2. Actually Im working on the GSM module, I just finished to write the code and was able to send a SMS, fine.

But I saw many memory issues when testing it. If I just run my setup and call my function to send a SMS everything goes right, but if I make a few other Serial.print() for report purposes, or call some other functions, nothing works correctly (random issues, probably the SRAM is full. By exemple jumping here and there in my code, having unknown values in my variables, etc).

So here’s my question : What could I do to avoid those problems ? Because if my functions are working fine today, Im sure they won’t work when we’ll put all part of our application together (a main intelligence getting sensors values, interacting with bluetooth and GSM, etc). I already made a big change, I used to work with StringObject and changed for char* after reading a lot about this. But I don’t think I understood everything about memory, that’s why I ask here.

Note that I don’t expect anybody to look at my whole code searching for optimisations, but if you could have a look and tell me what are my bad habits (because if you see a mistake in the way I use memory at a place, it will probably be at a lot of other places too), that would be very kind.

My 3 files are attached because far too big to be placed in this message

Summary : My application works but has memory issues when I add a few lines of code, which will certainly be a problem when I’ll have to add other parts of my project. What am I doing wrong with my SRAM ? What are the bad things you can see just by having a fast look at my code ?

Note that when I compile, the IDE tells me I use 7442o (23%) of the ROM and 651o (31%) of the RAM.

Thanks in advance !

PS: I have already read this article : Optimizing SRAM | Memories of an Arduino | Adafruit Learning System
But I don’t use Strings anymore, and I could not use the whole PROGMEM thing with the char chains in the Serial communication, maybe I did that wrong, maybe this is just not possible. I only use one or two global variables and the size of my buffer corresponds with what I can receive…

GsmUse.ino (625 Bytes)

GSMClick.h (4.52 KB)

GSMClick.cpp (12.1 KB)

One of the easiest ways to overrun SRAM is this:

  Serial.print("Try SMS : ");

That string literal is copied from flash to SRAM at run time. You can stop that with the F() macro:
Serial.print(F("Try SMS : "));

Thanks for the answer !

Is there any difference between your solution and the one we find on some other pages, which has a function like this :

printPGM(PGM_P s)
 char c;
 while((c = pgm_read_byte(s++)) != 0)

(Not any code snippet, just some example I found on the web, using avr/pgmspace.h)

Are thoses two solutions doing the same thing ? If so, I guess your version is more modern, because it is really lighter, is that right ?

Thank you for your precious help

It's a different way to skin the same cat.

In addition to moving your strings into PROGMEM, where feasible, you might look at using smaller types. There are a few places you are using int (2 bytes) where a uint8_t or int8_t (byte) type would do.