Society of Robots - Robot Forum

Software => Software => Topic started by: airman00 on February 06, 2009, 12:34:02 PM

Title: Get Average of 100 ADC Values
Post by: airman00 on February 06, 2009, 12:34:02 PM
Will this code work to average out 100 values that are read of an A2D ?

Code: [Select]
int RSSI[100];

int GetAverageRSSI(void) {
int average;
int counter;
int temp;

counter = 0;

// store 100 RSSI Values into the RSSI array
while (counter < 100) { 
    RSSI[counter]=a2dConvert8bit(1);  //ADC1 is RSSI value
counter++;
}

counter = 0;
temp = 0;

// Get Average
while (counter < 100) { 
    temp = temp + RSSI[counter];
counter++;
}
average = (temp/100);
return average;
}
Title: Re: Get Average of 100 ADC Values
Post by: cosminprund on February 06, 2009, 12:51:47 PM
It would work but it doesn't need to be that complicated. Unless you need the last distinct 100 RSSI values don't store them into the array, just do the sum! This way you'd be saving 100 bytes of memory and there's a chance the code would be a tad more efficient (because only one memory location would be used so it wouldn't need to do pointer arithmetic):

Code: [Select]
int GetAverageRSSI(void) {
  int counter = 0;

  for (int i=0;i<100;i++) counter = counter + a2dConvert8bit(1);
  return counter / 100;
}
Title: Re: Get Average of 100 ADC Values
Post by: TrickyNekro on February 07, 2009, 01:12:11 PM
Yup that's right....
And if you want your code to be even more efficient try using ADC interrupt...
Title: Re: Get Average of 100 ADC Values
Post by: Admin on February 13, 2009, 01:18:28 AM
I'm not entirely sure why you want to average a bunch of RSSI values . . . but for maximum accuracy, put this AVRlib command in right before you start doing ADC readings:

Code: [Select]
//select speed/accuracy of data conversion
a2dSetPrescaler(ADC_PRESCALE_DIV128); // configure ADC scaling

Also to note, the simple act of you physically moving around the transceiver will change the RSSI values. :P
Title: Re: Get Average of 100 ADC Values
Post by: airman00 on February 13, 2009, 06:45:11 AM
I'm not entirely sure why you want to average a bunch of RSSI values

Quote
Also to note, the simple act of you physically moving around the transceiver will change the RSSI values. :P
Yep I know , thats why I set a range.

I got the whole RSSI system to work ,I'll take a video this weekend.