I have a number of character arrays (strings) that I need to store in PROGMEM to save RAM. I can't use F() all the time because I need to pass a pointer to the string and then possibly print it inside a subroutine. I searched the forum and found several threads on the same general subject. From them I put together code which works, but I wonder if it can be made more simple.
For instance the defined PGMT statement uses a "reinterpret_cast" which I have never seen before and it seems clunky.
I have not been able to make the data types nor the cast less wordy. Is there a better way to do it and still have the function able to print the string too?
/*
Tests transfer of progmem "string" to a different function and then print it over the serial port.
*/
#include <avr/pgmspace.h>
#define PGMT( pgm_ptr ) ( reinterpret_cast< const __FlashStringHelper * >( pgm_ptr ) )
/**********************************************************
* While I try to avoid global variables, it
* makes sense to have PROGMEM varialbe be global
* so that all subroutines and modules can use them
*
* Keyword "static" not required for global variables,
* But it is REQUIRED if it is defined in a function/subroutine
**********************************************************/
static const char message_1[] PROGMEM = "THIS IS FLASH";
static const char message_2[] PROGMEM = "THIS IS FLASH ALSO";
void setup() {
static const char message_1[] PROGMEM = "THIS IS FLASH";
Serial.begin(9600);
char message_0[] = "This is RAM";
Serial.println(message_0);
Serial.println(PGMT(message_1));
printprogmem (PGMT(message_2));
}
void loop() {
}
void printprogmem ( const __FlashStringHelper* mess) {
Serial.println (PGMT(mess));
}
-Tony