Arduino stringy business

Ever ran out of RAM in your Arduino projects? Me neither. But i was close. Especially easy if you’ve got some sort of wireless or networking baked in…

Anyways, ran across a useful tutorial/tip by tempmj from Dangerous Prototypes forums on saving RAM space with strings here. The idea is to store the strings in flash, which seems like an obvious solution, but here’s the clever bit – you read the strings from flash to RAM within a function, so the memory used by the string gets dealloc’d after the function’s done with whatever it’s doing (i.e. writing to serial).

The function itself gets called with PSTR, an AVR macro for declaring pointers to static strings in program space (i.e. flash). The whole thing looks something like this (direct copy/pasta from the original article):

void showString (PGM_P s) {
char c;
while ((c = pgm_read_byte(s++)) != 0)

showString(PSTR(“String goes in here”));

Oh, and although i’ve written “Arduino” up there, it’s actually a generic AVR thing, as the library used is from AVR libc.


Tags: , , , , , , , , , , , ,

No comments yet.

Leave a Reply