Arduino Stack Exchange is a question and answer site for developers of open-source hardware and software that is compatible with Arduino. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

first of all I'm sorry for my english.

My problem is that I want to use an array of numbers on a funcion, but I'm getting a weird result inside a for loop. Doing some tests I've seen that it happen even in the simplest sketch so I should be doing something wrong.

I've created a two level int and I want to itinerate 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 like 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!!

share|improve this question
up vote 2 down vote accepted

Constants like String(number_small[0][0]) are optimalized 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

share|improve this answer
    
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 yesterday

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!!

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.