Вопрос

Ok so I get this code to do the averaging : (written in C )

.
.
        int sum[3];
        int j;
        int avg;
      for(;;) //infinite loop
      {
       for(j=0;j<3;j++){
        i = ReadSensor(); // function that keeps saving sensor values as int i
        sum[j]=i;
        }
       avg=sum[0]+sum[1]+sum[2]+sum[3]; 
       printf("Sonar: %d \r \n", avg >> 2);
      }
.
.

Is this correct ? im shifting by 2 to divide by avg / 2^(2) which is 4 The problem is im expecting a value of about 15, however I get about 8--9 .. Im not sure why this is happening ?

Basically the sensor's readings fluctuate between 15-17, I want to get an average instead of printing noise values. Is my code correct ? Then why do I get wrong outputs !?

Это было полезно?

Решение

The number of values read from sensor is required twice. First, to control the number of iterations of for loop. Second, as the divisor of sum. Introduce a variable (say, N) to capture that.

Also, the division by shifting does not sound right, because that restricts the number of readings from the sensor to power of two.

enum { N = 4 };

sum = 0;
for( j = 0; j < N; j++) {
   i = ReadSensor(); // function that keeps saving sensor values as int i
   sum += i;
}
avg = sum / N; 
printf( "Sonar average: %d\n", avg );

Другие советы

Looks like your script only captures three values (j=0, j=1, j=2), then divides by four.

You have a few problems, here are some suggestions:

  • You're iterating through the inside loop 3 times, however you're saying you have 4 sensors, you should change your for loop to: for (j = 0; j < 4; j++).
  • sum is an array of 3 elements, yet you're accessing an element 1 past the end of the array when calculating avg (sum[3]). This will cause undefined behaviour. sum should be declared as char sum[4] for this reason and the one above.
  • (Optional) sum does not need to be an array in the above example, it can simply be an int.
  • (Optional) If you want to divide an int by 4, use the division operator. The compiler should be better at optimizing the code for your particular architecture than you.

This is how your code could now look, depending on whether you need to keep an array or not:

int sum[4];
int total, j;

for (;;)
{    
   total = 0; /* reset at every iteration of the outside loop */   

   for (j = 0; j < 4; j++) {
      sum[i] = ReadSensor();
      total += sum[i];
   }

   printf("Sonar: %d \r \n", total / 4);
}

OR

int total, j;

for (;;)
{    
   total = 0; /* reset at every iteration of the outside loop */   

   for (j = 0; j < 4; j++)
      total += ReadSensor();

   printf("Sonar: %d \r \n", total / 4);
}

Isn't this

avg=sum[0]+sum[1]+sum[2]+sum[3];

should be

avg=sum[0]+sum[1]+sum[2];

as the loop as well declaration int sum[3]; means we are trying to store only 3 values.

Now if you want 4 and ok with divide operator. There are the new code which should replace the mentioned lines

int sum[4];

for(j=0;j<4;j++)

avg=sum[0]+sum[1]+sum[2]+sum[3]; // this part stays the same
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top