Changing an array size with a pot???

I have an idea for a project that would need an array of varying size. I would like to use the array to record data from an input. This I know how to do. But I only know how to do it for a set amount (say [512]...what i need to do is make the array the largest size I will need (say 512) and then have the pot control how much of that will be used to record.

so my first thought was oh, How bout just make an array like this

int varArray [x]; X=AnalogRead(0);

but that doesnt work.

it seems it wont let me declare an array with a variable as its size.

so how else can i get a pot to use portions of an array?

the benefit to this would be so that the user could determine how long of a loop to create.

I hope this makes sense so far. (I dont have any code to show for an example)

thanks for any help!!!

You can dynamically allocate arrays of variable size using malloc, but that way involves pitfalls.

You should plan on statically allocating the largest array that you will be able to support, and use the potentiometer to control how much of the array to actually use.

"You should plan on statically allocating the largest array that you will be able to support, and use the potentiometer to control how much of the array to actually use."

how can i do that?

sorry for my ignorance, I just have no idea how to implement the pot to control the array.

int array[512];
int arrayLimit = 512;

void loop()
{
   arrayLimit = analogRead(potPin)/2.0;

   // do something with array[0] to array[arrayLimit-1]
}

You can have an int array with a dimension of 512, that will use 1K of ram that the atmega328p as, and the boot code from arduino uses some ram, and also does the analogRead function and even the loop and setup, so you could be hitting the stack in a very short amount of time and your program will fail and you dont know why
In worse case, use an array of uint8_t/char/unsigned char/byte, that will only use 512 bytes…

thanks!

Senso I am not quite sure what you mean…

are you saying to use uint8_t char value [512]; instead of int value [512];?

In worse case, use an array of uint8_t/char/unsigned char/byte, that will only use 512 bytes…

thanks

Yes, what to you really want to achieve? That might have other solutions than just a gigantic array.

Be aware that potmeters and ADC convertors may “float” a bit (e.g. values 900, 901, 902, 899, …) so the value of arrayLimit may not be exact the same every iteration.
To prevent this you could use an additional button so only when pressed the potmeter is read, or ignore small variations, or otherwise. Furthermore you could use the map function for maintainability.

#define ASIZE 512

int array[ASIZE];
int arrayLimit = 0;

void loop()
{
  if (digitalRead(button) == HIGH)   // assume HIGH is pressed
  {
    arrayLimit = map( analogRead(potPin), 0, 1023, 0, ASIZE);     // http://www.arduino.cc/en/Reference/map
  }
   // do something with array[0] to array[arrayLimit-1]
}

i am still a bit confused by // do something with array[0] to array[arrayLimit-1]

thanks

robtillaart's code fragment shows you how to use a pot to read a value to use as the high limit on a presized array. His comment is just a placeholder to indicate that you now have to add the code to do whatever you were planning with it.