Problem with Reading Values from an Array Stored in ROM

Hi,

I just came across a problem when reading values from an array which was stored in ROM by "PROGMEM" approach. Although all the values stored are "0", it was read as "65535" and other values. (Please see the screenshot below)

This problem did not come up when I deleted "PROGMEM", using RAM to store the array. Could anyone tell me what is going on with it and how should I handle that?

This is the way how I defined the array:
(I tried int, long, char, but it didn't work)

const long C_YX [2][84] PROGMEM  = {
  {0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
};

This is the way how I read the values:

  int X_INDEX; int Y_INDEX;
  for (Y_INDEX=0; Y_INDEX<2; Y_INDEX++) {
    for (X_INDEX=0; X_INDEX<84; X_INDEX++) {      
        Serial.print (X_INDEX);
        Serial.print (",");
        Serial.println (Y_INDEX);
        Serial.println (C_YX[Y_INDEX][X_INDEX]);
        Serial.println ("-----------");
        delay(1000);      
    }
  }

Serial.println (C_YX[Y_INDEX][X_INDEX]);

That's how you read from RAM.

Look at the examples here: PROGMEM - Arduino Reference

You need some special functions to pull things out of PROGMEM.

Delta_G:

Serial.println (C_YX[Y_INDEX][X_INDEX]);

That’s how you read from RAM.

Look at the examples here: PROGMEM - Arduino Reference

You need some special functions to pull things out of PROGMEM.

Thanks! I tried to use “pgm_read_byte_near()” to read and defined the array as a byte type.

const byte C_YX [2][84] PROGMEM = {}
Serial.println (pgm_read_byte_near(C_YX[Y_INDEX][X_INDEX]));

However, the problem still exists. The value jumps among “0”, “12”, “148” and “177”, but all of them stored are “0”.

You didn't do it right. Pay attention to the example.

    displayInt = pgm_read_word_near(charSet + k);

Note how they passed a pointer instead of trying to dereference the array in the function call.

You are reading values out of the array as if it were in RAM and passing them as the address to read from PROGMEM. You want the address in PROGMEM:

Serial.println (pgm_read_byte_near(&C_YX[Y_INDEX][X_INDEX]));

Note the '&' ('address_of') operator.

johnwasser:
You are reading values out of the array as if it were in RAM and passing them as the address to read from PROGMEM. You want the address in PROGMEM:

Serial.println (pgm_read_byte_near(&C_YX[Y_INDEX][X_INDEX]));

Note the '&' ('address_of') operator.

It works in this way, thank you!