Averaging Values

Hello,
First, I’m such a newbie! Second, I am working with 5 photocells which are connected to the arduino from A0 -A4. I am able to read the values and such. However, I have to average the top three values of the photocells.

For example:
Cell 1 = 100
Cell 2 = 600
Cell 3 = 200
Cell 4=300
Cell 5 = 500

I have to get the average of the top three, so 600+300+500. I am trying to put them in an array and sort the top three and get the average like that, however, I am having issues.

code that I have so far:

int analogPin0 = 0; 
    int analogPin1 = 1; 
    int analogPin2 = 2; 
    int analogPin3 = 3; 
    int analogPin4 = 4; 
   int number;
    
  
    int analogreading = 0;
    int analogreading1 = 1;
    int analogreading2 = 2;
    int analogreading3 = 3;
    int analogreading4 = 4;
    
    int list[] = { analogPin0, analogPin1, analogPin2, analogPin3, analogPin4 };

    void setup()
    {
      Serial.begin(9600);  
    }

    void loop()
    {
   analogreading = analogRead(analogPin0);
   Serial.print("Pin0=" );
  
        Serial.println(analogreading);
       
    
 analogreading1=   analogRead(analogPin1);
       Serial.print("Pin1 =");
        Serial.println(analogreading1);
        
         analogreading2=   analogRead(analogPin2);
       Serial.print("Pin2 =");
        Serial.println(analogreading2);
        
        
          analogreading3=   analogRead(analogPin3);
       Serial.print("Pin3 =");
        Serial.println(analogreading3);

        analogreading4=   analogRead(analogPin4);
       Serial.print("Pin4 =");
        Serial.println(analogreading4);
        
     //   number=(analogreading1 + analogreading2)/2;
     //   Serial.print("num=");
     //   Serial.println(number);
        
   
        for (int i=3 ; i ; i--) {
        for (int j=0 ; (j + i) < 5 ; j++) {
            if (list[ j ] < list[ j + i ]) {
                int t = list[ j ];
                list[ j ] = list[ j + i ];
                list[ j + i ] = t;
            }
        }
    }
    int sum = 0;
    for (int k=0 ; k < 3 ; k++) {
        sum += list[ k ];
    }
    float average = sum / 3.0;
    
    Serial.print("avg=");
    Serial.println(average);
    
    
       
        delay(2000);
     
    }

And the output is just:

Pin0=157
Pin1 =173
Pin2 =170
Pin3 =142
Pin4 =175
avg=3.00

Pin0=157
Pin1 =172
Pin2 =169
Pin3 =140
Pin4 =173
avg=3.00

The Average is remaining the same?

in the list the average should be 173 for the first one and 171 for the second?

Thank you so much for helping.

The loop with 'i' and 'j', is that a sorting algorithm ? Where did you get that from ? Can you print the 5 values after sorting ?

It is supposed to be a sorting algorithm, However I don't think its doing its job, the output that I have posted under the code is what I am getting. of course the photocell numbers are different.

You are initializing list[] to contain the numbers 1,2,3 and 4 and those are never changed. Why do you think you need to sort the list?

Try the following: actually should order any number of integers.
int readings[5];
int rank[5];
int i, j;

//first, put all the readings in the readings array, any order

for (i = 0; i < 5; i++) rank = 0; //initialize the rank array

  • for (i = 0; i < 5; i++)*
  • for(j = i+1; j < 5; j++)*
    if readings[j] <= readings rank ++;
    I didn’t test this, but I’m pretty sure it will work. Good luck.
    John Doner
    When this is done running, the rank[] array should contain the digits 0, 1, 2, 3, 4 in some order, and the location of the value 4 in rank[] should correspondto the location of the largest reading in readings[], etc.

I didn't test this, but I'm pretty sure it will work. Good luck.

That code will never even compile.

a starter slightly different

//
//    FILE: sevenofnine.ino
//  AUTHOR: Rob Tillaart
// VERSION: 0.1.00
// PURPOSE: 
//    DATE: 
//     URL:
//
// Released to the public domain
//
int ar[] = { 
  157, 173, 170, 0, 175, 142, 165, 181, 49 };

void setup() 
{
  Serial.begin(115200);
  Serial.println("Start ");

  bubblesort(ar, 9);
  int sum = 0;
  for (int i = 2; i < 9; i++)
  {
    sum += ar[i];
  }
  float avg = sum/7.0;
  Serial.println(avg, 3);
}

void loop() 
{
}

void bubblesort(int in[], int len) 
{
  int tmp;
  for (int j = 0; j < len; j++) 
  {
    for (int i = 1; i < len - j; i++) 
    {
      if(in[i-1] > in[i])
      {
        tmp = in[i];
        in[i] = in[i-1];
        in[i-1] = tmp;
      }
    }
  }
}