Distance Calculations take too much time!

Hello I'm participating in a robotics competition and I'm using an Arduino Mega 1280. The problem I have is with my four GP2D12 IR distance sensors. They work pretty fine but since the nature of my project requires some precision, I programmed the arduino to take 10 values, calculate their mean, then calculate their Statistical Variance, then take only the values closest to the mean. All this to eliminate the peak values the sensors give from time to time, in order to get much better distance sensing.

I was very proud of my function and I went out to test the robot in the hall 8). Problem is, the time between two readings becomes too long, and meanwhile the robot has already crashed on the wall. :D

So what can I do, either to reduce the time consumed by my function, or do something to make the IRs read with a higher frequency perhaps?

Thank you

So what can I do, either to reduce the time consumed by my function

Without seeing how you implemented your code, the only answer you can get is "optimize it." How? Again, would need to see how you are doing things now.

or do something to make the IRs read with a higher frequency perhaps

Higher frequency? That would mean more samples and more processing (time).

Do it figure skating judging style - throw out the high & low & average the rest :)

CrossRoads: Do it figure skating judging style - throw out the high & low & average the rest :)

Also, averaging 5 readings will be faster than averaging 10 readings. Reading an IR sensor is not a fast process.

You are probably using floating point. If so, switch to fixed point calculations.

Quick and dirty 'average' function which works for smoothing :

average=(average*0.75) + (lastread * 0.25);

Use float type for lastread & average;

You can tweak the multipliers to 'alter' the number of readings in the average, the only proviso is that they add up to 1.

Problem is, the time between to readings becomes too long, and meanwhile the robot has already crashed on the wall.

If I interpret this sentence in English (and to == two) you don't have time to do more than 1 reading.

So instead of calculating faster as anyone suggest you need to slow down the robot. This means sssslllllllooooowwwww ddddooooowwwwwnnnn unless you are far enough of the wall you may speed up a little but but if something (a wall a brick a humanoid) comes close => drop the speed, switch gear etc and you will have time enough to do any math you need.

You maybe won't win the battle but at least the walls are save ;)

drums**drums
Here is my beautiful function 8)

float test_values(int *tab, int sizeTab){
        // Local Vars
        int i,j;
        int copy[sizeTab];
        float var[sizeTab];
        float mean=0;
        float meanVar=0;
        int nb_best = sizeTab/2;
        int newCopy[nb_best];
        float newMean=0;
        
        // Copy tab, calculate the Mean
        Serial.print("Tab values=>");
        for (i=0; i<sizeTab;i++){
          copy[i]=tab[i];
          Serial.print(tab[i]);
          Serial.print("|");
          mean+=tab[i];
        }
        mean/=sizeTab;
        Serial.println("");
                        
        // Calculating variances and Mean of variances: Var = (value - Mean)²
        Serial.println("Variances =>");
        for(i=0 ; i<sizeTab ; i++){
          var[i]=pow((tab[i]-mean), 2);
          Serial.print(var[i]);
          Serial.print("|");
          meanVar+=var[i];
        }
        meanVar/=sizeTab;
        Serial.println("");
                  
        // Selection of best values => the colosest to the mean
        for (i=0; i<nb_best; i++){
          int index=-1;
          int max=0;
                  for (j=0; j<sizeTab; j++){
                    if (copy[j]>max && var[j]< meanVar){
                      max=copy[j];
                      index=j;}
                  }
          newCopy[i]=copy[index];
          newMean+=newCopy[i];
          copy[index]=0;
        }
  
  // Copy selected values and calculate new Mean
  Serial.print("Selected values =>");
  for(i=0;i<nb_best;i++){
      Serial.print(newCopy[i]);
      Serial.print("|");
  }
  newMean/=nb_best;
            
return newMean;
}

I calculated with millis(), it takes roughly 590ms to apply this function to four tabs of 10 values from my four IRs. TOO MUCH!

amine: drumsdrums Here is my beautiful function 8)

I calculated with millis(), it takes roughly 590ms to apply this function to four tabs of 10 values from my four IRs. TOO MUCH!

Are all the serial prints needed? Taking those out might speed things up...?

Throw away all the serial print's from the function and time again ...

(crosh you beat me :)