PROGMEM not storing in flash

I need to get these strings out of dynamic memory and into flash. When I use "PROGMEM" as in the code below, it only saves about 20 bytes of dynamic memory.

const char *const alarm_msgs[] PROGMEM = {
    "Sump Pump Failed    ", // 0
    "Shop Floor Wet      ",
    "Studio Bath Wet     ",
    "Sump is Full        ",
    "Sump is High        ",	
    "Sump Sensor Error   ", // 5
    "Shop Well Tank LOW  ",
    "Shop Rain Tank LOW  ",
    "House Rain Tank LOW ", 	 
    "House Well Tank LOW ", 
    "Utility Floor Wet   ", // 10 
    "Shop Below 35       ",
    "Studio Below 60     ",
    "Paint Room Below 60 ",
    "Main: no flow       ",  
    "Main: flow, no heat ", // 15
    "Bsmt: no flow       ", 
    "Bsmt: flow, no heat ", 
    "Shop: no flow       ",  
    "Shop: flow, no heat ",
    "Vent needs wndow opn", // 20
    "Vent needs door open"
} ;

If I comment out the entire set of strings and replace it with

const char* alarm_msgs[] = " now " ;

It saves 440 bytes of dynamic memory. The only idea I have is that I have to use "PROGMEM" on each line, or build the array as 21 separate statements, all with "PROGMEM" in each one, something like this?

const char* alarm_msgs[0] PROGMEM = " first line " ;
const char* alarm_msgs[1] PROGMEM = " second line " ;
etc.

The progmem documentation shows you how to do it. You declare the individual strings first and then point the array elements at them. It's a bit messy.

Too much trouble to read the reference huh?

Are they fixed length ? Then you can put an array in PROGMEM, and you don't have to use the separate strings with a list of pointers.

// 20 characters plus zero-terminator makes 21
const char alarm_msgs[][21] PROGMEM =
{
  "Sump Pump Failed    ",
  "Shop Floor Wet      ",
  "Studio Bath Wet     ",
  "Sump is Full        ",
};

Koepel, thanks.

Yes, they are constant length. The IDE was throwing an error
(too many initializers for 'const char []')
because I didn't have the second array bounds specified. I thought there might be a way to build the array without having to do individual declarations.

BTW, I see you have a comma at the end of the last line. My code seems OK without it, am I missing something?

AWOL, funny you should post that link. That page is already open in my browser.

Here is some code for a posting with a similar problem

// with PROGMEM
//Sketch uses 3780 bytes (11%) of program storage space. Maximum is 32256 bytes.
//Global variables use 202 bytes (9%) of dynamic memory, leaving 1846 bytes for local variables. Maximum is 2048 bytes.
// without PROGMEM
//Sketch uses 3778 bytes (11%) of program storage space. Maximum is 32256 bytes.
//Global variables use 696 bytes (33%) of dynamic memory, leaving 1352 bytes for local variables. Maximum is 2048 bytes.

const char Menu[15][3][11] PROGMEM  =
{ {"Set Clocks", "", ""},
  {"", "Clk0", ""},
  {"", "Clk1", ""},
  {"", "Clk2", ""},
  {"", "Save", ""},
  {"Set Sweep", "", ""},
  {"", "Start freq", ""},
  {"", "Stop freq", ""},
  {"", "Step size", ""},
  {"", "Sweep", ""},
  {"", "", "Single"},
  {"", "", "Continuous"},
  {"", "Time (ms)", ""},
  {"", "Save", ""},
  {"Reset", "", ""},
};

void setup() {
  Serial.begin(115200);
  for (int i = 10; i > 0; i--) {
    Serial.print(' '); Serial.print(i);
    delay(500);
  }
  Serial.println();
  for (size_t i = 0; i < 15; i++) {
    String element0(reinterpret_cast<const __FlashStringHelper *>(Menu[i][0]));
    String element1(reinterpret_cast<const __FlashStringHelper *>(Menu[i][1]));
    String element2(reinterpret_cast<const __FlashStringHelper *>(Menu[i][2]));
    Serial.print(element0); Serial.print(" , "); Serial.print(element1); Serial.print(" , "); Serial.print(element2); Serial.println();
  }
}


void loop() {
}

Dr_Quark:
BTW, I see you have a comma at the end of the last line. My code seems OK without it, am I missing something?

You're not missing anything. Some of us are in the habit of doing so. I do it so I will not forget the comma if I expand with additional entries.

drmpf:
Here is some code for a posting with a similar problem

There's a version of that snippet that does not foolishly waste 1344 bytes of Flash and 10 bytes of SRAM.

Dr_Quark:
BTW, I see you have a comma at the end of the last line. My code seems OK without it, am I missing something?

When filling an array with data, I sometimes keep that extra comma at the end, for example when I want to add more data. The compiler does not seem to mind.

I believe that the extra comma is part of the syntax and is optional.

Dr_Quark:
AWOL, funny you should post that link. That page is already open in my browser.

And you hadn't read it?

AWOL :grin: Only if you think memorizing doesn't also require reading.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.