Array Size Based on EEPROM Value

Hello, I am trying to set an array size based off of a value stored in EEPROM, in this case "the number of LEDs"
I don't need the size of the array to vary throughout the sketch, just upon initial boot.

I tried this prior to the setup loop:

const int NUMPIXELS = EEPROM.read(2);

int value[NUMPIXELS];

But I get this error:
array bound is not an integer constant before ']' token
int value[NUMPIXELS];
^

Any idea on how I could do this other than just set an array of size 1000?

You could try dynamic allocation with malloc...

have a look at Arduino Playground - DynamicArrayHelper

Statically declared arrays need the size to be known at compile-time.

You either need to use a pointer and initialize it with malloc, or use a local variable for the array,
but the array only exists until the function returns. For instance:

int numpixels ;

void setup ()
{
  numpixels = EEPROM.read(2);
  int value[numpixels];
  while (true)  // prevent setup returning
    my_loop (value) ;
}

void my_loop (int * value)
{
  .....  value [n] ....
}

If you are not expecting to use a large number of LEDs, you can allocate the maximum number of LEDs that you will ever use, and simply keep the actual number in a variable that is set to a value read from EEPROM. It wastes bytes but if the number is small, it's simpler.

A worked out example for the use of malloc

// pointer to array of integers
int *ptr;

void setup()
{
  // value from eeprom
  int numelements = 12;

  // allocate memory; note the cast in front of malloc
  ptr = (int*)malloc(sizeof(int) * numelements);
  if (ptr == NULL)
  {
    // error
    for(;;);
  }
  // clear the memory to predefined value
  memset(ptr, 0, sizeof(int) * numelements);

  // assign values to array elements using index
  for (int cnt = 0; cnt < numelements; cnt++)
  {
    // assign
    ptr[cnt] = cnt + 1;
  }

  // demo use of a copy of the pointer
  int *workptr = ptr;
  // print the values in the array using a different approach
  for (int cnt = 0; cnt < numelements; cnt++)
  {
    // print using work pointer
    printf("%d\n", *workptr);
    // increment work pointer to point to next element
    workptr++;

    // or in a one-liner
    //printf("%d\n", *workptr++);
  }

  // ONLY if you no longer need the array, free the memory
  //free(ptr);

}

void loop()
{

}

There are two ways that you can access array members. The first way is identical to how one usually accesses array members (using indexing) and shown for the assigning of values to the array members in above code. The second way is by using the pointer and incrementing it (shown in the printing of the values in above code).

You need to cleanup dynamically allocated memory when you no longer need it; the free() in above code is commented out as I assume you need that integer array elsewhere :wink:

Note that this was created as a PC application and hence the use of printf statements; you can change it to a similar Serial.println() for debugging purposes.

aarg:
If you are not expecting to use a large number of LEDs, you can allocate the maximum number of LEDs that you will ever use, and simply keep the actual number in a variable that is set to a value read from EEPROM. It wastes bytes but if the number is small, it's simpler.

This is the simplest answer. There must be a maximum number you can tolerate, make the array that large. Then use the EEPROM variable to control how much of that array you actually use.