Go Down

Topic: Why does "for" not work (Read 1 time) previous topic - next topic

KenH

Feb 17, 2010, 03:54 am Last Edit: Feb 17, 2010, 03:55 am by KenH Reason: 1
I jumpered the Arduino voltage of 5.0VDC direct to analog pin #5 (m168 pin $28).

With the following code, if I read the input pin a single time as in

Code: [Select]
voltraw - analogRead(volt_pin);

the result is exactly 5.0 vdc which is exactly what I read with the DVM on the pin.

BUT if I do the following I get 5.26 and the volt_pin gives 1076 as the average.

Code: [Select]
// variables for input pin and control LED
#include <LiquidCrystal.h>

int volt_pin = 5;  //pin 24

int voltraw = 0;
int voltP = 0;
float volt = 0.0;

LiquidCrystal lcd(7, 6, 5, 4, 3, 2); // (RS, Enable, data lines)this meets the include LiquidCrystal.h above

void setup()
{
 pinMode(volt_pin, INPUT); // declaration of pin modes
 lcd.begin(16,2); // setup to send to LCD with 16 columns and 2 lines.
}
void loop(){
 {
  for (int i = 0; i < 20; i++) //this is to setup the following.
  voltraw = voltraw+analogRead(volt_pin); //this is to read input pin 20 times and add the result.
      voltraw = voltraw / 20;  // this divides by 20 to get the average of 20 reads.
   volt = ((voltraw * 5.0) / 1023);  //this should cover the voltraw to actual volts

   lcd.setCursor(0,0);
   lcd.print("V=");
   lcd.print(volt, 2);  //to print the calculated volts to 2 decimals

   lcd.setCursor(0,1);
   lcd.print("Vraw=");
   lcd.print(voltraw);  //this prints the input in bits.

   delay(300);
 }
}


Comments please to help me understand why if 1023 is read in a single time it comes out at 1023, but if it's read 20 times and divided by 20 it comes out at 1076. If I do the read at 5 or 10, it's even more off. and at 100 times it's really off.

Is there that much error in the ADC?  OR am I doing something wrong with the code?

Thanks for any help and info.

Ken H>


KenH

#2
Feb 17, 2010, 04:32 am Last Edit: Feb 17, 2010, 04:39 am by KenH Reason: 1
Osgeld - thanks for the link, but that is where I got my info.  From what I can see, it's exactly the same.  I can "make" the result correct by fudging the default voltage setting to 4.8 rather than the measured 5.0.

Am I missing something?  

[edit]if you are referring to the "x" that is called (is that the correct term?) in the println(x); line, it won't compile in my sketch. I must have "volt" in that place.

I expect I'm missing something - darn if I can see it.
[/edit]

Ken H.

BDub

Ken,

You have a compounding problem... the issue is not your For() loop, it just looks weird because you didn't use brackets.. but it works fine for one instruction.

The issue is that you never clear voltraw, before you start adding up your 20 reads.  The first time through the loop it's fine, but successive times it is over flowing your INT variable, and then dividing by 20 still.

Just put voltraw = 0; before your For() loop.

Coding Badly

#4
Feb 17, 2010, 05:12 am Last Edit: Feb 17, 2010, 05:12 am by bcook Reason: 1
One change in setup...

Code: [Select]
void setup()
{
[glow]/* Remove this line of code.  It changes the digital pin 5 to an input which may interfere with the LCD
 pinMode(volt_pin, INPUT); // declaration of pin modes
*/[/glow]
 lcd.begin(16,2); // setup to send to LCD with 16 columns and 2 lines.
}


Depending on how the number of times through loop, this change will make a very big difference in the result...

Code: [Select]
void loop(){
 {
[glow]   voltraw = 0;[/glow]
  for (int i = 0; i < 20; i++) //this is to setup the following.
  voltraw = voltraw+analogRead(volt_pin); //this is to read input pin 20 times and add the result.

Go Up