Go Down

Topic: Question about Arrays (Read 981 times) previous topic - next topic

noxasdj

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.....

Code: [Select]

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.

Groove

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

noxasdj

Arduino Duemilanove ATMega 328

Groove

#3
Feb 26, 2011, 10:09 pm Last Edit: Feb 26, 2011, 10:12 pm by Groove Reason: 1
Should be Ok.
Of course, you may want to read about for loops, unless you really like typing.
Code: [Select]
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.
Per Arduino ad Astra

noxasdj

Quote
You declared myArray with 31 elements, didn't you?


Sorry the code has to be like:

Code: [Select]
for (int i = 0; i<30;i++) {



Groove

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

noxasdj

What do you mean with long?

Can your please write me the code below?

Groove

Code: [Select]
long voltSum;  //global or static

voltSum += analogRead(0);
Per Arduino ad Astra

noxasdj


Groove

That won't find the median - just the arithmetic mean.
Per Arduino ad Astra

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy