Problem Reading PROGMEM

Hi Everyone,

I have a really long array of 1800 Bytes and I want to send it over serial to another arduino. Because of the length of the array I am storing it in program memory using PROGMEM instead of SRAM.

The problem I'm having is the following: If I store the array in SRAM and send it over serial at 9800 baud, it works, I can see all the bytes arrive well on the other side, but if i store the array in program memory, the data gets "corrupted" and different, random bytes arrive.

I tried using a delay and decreasing the baud rate just in case, but the problem persists.

Here is my code:

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

  for(int n=0; n < (IMG_HEIGHT*IMG_BWIDTH); n++)
  {

  Serial.print(img_data[n]);
  Serial.print("\n");

  }

}

If someone is able to help, I'd apreciate it, thanks!

If someone is able to help, I'd apreciate it, thanks!

If you are really storing the data in PROGMEM, and not allowing it to be copied to SRAM at run time, you have to use the appropriate methods to access the data, NOT the ones that would fetch the data from SRAM.

Serial.print doesn't know your data is in PROGMEM, which is a separate address space.
The simplest thing would be to read the data first and then pass it to Serial.print.

AWOL:
The simplest thing would be to read the data first and then pass it to Serial.print.

Hmm... How do I procede? I tried storing the values in a temporary variable before passing it to Serial.print but id didn't work.

Maybe if you showed us your attempt, we could help you.
We don't even know what the type of "img_data" is, so specific help is difficult.

AWOL:
Maybe if you showed us your attempt, we could help you.

Sorry about that, here's the full code:

#define IMG_BWIDTH 30
#define IMG_HEIGHT 60

const unsigned char img_data[IMG_HEIGHT*IMG_BWIDTH] PROGMEM={
    0,  7,255,192,  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, 63,252,  6, 64,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,192,  0,  0,  0,  0,  0,
    2,255,112, 96,129,  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,
   15,222,  2,  4,  8,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  3,  0,  0,  0,  0,  0,  0,
  255,240,  1,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  1,  0,  0,  0,  0,  0,  0,
  252, 65,144, 32,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 12,  0,  0,  0,  0,  0,  0,
  248,  8,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,144,  0,  0,  0,  0,  0,  0,
   65,132,  9,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 64,  0,  0,  0,  0,  0,  0,
    0, 16, 16,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  3,  0,  0,  0,  0,  0,  0,  0,
   36,128,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 48,  0,  0,  0,  0,  0,  0,  0,
   65,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 64,  0,  0,  0,  0,  0,  0,  0,
    8,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  6,  0,  0,  0,  0,  0,  0,  0,  0,
   16,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,192,  0,  0,  0,  0,  0,  0,  0,  0,
   32,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  2,  0,  0,  0, 49,  0,  0,  0,  0,  0,  0,  0,  0,  0,
    4,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,128,130,168,160,  0, 19,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 34,138, 42, 32,  0,128,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
   16,  0,  0,  0,  0,  0,  0,  0,  0, 34, 34, 34,136, 32,130,  0,  0, 32,  0,  0,  0,  1, 69, 64, 80,  0, 20,  5,  4,  0,
   32,  0,  0,  0,  0,  0,  0,  8,170,160,  0,  0,170,170, 40, 42,170,136,  0,  0,  0,  0, 64, 17,  4, 68, 64, 16, 68,  0,
    0,  0,  0,  0,  0,  0,  0,162, 10,  2,170,170,170,170,168, 32,  0, 34,  0,  0,  0,  0, 65, 65,  4, 16, 84, 16, 68,  0,
   64,  0,  0,  0,  0,  0, 10,130,171,239,255,238,170,170,160,162,170,162,  0,  0,  0,  0, 68,  1,  4, 16, 65, 16, 64,  0,
   36,  0,  0,  0,  0,  0,  8, 42,187,250,170,170,187,238,250,  0,  0, 34,  0,  0,  0,  0, 69, 80, 80, 68, 20,  5,  4,  0,
    0,  0,  0,  0,  0,  0, 34,170,255,234,170,170,171,187,178, 60,  0,170,160,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  152,  0,  0,  0,  0,  0,  0,171,254,170,  0,  2,186,255,248, 61,  3,  2, 40,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
    1,196,  0,  0,  0,  0, 10,187,254,168,112,  4,171,191,248, 54,  0, 40,136,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
    0,  8,198, 96,  0,  0, 42,191,250,163,240, 14,174,255,254,136,170,138, 34,128,  1,128,  0,  0,  0,  0,  0,  0,  0,  0,
    0,  0,  0,  0,  0,  0,139,191,250,168, 53, 26,171,255,252,138, 34,186,162,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
    0,  0,  0,  0,  0,  0, 42,251,250,170,170,170,174,191,254, 34,187,174,232,128,  0, 64,  0,  0,  0,  0,  0,  0,  0,  0,
    0,  0,  0,  0,  0,  2,170,174,255,186,170,170,187,255,255, 34,234,251,170,128,  0,128,  0,  0,  0,  0,  0,  0,  0,  0,
    0,  0,  0,  1,  0,  0,138,187,187,171,170,238,187,191,255,138,175,255,238, 32,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
    0,  0,  0,  0,  0,  0, 10,239,255,186,186,170,238,239,255,224,235,255,250, 32,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
    0,  0,  0,  0,  0,  0, 46,175,255,255,238,186,190,255,255,248,171,255,250,128,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
    0,  0,  0,  0,  0,  0,170,191,255,255,250,171,187,239,255,254, 42,255,238, 32,  0,144,  0,  0,  0,  0,  0,  0,  0,  0,
    0,  0,  0,  0,  0,  0, 42,238,255,255,251,170,174,187,255,254,174,190,234,128,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
    0,  0,  1,128,  0,  0,174,187,255,255,254,235,171,255,255,254, 10,171,170,128,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
    0,  0, 32,  0,  0,  0,138,174,255,255,255,186,163,255,255,255,162,186,186,128,  0,  4, 36,  0,  0,  0,  0,  0,  0,  0,
    0,  4,  0,  0,  0,  0, 43,187,239,255,254,170,234,170,174,234, 34, 42,168,  0,  0,  8,  2,  0,  0,  0,  0,  0,  0,  0,
    0,  0,  0,  0,  0,  0, 10,174,186,255,251,171,168,128, 10,128,  2, 34,226,  0,  0,  0,  0, 64,  0,  0,  0,  0,  0,  0,
    0,  0, 64,  0,  0,  0,  2,171,239,187,254,234,170, 40,  0,  0,  0, 40, 32,  0,  0,  2,170,168,  0,  0,  0,  0,  0,  0,
    0,  0, 40,  0,  8,  0,  0, 42,190,239,190,186,128,  0,  0,  0,  0,  0,160,  0,  0, 64,170,170,160,  0,  0,  0,  0,  0,
    0,  2,160,  0, 60,  0,  0, 42,171,190,250,168,  0,  0, 10,170,168,  0,  0,  0,  0,128, 16, 10,168,  0,  0,  0,  0,  0,
    0, 42,128,  2,240,  0,  0,130,234,186,170,160,  0,170,170,170,174,128,  0,  0,  0,  6,  0,170,170,128,  0,  0,  0,  0,
    0,170,128, 47,  0,  0,  0, 10, 42,170,170,128, 42,170,175,255,250, 32,  0,  0,  1,  0, 42,170,170,168,  0,  0,  0,  0,
   10,170, 64, 40, 48,  0,  0,  0, 40,162,170,  0,171,255,255,255,234,136,  0,  0,  2,  0,  2,170,170,170,  0,  0,  0,  0,
  170,170,  0,160,176,  0,  0,  0,  0,130,168,  2,191,186,175,250,170, 32,  0,  0,  1, 56,  0,170,170,170,  0,  0,  0,  0,
  170,170,130,130, 64,  0,  0,  0,  2,  8,168, 10,239,255,234,170,170,136,  0,  0, 16, 60,128,170,170,170,  0,  0,  0,  0,
  170,170,128,138,  0, 60,  0,  0,  0,  8, 32,  2,255,255,255,234,186,128,  0,  0,  0,  7,232, 42,170,170,128,  0,  0,  0,
  170,170,160,168,  0,252,  0,  0,  0,  0,  0, 10,175,255,255,251,170,128,  0,  0,  0,  0, 26, 42,170,170,128,  0,  0,  0,
  170,170,168,  8,  0,240, 32,  0,  0,  0,  0,  8,190,187,235,238,168, 32,  0,  0,  0,  0,  4,170,170,170,160,  0,  0,  0,
  170,170,160,170,  0,240,192,  0,  0,  0,  0,  0,170,254,238,170,186,  0,  0,  0,  0,  0, 72, 42,170,170,168,  0,  0,  0,
  170,168,  0,170,128,  2, 64,  0,  0,  0,  0,  0,  2,171,170, 58,160,  0,  0,  0,  0,  0,  2, 10,170,170,170,128,  0,  0,
  170,170, 42,170,168,  3,  0,  0,  0,  0,  0,  0,  2, 10, 42,  8,  0,  0,  0,  0,  0,  0,  1, 42,170,170,170,168,128,  0,
  170,170,170,170,169,  8,  4,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  1,  0,  0, 18, 42,170,170,170,170,168,  0,
  170,170,170,170,168,  4,  0,  1,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 54,128,  0, 42,170,170,170,170,170,  0,
  170,170,170,168, 36, 11,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,188, 96, 15,170,170,170,170,170,170,  0,
  170,170,170,170,  0,130, 14, 96,  0,  0,  0,  0,  0,  0,  0,  0,  1,  0,  0,  0, 95,  0, 63,234,170,170,170,170,170,128,
  170,170,170,170,170,170,143,255,  0,  0,  0,  0,  0,  0,  0,  0,  0, 12,  0,  0,249,240, 63,234,170,170,170,170,170,128,
  170,170,170,170,170,170,144, 63,240,  0,  0,  0,  0, 65,  0,  0,  0, 15,192,  0, 63,144, 63,170,170,170,170,170,170,160,
  170,170,170,170,170,170,175,239,240,  0,  1,  0,  0,  2, 24,  0,  0, 15,252,  0,  7,228, 15,218,170,170,170,170,170,168,
  170,170,170,170,170,170,168,255,240, 48, 64,  0, 13,129,  1,  0,  0,  3,255,  0,  0,114, 15,250,170,170,170,170,170,170,
  170,170,170,170,170,170,170, 62,192,188,  0,  0, 63,126,192, 36,  0,  2,127,208,  0,185,191,250,170,170,170,170,170,170,
};

byte temp;

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

  for(int n=0; n < (IMG_HEIGHT*IMG_BWIDTH); n++)
  {
  temp = img_data[n];
  Serial.print(temp);
  Serial.print("\n");

  }

}

void loop() {
  // put your main code here, to run repeatedly:

}

PROGMEM is in a different address space to RAM, and must be accessed differently.

The link has some examples.

Thank you very much AWOL!

I changed the code using the examples on the page you sent and it now works fine! :slight_smile:

. . . and best of all, you fixed it yourself!

Feels good, doesn't it?