Wow, guys... I didn't expect such a lively conversation Great!
To address your doubts...
How do I know that the input is 0?
From software mostly. I did use an oscilloscope to confirm that the input pin is at 0V but to make sure I really got 0 from the A/D converter I read the values from in the input. I added a little bit of code that showed min and max values and they were all zeros. I wasn't assuming that input value was 0, I actually checked it.
I didn't post the entire code because it is rather lengthy because of my PWM code and LCD config. Since this is a math problem rather than an Arduino problem I believed that only a part of the code is needed.
Here's the code:
#include <PWM.h>
#include <LCD.h>
const int currentPin1 = A1; //current sensor 1
const int fan1 = 10;Â Â Â Â // fan1 pin
const int currentPin2 = A2; //current sensor 2
const int fan2 = 9;Â Â Â Â // fan2 pin
int pwm;
int i;
double currentF1 = 0;
double currentMin=0;
double currentMax=0;
double currentMin2=0;
double currentMax2=0;
double currentF2 = 0;
LiquidCrystal lcd(8, 7, 6, 5, 4, 3);
void setup()
{
 lcd.begin(20,2);
 pinMode(fan1, OUTPUT);
 pinMode(fan2, OUTPUT);
 InitTimersSafe();
}
void loop()
{
 lcd.clear();
 for (i = 0; i<256; i++)
  {
   pwmWrite(fan1, i);
   pwmWrite(fan2, i);
   delay(80);
   lcd.setCursor(0,0);
   lcd.print(currentF1);
   currentF1=analogCurrent(currentPin1);
   if(currentF1 < currentMin) currentMin = currentF1;
   lcd.setCursor(6,0);
   lcd.print(currentMin);
   if(currentF1 > currentMax) currentMax = currentF1;
   lcd.setCursor(11,0);
   lcd.print(currentMax);
  Â
   lcd.setCursor(0,1);
   lcd.print((float)currentF2);
   currentF2=analogCurrent(currentPin2);
   if(currentF2 < currentMin2) currentMin2 = currentF2;
   lcd.setCursor(6,1);
   lcd.print(currentMin2);
   if(currentF1 > currentMax2) currentMax2 = currentF2;
   lcd.setCursor(11,1);
   lcd.print(currentMax2);
 Â
   delay(200);
  }
delay(500);
for ( i = 255; i> 0; i--)
  {
   pwmWrite(fan1, i);
   pwmWrite(fan2, i);
   delay(80);
   lcd.setCursor(0,0);
   lcd.print((float)currentF1);
   currentF1=analogCurrent(currentPin1);
   if(currentF1 < currentMin) currentMin = currentF1;
   lcd.setCursor(6,0);
   lcd.print(currentMin);
   if(currentF1 > currentMax) currentMax = currentF1;
   lcd.setCursor(11,0);
   lcd.print(currentMax);
  Â
   lcd.setCursor(0,1);
   lcd.print((float)currentF2);
   currentF2=analogCurrent(currentPin2);
   if(currentF2 < currentMin2) currentMin2 = currentF2;
   lcd.setCursor(6,1);
   lcd.print(currentMin2);
   if(currentF1 > currentMax2) currentMax2 = currentF2;
   lcd.setCursor(11,1);
   lcd.print(currentMax2);
   delay(200);
  }
}
double analogCurrent(int pin)
{
  double current;
  for(int i = 0; i < 100; i++)
  {
    current = current + (0.004887 * analogRead(pin));
  }
  current = current / 10;
  return current;
}
This is a test setup to read the current consumption from two DC fans. The code does a sweep from 0% to 100% and back to 0% PWM and displays current values (now/min/max)
The question here is why changing the code gives different results while hardware remains exactly the same?
Try it on your Arduino and see if you get the same results. With the above code my instant current value is displayed on the LCD as 0.22 (with input at 0V).
After changing the last lines to look like this:
    current = current + (analogRead(pin));
  }
  current = current / 10;
  return current * 0.004883;
The LCD shows 0.00
With about 5V supplied to the analog input pin the results are:
- 55.24 - first code
- 49.97 - after changing the code
Yes, I am aware that I'm taking 100 readings and dividing it by 10 later on. This is on purpose.
What gives guys? Shouldn't the output value be the same if I'm not changing anything else? It's just that one part of the code.
Nick - I'm sure it's me... but why?