Hi, I'm trying to store a large number of ints within arrays in Programme Memory. I've produced some example code to demonstrate the issue that I'm having.
The code works fine when I try to access the data within the arrays using a number (not stored within a variable). Or when I access the data using a variable that was declared outside of any functions and is not changed within a function. However when accessing the data with a variable that is changed within a function it returns incorrect data.
I think this is an issue surrounding pointers, but I'm baffled as to what I'm doing wrong.
const int ARRAY_0[] PROGMEM = {0,1,2,3,4,5}; //0
const int ARRAY_1[] PROGMEM = {0,1,2,3,4,5}; //1
const int ARRAY_2[] PROGMEM = {0,1,2,3,4,5}; //2
const int ARRAY_3[] PROGMEM = {0,1,2,3,4,5}; //3
const int ARRAY_4[] PROGMEM = {0,1,2,3,4,5}; //4
const int ARRAY_5[] PROGMEM = {0,1,2,3,4,5}; //5
const int ARRAY_6[] PROGMEM = {0,1,2,3,4,5}; //6
const int *const ARRAY_TABLE[] PROGMEM = {ARRAY_0,ARRAY_1,ARRAY_2,ARRAY_3,ARRAY_4,ARRAY_5,ARRAY_6};
int selected_array = 0;
void setup() {
Serial.begin(9600);
}
void loop() {
for (int i = 0; i < 6;i++){
int value = (int)pgm_read_word(&(ARRAY_TABLE[0])[i]);
Serial.print(value);
Serial.print(",");
}
Serial.println(" ");
for (int i = 0; i < 6;i++){
int value = (int)pgm_read_word(&(ARRAY_TABLE[selected_array])[i]);
Serial.print(value);
Serial.print(",");
}
Serial.println(" ");
//selected_array = 0;
for (int i = 0; i < 6;i++){
int value = (int)pgm_read_word(&(ARRAY_TABLE[selected_array])[i]);
Serial.print(value);
Serial.print(",");
}
delay(10000);
}
Writes the following to the Serial port, as expected.
0,1,2,3,4,5,
0,1,2,3,4,5,
0,1,2,3,4,5,
However, if I uncomment the change in value (from 0 to 0) of selected_array...
const int ARRAY_0[] PROGMEM = {0,1,2,3,4,5}; //0
const int ARRAY_1[] PROGMEM = {0,1,2,3,4,5}; //1
const int ARRAY_2[] PROGMEM = {0,1,2,3,4,5}; //2
const int ARRAY_3[] PROGMEM = {0,1,2,3,4,5}; //3
const int ARRAY_4[] PROGMEM = {0,1,2,3,4,5}; //4
const int ARRAY_5[] PROGMEM = {0,1,2,3,4,5}; //5
const int ARRAY_6[] PROGMEM = {0,1,2,3,4,5}; //6
const int *const ARRAY_TABLE[] PROGMEM = {ARRAY_0,ARRAY_1,ARRAY_2,ARRAY_3,ARRAY_4,ARRAY_5,ARRAY_6};
int selected_array = 0;
void setup() {
Serial.begin(9600);
}
void loop() {
for (int i = 0; i < 6;i++){
int value = (int)pgm_read_word(&(ARRAY_TABLE[0])[i]);
Serial.print(value);
Serial.print(",");
}
Serial.println(" ");
for (int i = 0; i < 6;i++){
int value = (int)pgm_read_word(&(ARRAY_TABLE[selected_array])[i]);
Serial.print(value);
Serial.print(",");
}
Serial.println(" ");
selected_array = 0;
for (int i = 0; i < 6;i++){
int value = (int)pgm_read_word(&(ARRAY_TABLE[selected_array])[i]);
Serial.print(value);
Serial.print(",");
}
delay(10000);
}
Writes the following to the Serial port:
0,1,2,3,4,5,
-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,
Notice how both the second and third loops return erroneous data, when the variable was changed between the two.
Any help with this will be much appreciated - thank you, in advanced.