Software: IDE 1.6.5
Hardware: Uno
I have a requirement to store two dimensional arrays of numbers in progmem. I started by following the Array of strings example at PROGMEM - Arduino Reference and after attempting to change this for integer use I have ran into a problem that has me stumped. I believe I have narrowed the issue down to the way I’m using pgm_read_word, I am unable to use the first square brackets with the For-loop variable (thisTune). Below is the code that loops twice and displays garbage data, I have commented out a line that only loops once – this shows the correct data but obviously only for a single array. Could someone kindly advise me of my error?
////one 31
const int one_melody[] PROGMEM = { 1047,932,1047,784,622,784,523,1047,932,1047,784,622,784,523,1047,1175,1245,1047,1245,1047,1245,1175,932,1175,932,1175,1047,932,784,932,1047 };
const int one_noteDurations[] PROGMEM = { 187,187,187,187,187,187,375,187,187,187,187,187,187,375,187,187,187,93,187,93,187,187,93,187,93,187,187,187,187,187,375 };
////two 44
const int two_melody[] PROGMEM = { 1480,1568,1568,1568,740,784,784,784,370,392,392,392,0,1568,1568,1568,1568,0,1319,0,1568,1319,0,1568,1760,1319,1319,1319,0,1175,1047,0,1109,0,784,0,1109,784,0,1109,0,1175,1175,1175 };
const int two_noteDurations[] PROGMEM = { 150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150 };
//tune tables
//melody
const int * const table_melody[] PROGMEM = {one_melody, two_melody};
//duration
const int * const table_duration[] PROGMEM = {one_noteDurations, two_noteDurations};
//length
const int table_length[] PROGMEM = {31, 44};
void setup() {
Serial.begin(9600);
}
void loop() {
int arraySize = NULL;
for (int thisTune = 0; thisTune < 2 ; thisTune++) {
//for (int thisTune = 1; thisTune < 2 ; thisTune++) {
Serial.print("length:");
arraySize = pgm_read_word(&table_length[thisTune]);
Serial.println(arraySize);
for (int thisNote = 0; thisNote < arraySize; thisNote++) {
int noteDuration = pgm_read_word(&table_duration[thisTune][thisNote]);
int thisTone = pgm_read_word(&table_melody[thisTune][thisNote]);
Serial.print("[thisTune]:");
Serial.print(thisTune);
Serial.print("[thisNote]:");
Serial.print(thisNote);
Serial.print("[thisTone]:");
Serial.print(thisTone);
Serial.print("[duration]:");
Serial.println(noteDuration);
}
}
while(1); // Stop
}