Go Down

### Topic: Question about Arrays (Read 1 time)previous topic - next topic

#### noxasdj

##### Feb 26, 2011, 09:59 pm
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

#1
##### Feb 26, 2011, 10:02 pm
512 ints on a processor that may only have 1024 bytes of RAM is a bit of an ask.
What board do you have?

#### noxasdj

#2
##### Feb 26, 2011, 10:07 pm
Arduino Duemilanove ATMega 328

#### Groove

#3
##### Feb 26, 2011, 10:09 pmLast 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.

#### noxasdj

#4
##### Feb 26, 2011, 10:14 pm
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

#5
##### Feb 26, 2011, 10:16 pm
For a median filter, you need an array, but for a simple arithmetic mean, a single long will suffice.

#### noxasdj

#6
##### Feb 26, 2011, 10:20 pm
What do you mean with long?

#### Groove

#7
##### Feb 26, 2011, 10:24 pm
Code: [Select]
`long voltSum;  //global or staticvoltSum += analogRead(0);`

#8
thank you

#### Groove

#9
##### Feb 26, 2011, 10:35 pm
That won't find the median - just the arithmetic mean.