first of all I'm sorry for my english.
My problem is that I want to use an array of numbers on a function, but I'm getting a weird result inside a for loop. Doing some tests I've seen that it happens even in the simplest sketch so I should be doing something wrong.
I've created a two level int and I want to iterate through the second level using a for loop. Outside the for loop all data gotten from that array is OK, but just inside the loop I'm just getting garbage as if the array was not initialized.
My simple sketch is this:
const int number_small[][14] PROGMEM = {
{124, 124, 387, 387, 399, 399, 443, 443, 483, 483, 387, 387, 124, 124},
};
void setup() {
Serial.begin(9600);
Serial.print(String(number_small[0][0]) + "\n");
Serial.print(String(number_small[0][1]) + "\n");
Serial.print(String(number_small[0][2]) + "\n");
Serial.print(String(number_small[0][3]) + "\n");
Serial.print(String(number_small[0][4]) + "\n");
Serial.print(String(number_small[0][5]) + "\n");
Serial.print(String(number_small[0][6]) + "\n");
Serial.print(String(number_small[0][7]) + "\n");
Serial.print(String(number_small[0][8]) + "\n");
Serial.print(String(number_small[0][9]) + "\n");
Serial.print(String(number_small[0][10]) + "\n");
Serial.print(String(number_small[0][11]) + "\n");
Serial.print(String(number_small[0][12]) + "\n");
Serial.print(String(number_small[0][13]) + "\n\n\n");
for (int i = 0; i<14; i++){
Serial.print("I: " + String(i) + " - " + String(number_small[0][i]) + "\n");
}
}
void loop() {
}
And the output is:
124
124
387
387
399
399
443
443
483
483
387
387
124
124
I: 0 - 0
I: 1 - 184
I: 2 - 0
I: 3 - 1
I: 4 - 0
I: 5 - -18248
I: 6 - -18248
I: 7 - -18248
I: 8 - 0
I: 9 - 135
I: 10 - 0
I: 11 - 0
I: 12 - 769
I: 13 - 0
What I'm doing wrong?,
Thanks!!
3 Answers 3
Constants like String(number_small[0][0])
are optimized by compiler during the build (=compile time).
However that's not the case of loop. So you have to use specific functions for reading values from PROGMEM
in the runtime:
for (int i = 0; i<14; i++){
Serial.print("I: ");
Serial.print(i);
Serial.print(" - ");
Serial.println((int)pgm_read_word(number_small[0]+i));
}
Btw: Don't use string concatenations. These are extremely hungry for the memory. See Majenko's article: The Evils of Arduino Strings
-
Hi, thanks for your answer!!. Just I've found the solution and I was posting it. And thanks for the suggestion about strings and the article.Daniel Carrasco Marín– Daniel Carrasco Marín2017年02月28日 23:02:23 +00:00Commented Feb 28, 2017 at 23:02
I was sure that I was doing something wrong and yes... Finally I did another test (storing the array on SRAM instead on PROGMEM) and it worked, so I've changed my "research direction" and I've found the solution.
With my initial sketch I was reading the PROGMEM from outside the loop, but from inside the loop what i read is the SDRAM, so I've to use the pgm_read functions to force to read from PROGMEM, and now finally is working:
pgm_read_word(number_small[0] + i)
const int number_small[][14] PROGMEM = {
{124, 124, 387, 387, 399, 399, 443, 443, 483, 483, 387, 387, 124, 124},
};
void setup() {
Serial.begin(9600);
Serial.print(String(number_small[0][0]) + "\n");
Serial.print(String(number_small[0][1]) + "\n");
Serial.print(String(number_small[0][2]) + "\n");
Serial.print(String(number_small[0][3]) + "\n");
Serial.print(String(number_small[0][4]) + "\n");
Serial.print(String(number_small[0][5]) + "\n");
Serial.print(String(number_small[0][6]) + "\n");
Serial.print(String(number_small[0][7]) + "\n");
Serial.print(String(number_small[0][8]) + "\n");
Serial.print(String(number_small[0][9]) + "\n");
Serial.print(String(number_small[0][10]) + "\n");
Serial.print(String(number_small[0][11]) + "\n");
Serial.print(String(number_small[0][12]) + "\n");
Serial.print(String(number_small[0][13]) + "\n\n\n");
int i;
for (i = 0; i<14; i++){
Serial.print("I: " + String(i) + " - " + String(pgm_read_word(number_small[0] + i)) + "\n");
}
}
void loop() {
}
Thanks and greetings!!
I refined your own answer:
- No need to use String to print a numeric value; just print the value.
- No need to add "\n" add the end of
print
, useprintln
instead. - No need to concatenate strings for printing; just print them one by one.
- In a
for
loop, declare the control variable inside
Code
const int ITEMS = 14;
const int number_small[][ITEMS] PROGMEM = {
{124, 124, 387, 387, 399, 399, 443, 443, 483, 483, 387, 387, 124, 124},
};
void setup() {
Serial.begin(9600);
for (int i=0; i < ITEMS; i++) {
Serial.println(pgm_read_word(number_small[0]+i));
}
Serial.println(); Serial.println();
for (int i = 0; i < ITEMS; i++){
Serial.print("I: "); Serial.print(i); Serial.print(" - " );
Serial.println(pgm_read_word(number_small[0] + i));
}
}
void loop() {
}
-
1I would add that
pgm_read_word(&number_small[0][i])
is more idiomatic thanpgm_read_word(number_small[0] + i)
.Edgar Bonet– Edgar Bonet2017年12月21日 09:08:13 +00:00Commented Dec 21, 2017 at 9:08