Arduino Uno SRAM

After countless hours trying to debug my 'was working fine with MEGA' code, i have diagnosed the issue to my SRAM. I except that my code (a combination of NTPClient, EthernetClient & various sensor readings that cover all available pins), is maybe too much work for the UNO & will be switching over to the MEGA.

However, during the debug i used free_ram() function below:

int free_ram()
{
extern int __heap_start, *__brkval;
int v;
return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
}


The output, with just the function in the source was around 1800 mark (about right if the UNOs limit is 2k). When i used this function with the full code, it read at -351. i expected 0 (zero) or above...

(in case the reader is interested, the MEGA's free_ram output was 7463)

My question is simple, what is the lowest limit reading of SRAM, using the function detailed?

• Using unsigned integers will possibly help. The code seems to assume that __brkval and __heap_start are after local variables in memory, which may not be true for the Mega. – David Mar 7 '15 at 17:36
• The Arudino Uno and Mega run at exactly the same speed. You will not solve the problem of the processor being too busy by switching. – Austin Mar 7 '15 at 19:59
• @David, the layout of memory segments depends on the compiler. Since the two boards use the same CPU and the same compiler, it will be the same. – Austin Mar 7 '15 at 20:00