I am having an issue that I can't seem to get my head wrapped around. What I am trying to accomplish is to have a minutes worth of sensor data in an array (reading every 5 seconds) so I'll have 12 values in this after one minute.
I want to keep the array to 12 values so I need to keep moving all the values in it down one index per new value hitting index [0] to make sure that each reading has a lifetime of 60 seconds on the array. Here is my code so far:
void add_readings_to_averages() {
// At this point, I am moving values down the array to make room at the beginning.
for (int a = 11; a >= 0; a--) {
Serial.print("The value at temperature_c_minute_avg[");
Serial.print(a);
Serial.print("] is ");
Serial.print(temperature_c_minute_avg[a]);
Serial.println();
if (a == 11) {
Serial.println("At 12 so need to overwrite this value");
}
if (a <= 10) {
Serial.print("a is ");
Serial.println(a);
temperature_c_minute_avg[a] = temperature_c_minute_avg[a + 1]; // This moves each entry down one index in the array creating an empty space at index[0]
}
if (a == 0) {
temperature_c_minute_avg[a] = temperature_c;
}
}
I've loaded my array at the time of initialization with some dummy data so I could see what was going on. First run displays as you would expect.
The value at temperature_c_minute_avg[11] is 120.00
At 12 so need to overwrite this value
The value at temperature_c_minute_avg[10] is 110.00
a is 10
The value at temperature_c_minute_avg[9] is 100.00
a is 9
The value at temperature_c_minute_avg[8] is 90.00
a is 8
The value at temperature_c_minute_avg[7] is 80.00
a is 7
The value at temperature_c_minute_avg[6] is 70.00
a is 6
The value at temperature_c_minute_avg[5] is 60.00
a is 5
The value at temperature_c_minute_avg[4] is 50.00
a is 4
The value at temperature_c_minute_avg[3] is 40.00
a is 3
The value at temperature_c_minute_avg[2] is 30.00
a is 2
The value at temperature_c_minute_avg[1] is 20.00
a is 1
The value at temperature_c_minute_avg[0] is 10.00
a is 0
2018/11/2 11:4:39 - Running for a total of 0 hours 0 minutes and 7 seconds
But the second time through the loop() my array is trashed.
The value at temperature_c_minute_avg[11] is 120.00
At 12 so need to overwrite this value
The value at temperature_c_minute_avg[10] is 120.00
a is 10
The value at temperature_c_minute_avg[9] is 120.00
a is 9
The value at temperature_c_minute_avg[8] is 120.00
a is 8
The value at temperature_c_minute_avg[7] is 120.00
a is 7
The value at temperature_c_minute_avg[6] is 120.00
a is 6
The value at temperature_c_minute_avg[5] is 120.00
a is 5
The value at temperature_c_minute_avg[4] is 120.00
a is 4
The value at temperature_c_minute_avg[3] is 120.00
a is 3
The value at temperature_c_minute_avg[2] is 120.00
a is 2
The value at temperature_c_minute_avg[1] is 120.00
a is 1
The value at temperature_c_minute_avg[0] is 0.00
a is 0
2018/11/2 11:4:44 - Running for a total of 0 hours 0 minutes and 12 seconds
The entire array was overwritten with the last value and the new value was not added. The array itself is setup as a global variable. The initialization at the top.
float temperature_c_minute_avg[12] = {10.0,20.0,30.0,40.0,50.0,60.0,70.0,80.0,90.0,100.0,110.0,120.0};
What am I missing here? Been staring at this for some time, but this is the first time I've tried to use an Arduino array so I can totally believe the issue is PBKAC! :)