Code doesn't run infinitely!

I'm having trouble with my code!

I'm using an ACS712-30A Current sensor, along with an arduino nano, for a project. I take readings every second but my code doesn't run infinitely. I get 16 readings then it stops, so I have push the reset button on the nano for it to restart!

  1. What exactly could be the problem? - I've been reading and the most talked about problem is memory leak!
  2. How do i resolve the issue? - Be it memory leak or any other problem.

Here is my code for reference!!

#define ACS712 A0

int lastData = 0;
int dataSum = 0;
int Count = 0;

float mean;
float value;
float Voltage;
float Current;
float Power;
float Consumption;

float vpc = 4.8828125; // approx. voltage per count

void setup() {
  Serial.begin (115200);
}

void loop() {

  // If statement to take measurements:
  if (millis() > lastData + 1) {
    dataSum += sq(analogRead(ACS712) - 512);
    Count++;

    lastData = millis();
  }
  // If statement to make calculations.
  if (Count == 1000) {

    // To get the RMS of the data.
    mean = dataSum / Count;
    value = sqrt(mean);

    // To get the Voltage (in volts) consumed.
    Voltage = value * vpc;

    // To get the Current (in amps) consumed.
    Current = Voltage / 66;

    // To get the Power (in watts) consumed.
    Power = Current * 240;

    Consumption = Power / 1000;
    Serial.println(String(Voltage) + "V ");
    Serial.println(String(Current) + "A ");
    Serial.println(String(Power) + "Watts");
    Serial.println(String(Consumption) + "kWh");

    dataSum = 0;
    Count = 0;
  }
}

Thanks...

You have ‘lastData’ set to data type ‘int’

millis() returns ‘unsigned long’

You should review the Blink Without Delay example in the examples section in the IDE.

larryd:
You have ‘lastData’ set to data type ‘int’

millis() returns ‘unsigned long’

You should review the Blink Without Delay example in the examples section in the IDE.

Thanks...
It worked like a charm!!
So happy... It's been a tough week because of that!