Outsource const PROGMEN to separate

I have a lot of const PROGMEM char* variablename = “ABC”;
and like to put that in a separte file.
so I created a static.h and a static.cpp
I did #include <pgmspace.h>in the cpp file.
But different versions produces errors.
can anybody give me an example
for the .h and the .ccp file

Thanks

I suspect that in your static.h file you have defined the variables as PROGMEM but you didn’t #include <avr/pgmspace.h> in the header file? Note, it’s avr/pgmspace.h by the way.

Here’s a quick (Arduino framework) sketch.

First, static.h:

#ifndef STATIC_H
#define STATIC_H


#include <avr/pgmspace.h>

const PROGMEM char abc[]  = {"ABC"};
const PROGMEM uint16_t myInt  = {12345};
const PROGMEM uint16_t intArray[] = {34567, 666, 8192, 4325};

#endif // STATIC_H

Then main.cpp:

#include "Arduino.h"
#include "static.h"

void setup() {
  Serial.begin(9600);

  // Read one unsigned 16 bit int from PROGMEM
  // into a local uint16_t and print it to Serial.
  // Because PROGMEM expects pointers, we need the 
  // address of myInt, not myInt itself. beware!
  uint16_t showMyInt;
  showMyInt = pgm_read_word_near(&myInt);
  Serial.println(showMyInt);
  Serial.println();

// Read 'n' uint16_t variables from an int array. The array is
// already a pointer, so no need for the address of the array.
for (uint8_t x = 0; x < (sizeof(intArray)/sizeof(uint16_t)); x++) {
    showMyInt = pgm_read_word_near(intArray + x);
    Serial.println(showMyInt);
  }
  Serial.println();

  // Read 'n' bytes from PROGMEM and send to Serial.
  char myAbc;
  for (uint8_t x = 0; x < strlen(abc); x++) {
    myAbc = pgm_read_byte_near(abc + x);
    Serial.print(myAbc);
  }
  Serial.println();
}

void loop() {
}

The output is:

12345

34567
666
8192
4325

ABC

HTH

Cheers,
Norm.

Thank you really great
Rainer

1 Like