Read data from flash

Hi,

I am trying to send a large text over serial, so in order to make the code 'cleaner' I thought to save the text in a .h file at the same directory as my code, as it follows:

const char APC[] PROGMEM = R"=====(

APC Series Transparent Transceiver Module APC220-43

)=====";

The ideia is to save the text into flash memory. Here's my code:

#include "APC.h"

const String tx = APC;

void setup() {
  pinMode(botao,INPUT);
  Serial.begin(9600,SERIAL_8N1);
  Serial.println(tx);
}

void loop() {  


  
}

The problem is that when serial is only reading garbage. I thought of using F() macro, but this way the whole text would have to be at the code. Any ideas on how to solve that?

You can’t store Strings in PROGMEM.

The PROGMEM reference page explains how to read data from PROGMEM.

In that case, I would do something like this:

Serial.println((__FlashStringHelper)APC);

This is the only way you can "notice" a print function that a string resides in flash and not in RAM.

Any declared array creates a memory pointer, and pointers are always targeted towards RAM (PROGMEM or not) unless they are reused as flash pointers by some special functions (e.g. pgm_read_byte()).
Apparently all String constructors take data from RAM (hence the "garbage"), thus it will never work for flash-stored strings.

On the other hand, print functions do have a version that handles flash-stored strings, which receives the result of the well-known F() macro (of type __FlashStringHelper, hence the explicit casting that otherwise would end up in the original problem anyways).