howto-dynamic array (size determined by pot value)


hey
i'm adapting "smoothing" sketch comes arduino 0018 midi controller, coming along nicely.

i'm using smoothing function average series of readings parallax ping sensor. implementing sketch own code easy. now, however, have more complicated problem.

i want able determine size of array (and thus, size of pool of averaged numbers) using potentiometer.

so far, have been unable tell how create array size can control @ whim. array's size fixed upon program initialization? smoothing code (which i've pasted below) begins const int setting "numreadings." value i'd able manipulate in realtime, may shorten/lengthen array depending on how snappy want ping respond.

unfortunately, when change const int regular old "int" code won't compile.


code: [select]
const int numreadings = 10;

int readings[numreadings];      // readings analog input
int index = 0;                  // index of current reading
int total = 0;                  // running total
int average = 0;                // average

int inputpin = 0;

void setup()
{
 // initialize serial communication computer:
 serial.begin(9600);                  
 // initialize readings 0:
 for (int thisreading = 0; thisreading < numreadings; thisreading++)
   readings[thisreading] = 0;          
}

void loop() {
 // subtract last reading:
 total= total - readings[index];        
 // read sensor:  
 readings[index] = analogread(inputpin);
 // add reading total:
 total= total + readings[index];      
 // advance next position in array:  
 index = index + 1;                    

 // if we're @ end of array...
 if (index >= numreadings)              
   // ...wrap around beginning:
   index = 0;                          

 // calculate average:
 average = total / numreadings;        
 // send computer (as ascii digits)
 serial.println(average, dec);              
}

the workaround i've had create series of many different sized arrays want , use pot select 1 being used.

this not ideal clutter of 17 arrays ungainly, , feel there must better way it.


Arduino Forum > Forum 2005-2010 (read only) > Software > Syntax & Programs > howto-dynamic array (size determined by pot value)


arduino

Comments

Popular posts from this blog

CAN'T INSTALL MAMBELFISH 1.5 FROM DIRECTORY - Joomla! Forum - community, help and support

error: expected initializer before 'void'

CPU load monitoring using GPIO and leds - Raspberry Pi Forums