I am trying to store a large number of values into a couple of arrays using PROGMEM, but I am finding that a few of the values are read back corrupted. I am storing just 1s and 0s, but when I read these values back, I will sometimes get values greater than 1. I am using a MEGA 2560 to do this.
// Each array contains 32000 values
const byte VALUES_1[] PROGMEM = { 1, 0, 1, .... 0 };
const byte VALUES_2[] PROGMEM = { 1, 1, 0, .... 0 };
const byte VALUES_3[] PROGMEM = { 1, 0, 1, .... 1 };
const byte VALUES_4[] PROGMEM = { 1, 0, 1, .... 1 };
const byte VALUES_5[] PROGMEM = { 1, 0, 1, .... 1 };
const long ARR_LEN = 32000;
long index = 0;
void setup() {
Serial.begin(57600);
while (!Serial) {}
}
byte readNextValue() {
if (index >= 160000L) {
return -1;
}
Serial.print(index);
Serial.print(" ");
long arr = index / ARR_LEN;
long i = index % ARR_LEN;
index++;
byte j = -1;
switch(arr) {
case 0:
j = pgm_read_byte_far(VALUES_1 + i);
Serial.println(j);
return j;
case 1:
j = pgm_read_byte_far(VALUES_2 + i);
Serial.println(j);
return j;
case 2:
j = pgm_read_byte_far(VALUES_3 + i);
Serial.println(j);
return j;
case 3:
j = pgm_read_byte_far(VALUES_4 + i);
Serial.println(j);
return j;
case 4:
j = pgm_read_byte_far(VALUES_5 + i);
Serial.println(j);
return j;
}
Serial.println("Unable to return next value");
return -1;
}
void loop() {
// put your main code here, to run repeatedly:
readNextValue();
}
What I have noticed is that when I use pgm_read_byte_far
for case 0
I will get garbage values for right away, but using pgm_read_byte_near
will cause these garbage values to occur sometime later. Any insight to this problem is appreciated.