Unexpected variable behaviour

Hello,

I wrote a simple code to average out analogRead values during exact timing periods.

const int mVperAmp = 100; // use 185 for 5A Module, and 66 for 30A Module
float Vref  = 5.00; //read your Vcc voltage,typical voltage should be 5000mV(5.0V)
//float Vref  = 1.10;

boolean firstFlag = true;
boolean timerResetFlag = true;

float measValue_1[10];
float measAverage_1 = 0.0;
float measValue_2[10];
float measAverage_2 = 0.0;
float mAh = 0.0;

int sampleCounter = 0;
int secCounter = 0;
int minCounter = 0;
int adcValue[2] = {0, 0};
int prevAdc[2] = {0, 0};
int runAdc[2] = {0, 0};

unsigned long sampleStarted = 0;
unsigned long averageStarted = 0;
unsigned long averageComplete = 0;
unsigned long averageTime = 0;

void setup()
{
  analogReference(DEFAULT);
  //analogReference(INTERNAL);
  Serial.begin(9600);
  sampleStarted = millis();
}

void loop()
{
  if ( (millis() - sampleStarted) < 90)
  {
    if (firstFlag == true)
    {
      sampleStarted = millis();
      if (sampleCounter == 0)
      {
        averageStarted = millis();
        Serial.print(averageStarted);
        Serial.println("ms, Start occured.");
      }
      prevAdc[1] = analogRead(A0);
      prevAdc[2] = analogRead(A1);
      firstFlag = false;
    }
    adcValue[1] = analogRead(A0);
    runAdc[1] = round( float(prevAdc[1] + adcValue[1]) / 2.0);
    prevAdc[1] = runAdc[1];
    adcValue[2] = analogRead(A1);
    runAdc[2] = round( float(prevAdc[2] + adcValue[2]) / 2.0);
    prevAdc[2] = runAdc[2];
  }
  else
  {

    //measValue_1[sampleCounter] = ( (float(runAdc) / 1024.0) * Vref - (Vref / 2.0)) * 5.4054;  //Sensitivity:185mV/A, 0A @ Vcc/2
    measValue_1[sampleCounter] = (float(runAdc[1]) / 1024.0);
    measValue_2[sampleCounter] = (float(runAdc[2]) / 1024.0);
    runAdc[1] = 0;
    runAdc[2] = 0;
    sampleCounter += 1;

    if (sampleCounter != 10)
    {
      while ((millis() - sampleStarted) < 100)
      {
        //wait for complete 100ms
      }
    }

    else
    {
      //Serial.println("average completed.");
      secCounter += 1;
      if (secCounter == 60)
      {
        minCounter += 1;
        secCounter = 0;
      }
      for (int i = 0; i <= 9; i++)
      {
        //Serial.println(measValue_1[i]);
        measAverage_1 += measValue_1[i];
        measValue_1[i] = 0.0;
        measAverage_2 += measValue_2[i];
        measValue_2[i] = 0.0;
      }

      sampleCounter = 0;
      measAverage_1 = measAverage_1 / 10.0;
      measAverage_2 = measAverage_2 / 10.0;
//      printTime();
//      Serial.print(measAverage_1 * Vref, 5); Serial.print("V --> ");
//      Serial.print(measAverage_2 * Vref, 5); Serial.print("V --> ");
//      Serial.print(((1 - ((measAverage_2 / measAverage_1) / 2)) * 100), 3); Serial.println("% error");

      //      if (measAverage_1 >= 0.1)
      //      {
      //        if (timerResetFlag == true)
      //        {
      //          secCounter = 0;
      //          minCounter = 0;
      //          timerResetFlag = false;
      //        }
      //        Serial.print(" --> ");
      //        mAh += (measAverage_1 * 10000) / 3600;
      //        Serial.print(mAh / 10, 4); Serial.println(" mAh");
      //      }
      //      else
      //      {
      //        mAh = 0.0;
      //        if (timerResetFlag == false)
      //        {
      //          secCounter = 0;
      //          minCounter = 0;
      //          timerResetFlag = true;
      //        }
      //        Serial.println();
      //      }
      
      measAverage_1 = 0.0;
      measAverage_2 = 0.0;
      while ((millis() - sampleStarted) < 100)
      {
        //wait for complete 100ms
      }
      averageTime = (millis() - averageStarted);
      Serial.print(millis());Serial.print(" ");Serial.print(averageStarted);Serial.print(" ");
      Serial.print(averageTime); Serial.println(" ms");
    }

    firstFlag = true;
    sampleStarted = millis();
  }
}

void printTime()
{
  if (minCounter < 10)
  {
    Serial.print("0");
  }
  Serial.print(minCounter);
  Serial.print(":");
  if (secCounter < 10)
  {
    Serial.print("0");
  }
  Serial.print(secCounter); Serial.print(" --> ");

}

My concern is, that averageStarted variable acts unexpectedly. After firstFlag condition is checked, it seems OK, I get "Start occured." message every 1000ms. But at the end of the loop, when I try to calculate the averageTime, the averageStarted seems to have zero value first, then after every 66 second, it increments with 2^16 (unsigned int?).

I don't get it, besides averageStarted = millis(); the code is not intended to modify the averageStarted variable.
Can somebody tell me what could cause this?

Thanks in advance,
Arpad

You declare these variables as arrays of size 2

int adcValue[2] = {0, 0};
int prevAdc[2] = {0, 0};
int runAdc[2] = {0, 0};

But your code references and assigned to the 3rd element

      prevAdc[1] = analogRead(A0);
      prevAdc[2] = analogRead(A1);

so you are stomping on some other memory, probably your variable that is acting odd.

To add to what blh64 said, arrays are zero indexed. If you have an array of two elements like your adcValue, the first element is adcValue[0] and the second element is adcValue[1].

Oh, what a dumb mistake. Thanks for the open-eyed inspection.
I used array indexing correctly for e.g. measValue_1[], but managed to take a wrong turn with adc arrays. Thanks again, lesson learned.