Pages: [1]   Go Down
Author Topic: Question about Arrays  (Read 821 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 74
Arduino as a way of life
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

UK
Offline Offline
Faraday Member
**
Karma: 17
Posts: 2884
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Per Arduino ad Astra

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 74
Arduino as a way of life
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Arduino Duemilanove ATMega 328
Logged

UK
Offline Offline
Faraday Member
**
Karma: 17
Posts: 2884
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Should be Ok.
Of course, you may want to read about for loops, unless you really like typing.
Code:
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.
« Last Edit: February 26, 2011, 04:12:37 pm by Groove » Logged

Per Arduino ad Astra

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 74
Arduino as a way of life
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Sorry the code has to be like:

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


Logged

UK
Offline Offline
Faraday Member
**
Karma: 17
Posts: 2884
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Per Arduino ad Astra

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 74
Arduino as a way of life
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

What do you mean with long?

Can your please write me the code below?
Logged

UK
Offline Offline
Faraday Member
**
Karma: 17
Posts: 2884
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
long voltSum;  //global or static

voltSum += analogRead(0);
Logged

Per Arduino ad Astra

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 74
Arduino as a way of life
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

thank you smiley
Logged

UK
Offline Offline
Faraday Member
**
Karma: 17
Posts: 2884
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Per Arduino ad Astra

Pages: [1]   Go Up
Jump to: