I'm trying to do a moving average with c++ to display a sensor data moving average, i want to do the moving average each 2 seconds with the last 15 values. This is what I'm trying but the array don't update correctly and the last value of the array takes stranges values. How can I do it? Thanks in advance
void loop()
{
count = 0;
int suma = 0;
int mm[15];
int suma2;
for (int x = 0; x < 15; x++) {
count++;
Serial.print("[");
Serial.print(x);
Serial.print("] ");
mm[x] = sensor.getTemperature();
suma += mm[x];
Serial.println(mm[x]);
delay(2000);
}
Serial.print("------------------------The first moving average is = ");
Serial.println(suma / 15);
delay(1000);
Serial.print("------------------------The first complete array is--------------------------- ");
for (int i = 0; i < 15; i++) {
Serial.print("[");
Serial.print(i);
Serial.print("] ");
Serial.println(mm[i]);
}
while (true) {
for (int y = 0; y < 15; y++) {
mm[y] = mm[y + 1];
}
mm[0] = sensor.getTemperature();
delay(2000);
Serial.println("----------------------------The next complete array is------------------------------");
for (int z = 0; z < 15; z++) {
Serial.print("[");
Serial.print(z);
Serial.print("]");
Serial.println(mm[z]);
}
for (int w = 0; w < 14; w++) {
suma2 = 0;
suma2 += mm[w];
}
Serial.print("------------------------The next moving average is = ");
Serial.println(suma2 / 15);
delay(2000);
}
}
And this is the output:
13:25:40.167 -> [1] 21
13:25:42.149 -> [2] 21
13:25:44.159 -> [3] 21
13:25:46.140 -> [4] 22
13:25:48.145 -> [5] 22
13:25:50.173 -> [6] 22
13:25:52.162 -> [7] 21
13:25:54.186 -> [8] 21
13:25:56.162 -> [9] 21
13:25:58.196 -> [10] 21
13:26:00.198 -> [11] 21
13:26:02.174 -> [12] 21
13:26:04.189 -> [13] 21
13:26:06.194 -> [14] 22
13:26:08.176 -> ------------------------The first moving average is = 21
13:26:09.179 -> ------------------------The first complete array is--------------------------- [0] 21
13:26:09.179 -> [1] 21
13:26:09.179 -> [2] 21
13:26:09.179 -> [3] 21
13:26:09.179 -> [4] 22
13:26:09.179 -> [5] 22
13:26:09.179 -> [6] 22
13:26:09.179 -> [7] 21
13:26:09.179 -> [8] 21
13:26:09.179 -> [9] 21
13:26:09.179 -> [10] 21
13:26:09.226 -> [11] 21
13:26:09.226 -> [12] 21
13:26:09.226 -> [13] 21
13:26:09.226 -> [14] 1073479292
13:26:11.180 -> ----------------------------The next complete array is------------------------------
13:26:11.216 -> [0]22
13:26:11.216 -> [1]21
13:26:11.216 -> [2]21
13:26:11.216 -> [3]22
13:26:11.216 -> [4]22
13:26:11.216 -> [5]22
13:26:11.216 -> [6]21
13:26:11.216 -> [7]21
13:26:11.216 -> [8]21
13:26:11.216 -> [9]21
13:26:11.216 -> [10]21
13:26:11.216 -> [11]21
13:26:11.216 -> [12]21
13:26:11.216 -> [13]1073479292
13:26:11.216 -> [14]1073479292
13:26:11.216 -> ------------------------The next moving average is = 71565286
13:26:15.193 -> ----------------------------The next complete array is------------------------------
13:26:15.240 -> [0]22
13:26:15.240 -> [1]21
13:26:15.240 -> [2]22
13:26:15.240 -> [3]22
13:26:15.240 -> [4]22
13:26:15.240 -> [5]21
13:26:15.240 -> [6]21
13:26:15.240 -> [7]21
13:26:15.240 -> [8]21
13:26:15.240 -> [9]21
13:26:15.240 -> [10]21
13:26:15.240 -> [11]21
13:26:15.240 -> [12]1073479292
13:26:15.240 -> [13]1073479292
13:26:15.240 -> [14]1073479292
13:26:15.240 -> ------------------------The next moving average is = 71565286
13:26:19.238 -> ----------------------------The next complete array is------------------------------
13:26:19.238 -> [0]22
13:26:19.238 -> [1]22
13:26:19.238 -> [2]22
13:26:19.238 -> [3]22
13:26:19.238 -> [4]21
13:26:19.238 -> [5]21
13:26:19.238 -> [6]21
13:26:19.238 -> [7]21
13:26:19.238 -> [8]21
13:26:19.238 -> [9]21
13:26:19.238 -> [10]21
13:26:19.238 -> [11]1073479292
13:26:19.238 -> [12]1073479292
13:26:19.238 -> [13]1073479292
13:26:19.238 -> [14]1073479292
13:26:19.238 -> ------------------------The next moving average is = 71565286