Pages: [1]   Go Down
Author Topic: array average  (Read 396 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi guys I need some help, I am fairly new to arduino, I have an array of 100 integers and I need the average of these integers, how can I do it?

This is my code;

for (int x = 0; thisPin < 100; x++)
  {
    adcValue1
  • = analogRead(phototransistorPin1);
    adcValue2
  • = analogRead(phototransistorPin2);
    adcValue3
  • = analogRead(phototransistorPin3);
    adcValue4
  • = analogRead(phototransistorPin4);
  }

now I need the average of each adcValue
Thanks
Logged

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What Arduino, or compatible, device is this running?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am using arduino mega 2560
Logged

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3380
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hard to say with bullet point style code
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm sorry that was just a typing error,

This is the code: for (int x = 0; x< 100; x++)
  {
    adcValue1= analogRead(phototransistorPin1);
    adcValue2= analogRead(phototransistorPin2);
    adcValue3= analogRead(phototransistorPin3);
    adcValue4= analogRead(phototransistorPin4);
  }
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 85
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


float runningTotal = 0;
float average = 0;
for (int x = 0; x< 100; x++)
  {
   
    adcValue1= analogRead(phototransistorPin1);
    runningTotal = adcValue1 + runningTotal;
    adcValue2= analogRead(phototransistorPin2);
    runningTotal = adcValue1 + runningTotal;
    adcValue3= analogRead(phototransistorPin3);
    runningTotal = adcValue1 + runningTotal;
    adcValue4= analogRead(phototransistorPin4);
     
   average = runningTotal/x;
   
  }
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12631
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
for (int x = 0; thisPin < 100; x++)
{
    adcValue1[x] = analogRead(phototransistorPin1);
    adcValue2[x] = analogRead(phototransistorPin2);
    adcValue3[x] = analogRead(phototransistorPin3);
    adcValue4[x] = analogRead(phototransistorPin4);
}

You need to use [ code ] [ /code ] tags to stop the forum software from mangling your code.

To generate an average, add up all the samples and divide by the number of samples.
Code:
long total = 0;
for(int x = 0; x < 100; x++)
{
    total += analogRead(phototransistorPin1);
}
adcValue1 = total/100;

Since you're doing the same thing four times, it would make sense to use arrays to hold your pins and to hold the resulting averages.

Code:
const int NUM_PINS = 4;
const int phototransistorPin[NUM_PINS] = { 2, 3, 4, 5 }; // substitute whatever pins you're using

...


long average[NUM_PINS];
long total[NUM_PINS];
for(int pin = 0; pin < NUM_PINS; pin++)
{
  total[pin] = 0;
  for(int sample = 0; sample < 100; sample++)
  {
    total[pin] += analogRead(phototransistorPin[pin]);
  }
  average[pin] = total[pin]/100;
}
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you all!
Logged

Pages: [1]   Go Up
Jump to: