Author Topic: Get Average of 100 ADC Values  (Read 3411 times)

0 Members and 1 Guest are viewing this topic.

Offline airman00Topic starter

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 3,650
  • Helpful? 21
  • narobo.com
    • Narobo.com - Mechatronics and related
Get Average of 100 ADC Values
« 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;
}
Check out the Roboduino, Arduino-compatible board!


Link: http://curiousinventor.com/kits/roboduino

www.Narobo.com

Offline cosminprund

  • Robot Overlord
  • ****
  • Posts: 284
  • Helpful? 8
Re: Get Average of 100 ADC Values
« Reply #1 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;
}

Offline TrickyNekro

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 1,208
  • Helpful? 15
  • Hardware and Firmware Designer
    • The Hellinic Robots Portal
Re: Get Average of 100 ADC Values
« Reply #2 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...
For whom the interrupts toll...

Offline Admin

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,704
  • Helpful? 173
    • Society of Robots
Re: Get Average of 100 ADC Values
« Reply #3 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

Offline airman00Topic starter

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 3,650
  • Helpful? 21
  • narobo.com
    • Narobo.com - Mechatronics and related
Re: Get Average of 100 ADC Values
« Reply #4 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.
Check out the Roboduino, Arduino-compatible board!


Link: http://curiousinventor.com/kits/roboduino

www.Narobo.com

 

SMF spam blocked by CleanTalk