Arduino Memory
Arduino has very littel memory, the uno only 2kbyte of ram.
- I had sketch hang/crash without any warning, and it was due to lack of memory(ram)
- check with $avr-size on the compiled elf file, under /tmp/build?????/*.elf
- Solution
- Move debug strings to to flash with PROGMEM or macro PSTR
Nice solution from http://www.utopiamechanicus.com/399/low-memory-serial-print/
#include <avr/pgmspace.h>
int freeRam () {
extern int __heap_start, *__brkval;
int v;
return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
}
void StreamPrint_progmem(Print &out,PGM_P format,...)
{
// program memory version of printf - copy of format string and result share a buffer
// so as to avoid too much memory use
char formatString[128], *ptr;
strncpy_P( formatString, format, sizeof(formatString) ); // copy in from program mem
// null terminate - leave last char since we might need it in worst case for result's \0
formatString[ sizeof(formatString)-2 ]='\0';
ptr=&formatString[ strlen(formatString)+1 ]; // our result buffer...
va_list args;
va_start (args,format);
vsnprintf(ptr, sizeof(formatString)-1-strlen(formatString), formatString, args );
va_end (args);
formatString[ sizeof(formatString)-1 ]='\0';
out.print(ptr);
}
#define Serialprint(format, ...) StreamPrint_progmem(Serial,PSTR(format),##__VA_ARGS__)
#define Streamprint(stream,format, ...) StreamPrint_progmem(stream,PSTR(format),##__VA_ARGS__)
void setup()
{
Serial.begin(9600);
Serial.print("12345678901234567890123456789012345678901234567890");
Serialprint("");
Serialprint(" memory available: %d bytes\n",freeRam());
}
void loop()
{
}
...
