c++arraysstringarduinoprogmem

Concatenating strings in PROGMEM for arduino UNO


I am trying to create a list of wavefile names soundList to play through an adafruit WAV shield ontop an arduino UNO. Since these file names are never going to change, and I am running out of sRam, I'd like to store them in PROGMEM but I get an error:

invalid operands of types 'const char*' and 'const char [5]' 
to binary 'operator+'

Am I not able to concatenate strings in PROGMEM? I don't understand why.

for (int i = 1; i < 19; ++i)
  {
    const char soundList[i] PROGMEM = "Track" + i + ".WAV";
    return soundList;
  }

Also I am having a hard time then reading the wavefiles out of soundList?

pgm_read_word(&(soundList[i])));

any words of wisdom?


Solution

  • A for loop is a runtime operation. You cannot perform runtime operations that assign to a PROGMEM variable since the variable will be stored in (read-only at runtime) flash.

    But since the only variable part is the number, store the other two parts in flash and create the filename at runtime.

    #define FILEPREFIXLEN 5
    char fileprefix[] PROGMEM = "Track";
    #define FILESUFFIXLEN 4
    char filesuffix[] PROGMEM = ".WAV";
    #define FILEMIDLEN 2
    
     ...
    
    char filename[FILEPREFIXLEN + FILEMIDLEN + FILESUFFIXLEN + 1];
    // Read fileprefix into filename, append number, append filesuffix