Go Down

Topic: Store and read int array to Progmem (Read 511 times) previous topic - next topic

jw322103

Mar 26, 2018, 06:05 pm Last Edit: Mar 26, 2018, 06:06 pm by jw322103
Hi

I am working on a larger project but the root problem right now is storing large arrays to work with. They are small enough that I can use the flash memory (Progmem) but this is where my problem comes up.
I keep getting errors when trying to save them to flash when defining the size of the array. If I do not define the array size it seems to work but then I can not seem to read the values back after I do.
Below is my code, I can not see anything obvious so I must be misunderstanding something. Any help will be greatly appreciated.

#include <avr/pgmspace.h>

int i = 0;
const int test[] PROGMEM = {
  4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3
  };
 
void setup() {
    Serial.begin(9600);                // sets serial baud rate to 9600
 }

void loop() {

  Serial.println(test);
   Serial.print(" ");
  i++;
  if(i == (sizeof(test)-1))  // Reset the counter to repeat the wave
    i = 0;
  delay(100);
 
}

PaulS

The sizeof() an array is NOT the number of elements in the array.

The number of elements in the array is the size of the array divided by the size of one element of the array.

Your improperly posted code does something. You expect it to do something. What either of those things is, only you know.
The art of getting good answers lies in asking good questions.

TolpuddleSartre

The sizeof() an array is NOT the number of elements in the array.
It is sometimes, like for char/byte/Boolean arrays.

jw322103

Thank you PaulS. I was not aware of that, but it makes sense.

I am basically just trying to figure out how to store an array into the flash memory/Progmem and read the values back.


econjack

As Paul mentioned, often you will see this in a program:

#define ELEMENTS(x) (sizeof(x) / sizeof(x[0]))

// more code...

   for (i = 0; i < ELEMENTS(myArray); i++) {


The macro ELEMENTS() returns the number of elements in the array that is passed in.

Go Up