Array stored in PROGMEM gets corrupted on read

Hello there! I've recently gotten into using the Mega 2560 and am a big fan of all the program memory available, so I wanted to store some images on it to display on a 240x240 GC9A01 TFT screen. When I stored the array in dynamic memory, it displayed just fine, but when I stored it in PROGMEM and read the RGB565 values (Of type uint16_t) it was all messed up, I managed to fix some of it by using pgm_read_byte_near(&image[y][x]), but now it's all bluescale, any idea what I could be doing wrong? Here's an image of the screen and my code.

const uint16_t* const image [73][29] PROGMEM // Imagedata on pastebin: https://pastebin.com/PdvAKR3E

void displayMocktail()
{
  for (uint8_t y = 0; y < 73; y++)
  {
    for (uint8_t x = 0; x < 29; x++)
    {
      if (pgm_read_byte_near(&image[y][x]) != 0xFFFF)
      {
        mylcd.Fill_Rect(x*2 + 91,y*2 + 47,2,2,pgm_read_byte_near(&image[y][x]));
      }
    }
  }
}

void setup() {
  mylcd.Init_LCD();
  mylcd.Fill_Screen(BLACK);

  displayMocktail();
}

How many bits does this function read? How many bits per pixel is your image?

It's in RGB565 form so that would look like 0xFFFF, which I guess is 6 bits? The variable type is an unsigned int16 though. Sorry I'm still getting used to C++!

Maybe this gives a hint:

C:\Users\bugge\AppData\Local\Temp\.arduinoIDE-unsaved2025226-1292-25vxoo.yfvpz\sketch_mar26a\sketch_mar26a.ino:13:44: warning: comparison is always true due to limited range of data type [-Wtype-limits]
       if (pgm_read_byte_near(&image[y][x]) != 0xFFFF)

pgm_read_byte_near() returns a byte, not an unsigned int. So you only read one byte from the array.

Ohhh that makes so much sense! I fixed it using pgm_read_word_near() instead! Thank you so much for the quick reply and have a great day!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.