Hi,
I have been dealing with some really really weird issues lately. In the end I concluded that they were caused by memory issues (not enough). But I don't really understand why... Can anyone help shine light on these things?
Background:
The project I am currently working on is using a GPS module and a small OLED display. If I run a test program (without changing any wires) that only interacts with the GPS module (write info to the serial monitor instead of OLED) it works on both an UNO and an MEGA. If I run another test program (again without changing any wires) that only interacts with the OLED it runs fine on both an UNO and an MEGA. If I then run the main project that interacts with the OLED and the GPS it only works on a MEGA... I tried three UNO's and I get the same result. I know that the OLED is memory hungry so that is why I suspect the problem is related to the limited memory on an UNO. [FYI: The GPS module is connected to pins 10/11 and the OLED is connected to SDA/SCL. In case of the UNO I use the SDA/SCL on A4/A5 and on the MEGA I use the SDA/SCL on pins 20/21].
But if I look at the IDE info I get when I upload the program to the UNO and MEGA it is not clear that there is an issue:
UNO: Sketch uses 20362 bytes (63%) of program storage space. Maximum is 32256 bytes. Global variables use 772 bytes (37%) of dynamic memory, leaving 1276 bytes for local variables. Maximum is 2048 bytes.
MEGA: Sketch uses 20852 bytes (8%) of program storage space. Maximum is 253952 bytes. Global variables use 772 bytes (9%) of dynamic memory, leaving 7420 bytes for local variables. Maximum is 8192 bytes.
So, what is going on here? I am uploading the exact same sketch to both the UNO and the MEGA. Thank you!
Good points. I am less interested in what actually is going on in this particular script than how to predict problems. I'd like to understand that. But the point made here that local variables are not counted is a good one. That was not clear to me (but it seems obvious now as it says global variables in the message).
My follow up question then is this; for future releases, would it be possible to have a feature that examines a sketch to estimate its memory usage prior to run-time? Is this even possible (IDE feature)? The reason I am asking is that in cases where this happens, you go crazy chasing the problem as nothing seems wrong on the surface...
As for specifics to my sketch mentioned here, yes I am using the String class... My bad. I did not know it was a such a bad idea to use in terms of memory but I will re-think those portions knowing that you brought it up! Thank you!
I just want to report back that by not using the controversial (but convenient) String class, as suggested here, fixed my problem. The sketch now works on an Uno too! Thanks guys!!
I don't think it will be very easy to count memory usage of local variables. It will require a full analysis of the code including when functions are called. Not to forget that interrupts that are not predictable will also require memory.
Both just require memory for being called without even taking into account what happens inside them.
sterretje:
I don't think it will be very easy to count memory usage of local variables. It will require a full analysis of the code including when functions are called. Not to forget that interrupts that are not predictable will also require memory.
Both just require memory for being called without even taking into account what happens inside them.
Yes, that might be tricky. Then there is the use of libraries which I tend to use as a black-box. They might use local variables for various tasks and that can then cause problems. It seems this memory business is a bit tricky when it is very limited (Uno). In my case, I was lucky to fix the problem by not using the silly String class. An old habit from programming in environments where memory is not as limited as it is in an Uno. Good advice you gave there. Thank you!!