Multi-Dimensional Arrays problem

I made this test program..

const PROGMEM byte prog[2][25] ={
{0,1,2,3,99},
{1,2}
}; //5 programmas met 23 aantal stappen 99 is einde

byte x=0;
byte i=0;

void setup() {
// put your setup code here, to run once:

Serial.begin(9600);
}

void loop()
{
for (i = 0; i <5; i++)
{
x=prog[0][i];
Serial.print (i);
Serial.print (" ");
Serial.println(x);
delay(1000);
}
delay(5000);
}

if i write x=prog[0][4]; it returns 99 (what you would expect)
But if i use the variable i (witch is 0,1,2,3,4) it doesent work!&#$&^

Im doing something wrong??
I also tried to avoid serial print (while that give sometimes problems) but that also doesent help.

Here's a ref guide for you: PROGMEM - Arduino Reference

And a working example.

const byte prog[2][2] PROGMEM = { {1, 2}, {3, 4} };

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

  for (int i = 0; i < 2; i++) {
    for (int j = 0; j < 2; j++) {
      Serial.print(F("prog["));
      Serial.print(i);
      Serial.print(F("]["));
      Serial.print(j);
      Serial.print(F("] = "));
      Serial.println(pgm_read_byte_near(&prog[i][j]));
    }
  }
}

void loop() { }
1 Like

Ah.. its about the adress pointer

Thanx

No, it's about the pgm_read_byte

Please remember to use code tags when posting code

In this case the compiler does the work because you only use constants. The compiler knows, that prog[0][4] is 99, inserts that directly and compiles it to x=99;
If you use variables as index this is not possible and the array must be accessed at runtime. Because the AVR processors need special commands to read data from PROGMEM you must advise the compiler to do so. This is done by the pgm_read... functions.

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination