I'm using this fairly basic code to calculate the average of some values (1 reading per minute, average over 5 readings)
The first cycle works fine - then things go crazy
tA = 24
tA = 24
tA = 24
tA = 24
tA = 24
then....
tA = 120
tA = 60
tA = 40
tA = 30
tA = 24
and then keeps going like this!
Here are the relevant bits of code for the average calcs:
const int TnumReadings = 5;
int Treadings[TnumReadings]; // the readings from temp sensor
int Ttotal = 0; // the running total
int TAverage = 0; // the average
int Tindex = 0;
for (int thisTempReading = 0; thisTempReading < TnumReadings; thisTempReading++)
Treadings[thisTempReading] = 0;
void calcAverages(){
Ttotal= Ttotal - Treadings[Tindex]; // subtract the last TEMP reading:
Treadings[Tindex] = temp; // read TEMP value:
Ttotal= Ttotal + Treadings[Tindex]; // add the TEMP reading to the total:
Tindex = Tindex + 1; // advance to the next position in the array:
TAverage = Ttotal / Tindex ; // calculate the average:
if (Tindex >= TnumReadings) // if we're at the end of the array...
{
Tindex = 0; // ...wrap around to the beginning:
}
Serial.print(" tA = ");
Serial.println(TAverage);
}
I am sure that I am missing something obvious here but no amount of coffee seems to be helping LOL!
Stocky