ACS712 current sensor sketch! help! [SOLVED]

Hello,
I am trying to get the amp-hour from a circuit of LEDs using the ACS712 current sensor, so I need some help in the sketch. There is some variable type that I am doing wrong and getting the "nan" and "inf" values on the monitor.

here is my sketch and attached is the monitor result:

float sample2 = 0.0;
float val = 0.0;
float actualval = 0.0;
float amps = 0.0;
float totamps = 0.0;
float avgamps = 0.0;
float amphr = 0.0;
float watt = 0.0;
float energy = 0.0;

float milisec = millis(); // calculate time in milliseconds
float time=milisec/1000.0; // convert milliseconds to seconds

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
}

void loop() {
  // taking 150 samples from sensors with a interval of 2msec and then average the samples data collected
  for(int i=0;i<150;i++)
  {
    sample2+=analogRead(A0); //read the current from sensor
    delay(2);
  }
  sample2=sample2/150;
  val =(5.0*sample2)/1024.0;
  actualval =val-2.5; // offset voltage is 2.5v
  amps =actualval*10; // 100mv/A from data sheet, 10 is a factor
  Serial.print("milisec: ");
  Serial.println(milisec);
  Serial.print("time: ");
  Serial.println(time);
  Serial.print("analog: ");
  Serial.println(analogRead(A0));
  Serial.print("Ampere: ");
  Serial.println(amps);

  totamps=totamps+amps; // calculate total amps
  avgamps=totamps/time; // average amps
  amphr=(avgamps*time)/3600.0; // amp-hour
  //watt =voltage*amps; // power=voltage*current
  energy=(watt*time)/3600; // Watt-sec is again convert to Watt-Hr by dividing 1hr(3600sec)
  // energy=(watt*time)/(1000*3600); for reading in kWh
  Serial.print("Avg Amps: ");
  Serial.println(avgamps);
  Serial.print("Total Amps: ");
  Serial.println(totamps);
  Serial.print("AmpH: ");
  Serial.println(amphr);
}

your getting NAN and INF because you are not updating 'time' and 'millisec' in your loop.

I've added those lines to your code (compiles, not tested!):

float sample2 = 0.0;
float val = 0.0;
float actualval = 0.0;
float amps = 0.0;
float totamps = 0.0;
float avgamps = 0.0;
float amphr = 0.0;
float watt = 0.0;
float energy = 0.0;

float milisec = millis(); // calculate time in milliseconds
float time = milisec / 1000.0; // convert milliseconds to seconds

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);

  milisec = millis(); // calculate time in milliseconds
  time = milisec / 1000.0; // convert milliseconds to seconds
}

void loop() {
  // taking 150 samples from sensors with a interval of 2msec and then average the samples data collected
  for (int i = 0; i < 150; i++)
  {
    sample2 += analogRead(A0); //read the current from sensor
    delay(2);
  }
  sample2 = sample2 / 150;
  val = (5.0 * sample2) / 1024.0;
  actualval = val - 2.5; // offset voltage is 2.5v
  amps = actualval * 10; // 100mv/A from data sheet, 10 is a factor
  time = (millis() - milisec) / 1000.0;
  milisec = millis();
  Serial.print("milisec: ");
  Serial.println(milisec);
  Serial.print("Averaging time: ");
  Serial.println(time);
  Serial.print("analog: ");
  Serial.println(analogRead(A0));
  Serial.print("Ampere: ");
  Serial.println(amps);

  totamps = totamps + amps; // calculate total amps
  avgamps = totamps / time; // average amps
  amphr = (avgamps * time) / 3600.0; // amp-hour
  //watt =voltage*amps; // power=voltage*current
  energy = (watt * time) / 3600; // Watt-sec is again convert to Watt-Hr by dividing 1hr(3600sec)
  // energy=(watt*time)/(1000*3600); for reading in kWh
  Serial.print("Avg Amps: ");
  Serial.println(avgamps);
  Serial.print("Total Amps: ");
  Serial.println(totamps);
  Serial.print("AmpH: ");
  Serial.println(amphr);
}

hope that helps....

sherzaad:
your getting NAN and INF because you are not updating 'time' and 'millisec' in your loop.

I've added those lines to your code (compiles, not tested!):

float sample2 = 0.0;

float val = 0.0;
float actualval = 0.0;
float amps = 0.0;
float totamps = 0.0;
float avgamps = 0.0;
float amphr = 0.0;
float watt = 0.0;
float energy = 0.0;

float milisec = millis(); // calculate time in milliseconds
float time = milisec / 1000.0; // convert milliseconds to seconds

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);

milisec = millis(); // calculate time in milliseconds
  time = milisec / 1000.0; // convert milliseconds to seconds
}

void loop() {
  // taking 150 samples from sensors with a interval of 2msec and then average the samples data collected
  for (int i = 0; i < 150; i++)
  {
    sample2 += analogRead(A0); //read the current from sensor
    delay(2);
  }
  sample2 = sample2 / 150;
  val = (5.0 * sample2) / 1024.0;
  actualval = val - 2.5; // offset voltage is 2.5v
  amps = actualval * 10; // 100mv/A from data sheet, 10 is a factor
  time = (millis() - milisec) / 1000.0;
  milisec = millis();
  Serial.print("milisec: ");
  Serial.println(milisec);
  Serial.print("Averaging time: ");
  Serial.println(time);
  Serial.print("analog: ");
  Serial.println(analogRead(A0));
  Serial.print("Ampere: ");
  Serial.println(amps);

totamps = totamps + amps; // calculate total amps
  avgamps = totamps / time; // average amps
  amphr = (avgamps * time) / 3600.0; // amp-hour
  //watt =voltageamps; // power=voltagecurrent
  energy = (watt * time) / 3600; // Watt-sec is again convert to Watt-Hr by dividing 1hr(3600sec)
  // energy=(watttime)/(10003600); for reading in kWh
  Serial.print("Avg Amps: ");
  Serial.println(avgamps);
  Serial.print("Total Amps: ");
  Serial.println(totamps);
  Serial.print("AmpH: ");
  Serial.println(amphr);
}




hope that helps....

oops ! I did not notice that when i made up the sketch. Thanks a lot I am testing now I must leave the circuit running for a while to see if amph will update from 0.00