Pages: [1]   Go Down
Author Topic: Analog input not allowing full 1024 bits  (Read 673 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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
}


* Capture.JPG (18.97 KB, 251x247 - viewed 7 times.)
Logged

0
Offline Offline
Faraday Member
**
Karma: 16
Posts: 2855
ruggedcircuits.com
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
AVGvalue = (AVGvalue+50)/100;

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

Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Faraday Member
**
Karma: 16
Posts: 2855
ruggedcircuits.com
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Indeed it does, thanks so much for your help RC.  smiley
Logged

Montreal
Offline Offline
Faraday Member
**
Karma: 29
Posts: 2590
Per aspera ad astra.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: