PROGMEM

Using Arduino 1.6.4
How do you get a string to NOT store in SRAM?
Looking at other posts here , I've tried this and lots of other things to NO avail:

const char gCode_ch0[] PROGMEM ="hello there xxxxxxxx";
const char * const gCode PROGMEM = {gCode_ch0};

OR

const char gCode[] = "xxxxxx";

Increasing the string in either case and recompiling uses more SRAM...

SteveK2216:
Using Arduino 1.6.4
How do you get a string to NOT store in SRAM?
Looking at other posts here , I've tried this and lots of other things to NO avail:

const char gCode_ch0[] PROGMEM ="hello there xxxxxxxx";
const char * const gCode PROGMEM = {gCode_ch0};

OR

const char gCode[] = "xxxxxx";

Increasing the string in either case and recompiling uses more SRAM...

Try this:

void setup (void)
{
    const char *string = PSTR ("Hello there, I reside in PROGMEM.");
    while (!Serial);
    Serial.begin (115200);
    Serial.println (string); // prints gibberish as expected
    Serial.println_P (string); // prints properly
}

void loop (void)
{
}

Oops I forgot that standard Arduino IDE doesn't have print_P (I added that myself).

If you want to do that, find "Print.cpp" and "Print.h" and add this code to it:

Print.cpp

// print_P, unsigned
size_t Print::print_P (const unsigned char *str)
{
	size_t n = 0;
	unsigned char c;

	while (c = pgm_read_byte (str + n++)) {
		write (c);
	}

	return n;
}

// println_P, unsigned
size_t Print::println_P (const unsigned char *str)
{
	size_t n = print_P (str);
	return (n + println());
}

// print_P, signed
size_t Print::print_P (const char *str)
{
	size_t n = 0;
	char c;

	while (c = pgm_read_byte (str + n++)) {
		write (c);
	}

	return n;
}

// println_P, signed
size_t Print::println_P (const char *str)
{
	size_t n = print_P (str);
	return (n + println());
}

Print.h

	size_t print_P (const unsigned char *);
	size_t println_P (const unsigned char *);
	size_t print_P (const char *);
	size_t println_P (const char *);

Why the Arduino code doesn't have this is beyond me.......

(edit to add): My modified Print.cpp and Print.h are included in full in the attached ZIP file.

print.zip (3.49 KB)

OK, thanks, but I really wasn't trying to print.

Looks like a char array declared with PROGMEM must be declared globally (not in any function) in order to work.
At least, that's what worked for me:

...

char buffer[10];
const char gCode[] PROGMEM = "ABCDEFGHIJKLxxxxxxxxxzzzzzzzzzzzzzzzsldk";

char* gCodeMgr(int i) {

buffer[0] = pgm_read_byte(&gCode*);*

  • return &buffer[0];*
    }
    ...

Krupski:
Oops I forgot that standard Arduino IDE doesn't have print_P (I added that myself).

No need to modify the core files.

Create a define for ease of use, but the features are already there:

#define FlashString(x) ((const __FlashStringHelper*)x)

void setup (void){
  
    const char *string = PSTR ("Hello there, I reside in PROGMEM.");
    while(!Serial);
    Serial.begin(115200);
    Serial.println( FlashString( string ) );
}

void loop (void){
}