Really simple array question.......

I have an array that I wish to program an eeprom - here is a bit of it :

  const byte bufferOut[EepromSize] PROGMEM  = {
        96, 2, 165, 148, 235, 106, 150, 135, 1, 162, 192, 200, 63, 34, 214, 15, 19, 239.... etc

however, I get different results with the following two methods of reading this array.... and I can not see what I am missing !

        char tmp[50];
        sprintf(tmp,"%d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d\n\r",bufferOut[0],bufferOut[1],bufferOut[2],bufferOut[3],bufferOut[4],bufferOut[5],bufferOut[6],bufferOut[7],bufferOut[8],bufferOut[9],bufferOut[10]);
        Serial.print(tmp);

        for (int i = 0; i < 10; i++){
          Serial.print(bufferOut[i]);
          Serial.print(", ");
        }
        Serial.println("");

The output is :

96, 2, 165, 148, 235, 106, 150, 135, 1, 162, 192

0, 0, 184, 0, 0, 0, 1, 0, 0, 184

where the first line is correct and the second line is not.

:confused:

That's because the compiler knows the value for the first line and doesn't have to fetch from PROGMEM but in the loop the compiler generates code to fetch from RAM when your data is in PROGMEM. You have to use pgm_read_byte() to get a byte from PROGMEM:

const byte bufferOut[] PROGMEM  =
{
  96, 2, 165, 148, 235, 106, 150, 135, 1, 162, 192, 200, 63, 34, 214, 15, 19, 239
};


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


  char tmp[50];


  sprintf(tmp, "%d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d\n\r",
          bufferOut[0], bufferOut[1], bufferOut[2], bufferOut[3], bufferOut[4],
          bufferOut[5], bufferOut[6], bufferOut[7], bufferOut[8], bufferOut[9], bufferOut[10]);
  Serial.print(tmp);


  for (int i = 0; i < 10; i++)
  {
    Serial.print(pgm_read_byte(&bufferOut[i]));
    Serial.print(", ");
  }
  Serial.println("");
}


void loop() {}

Thanks, that makes sense !