How to print data from Program memeory

Hello all!

I want to store the AT commands in program space to save memory using the "pgmspace.h) but I am not able to print them, in fact I do not know if they are stored into the flash.

The code I am using is the following:

#include <avr/pgmspace.h>

const char CREG[] PROGMEM = {0X41,0X54,0X2B,0X43,0X52,0X45,0X47,0X3F,0X0D,0X0A};

char mychr=0;
uint8_t k =0;

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
}


void loop() {
  // put your main code here, to run repeatedly:
 /* 
  for (k = 0; k < strlen(CREG); k++)
   {mychr = pgm_read_byte_near(CREG + k);
   Serial.println(mychr);
   }
}

Your help is appreciated.
Regards,
Manuel

k < strlen(CREG)

strlen is for strings in ROM.

Try strlen_P instead.

Make your strings more readable?

const char CREG[] PROGMEM = {"AT+CREG?\r\n\0"};

and maybe (untested)

byte counter = 0;
char myChar = 0;
do {
  myChar =  pgm_read_byte_near(CREG + counter++); 
  if (myChar == 0) break;
  Serial.print(mychar);
}

Delta_g ,

Thanks for the help. It prints now but random numbers.

Riva,

Thanks its working and you are right the code its much more readable. I was following some examples in the explanation in the Arduino PROGRMEM tutorial, but, it was not working.

Regards, Manuel