Question about Arrays

Hello,

I would like to know if there is anyway to make the median of the value with any control statement such as for, while…

void loop () {
for (int i = 0; i<31;i++) {
     voltL = analogRead (0);
     myArray[i] = voltL;
  }
   value = (myArray[0]+myArray[1]+myArray[2]+myArray[3]+myArray[4]+myArray[5]+myArray[6]+myArray[7]+myArray[8]+myArray[9]+myArray[10]+myArray[11]+myArray[12]+myArray[13]+myArray[14]+myArray[15]+myArray[16]+myArray[17]+myArray[18]+myArray[19]+myArray[20]+myArray[21]+myArray[22]+myArray[23]+myArray[24]+myArray[25]+myArray[26]+myArray[27]+myArray[28]+myArray[29])/30;
 Serial.println (value);
}

That way, it works but I wolud like to make an array of 512 values.

Thank you.

512 ints on a processor that may only have 1024 bytes of RAM is a bit of an ask. What board do you have?

Arduino Duemilanove ATMega 328

Should be Ok.
Of course, you may want to read about for loops, unless you really like typing.

for (int i = 0; i<31;i++)

You declared myArray with 31 elements, didn’t you?
So why did your average only use 30 of the values?

Or skip the array altogether, and sum the readings into a long.

You declared myArray with 31 elements, didn’t you?

Sorry the code has to be like:

for (int i = 0; i<30;i++) {

For a median filter, you need an array, but for a simple arithmetic mean, a single long will suffice.

What do you mean with long?

Can your please write me the code below?

long voltSum;  //global or static

voltSum += analogRead(0);

thank you :slight_smile:

That won't find the median - just the arithmetic mean.