Can I store a string literal that would be called in a function in PROGMEM

So basically, I'm running out of SRAM and am starting to get unknown behaviour and was wondering I had a function like so:

void function(String str){
   //do some stuff
}

and the "str" variable is constant could I store it in eeprom to save memory or declare it as a constant or would this have no effect on the amount of SRAM used. I am asking because I call the same string literal in several functions.
Finally how would I go about storing it in eeprom a lot of the examples are a bit confusing

Many thanks for any help.

Why use Strings (uppercase S) and not C-strings (lowercase s) ?

There is a difference

You can do this :

const char PROGMEM test[] = {"This is a test"};
char buffer[20];

void setup()
{
  Serial.begin(115200);
  while (!Serial);
  int len = strlen_P(test);
  for (int k = 0; k < len; k++)
  {
    char myChar =  pgm_read_byte_near(test + k);
    Serial.print(myChar);
  }
}

void loop()
{
}
1 Like

To save SRAM, for Serial.prints be sure to use the "F" macro (also stores the text in program memory), e.g.

Serial.println(F("Starting up!"));
1 Like

Thanks, that what I'm using now and memory seems good so far! :slight_smile:

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