Arduino Nano ATMega328P running out of SRAM ?

Hi together,

i’m Thomas, 38 years old and started with Arduino approx two years ago because i was impressed by neopixels and wanted them to drive my own pattern.
A few months ago i rebuilded a child MP3 Player found here:

My Version works, but is not very stable.
After pressing more than two Folder buttons it hungs up.
I reduced some variables so that the code compiles with 70% usage of SRAM and than is works better.
As far as i investigated it seems to be that the heap grows into Stack.
Do you have any idea how to optimise heap size?
Here is my used code:
(not enough space, so i attached it)
Would be very great if someone would have a clue for me.
Parallel i try tu build up my own board with a ATMega1284P but this could last a very long time.

Thanks a lot.


Code.txt (14.7 KB)

If your heap is growing into your stack you've got a memory leak somewhere, which is not an area with which I'm completely comfortable.

This little chunk of code however, I've found to be useful. Just add it to your code and call it at various places to see what is happening with your memory. It may lead you to the problem area.


//=======1=========2=========3=========4=========5=========6        freeRam
int freeRam()
    extern int __heap_start, *__brkval;
    int v;
    return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);


It may help if you move your declarations for File actualDirPointer, actualFilePointer and toCompare outside of loop().

This is a pure guess... File looks like a class so having 'em instantiated once rather than every time around loop() might make a difference.


I saw all those labels and gotos and threw my hands up and am walking away.

When you comprehend that loop() loops, and let it manage what to do on any given pass through loop, and completely rewrite your code, your problem may go away, or it may not. If it doesn’t, at least it will be easier to find.