Go Down

Topic: Analog input not allowing full 1024 bits (Read 724 times) previous topic - next topic

bfisher36

Hi, I am using an Arduino Ethernet and never payed attention to the number of bits that come from the ADC until today when my Pachube graph started flat-lining.  So I started looking into it and found that my Arduino is only reading a maximum of 1012 bits from the ADC. Not like I need the extra 50mV of detection, but I am just curious as to why this might be happening with my Arduino.  I am wondering if anyone else has ever run into this. 

It's important to note that I am doing an averaging equation from a collection of 100 ADC readings as well. Here is the code for that:

Code: [Select]

void avg_sample(int pv_type) {
  int i, j, analog_value[100];
  AVGvalue = 0;
 
  while(i < 100) {
    analog_value[i] = analogRead(pv_type); //Store value into array of 100 (this seems to be close to the maximum)
    i++;
  }
   
  for(j = 0; j < 99; j++) { //Add the 100 numbers in the array up
    AVGvalue = (AVGvalue + analog_value[j]);
  }
 
  AVGvalue = (AVGvalue / 100); //Average by dividing, store in AVGvalue
}


RuggedCircuits

You're only averaging 99 values. Your loop should go up to j<100, not j<99. Also, dividing by 100 is rounding down. You should add 50 first to implement rounding:

Code: [Select]
AVGvalue = (AVGvalue+50)/100;

--
The Flexible MIDI Shield: MIDI IN/OUT, stacking headers, your choice of I/O pins

bfisher36

Ah I see!  Yes that makes sense. I am not sure I understand the reason for adding 50 though? 

RuggedCircuits

It's just for rounding. Imagine you added up 100 numbers and got 9999. Printing out "9999/100" would print 99 since the C/C++ language just discards any fractional part in an integer/integer division.

If you first add 50 you get "(9999+50)/100" --> 10049/100 --> 100, which is a lot closer to the "true" answer of 99.99.

So your '1012' number may be artificially low because of rounding as well (although not adding in the 100th sample probably has more effect!)

--
The Gadget Shield: accelerometer, RGB LED, IR transmit/receive, speaker, microphone, light sensor, potentiometer, pushbuttons

bfisher36

Indeed it does, thanks so much for your help RC.  :)

Magician

How you declare your AVGvalue?
1023 x 100 = more than integer could fit, has to be long.

Go Up