使用 arduino 在 C++ 中移动平均

Moving average in C++ with arduino

我正在尝试使用 C++ 计算移动平均线以显示传感器数据移动平均线,我想每 2 秒使用最后 15 个值计算移动平均线。这就是我正在尝试的,但数组没有正确更新,并且数组的最后一个值采用了奇怪的值。我该怎么做?提前致谢

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);
    }
}

这是输出:

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

我建议创建一个循环缓冲区,您可以在其中填充数据。我使用了您的整数,请确保这是合适的数据类型。

#define BUF_SIZE 15

float array_calculate_avg(int * buf, int len);

int   buf[BUF_SIZE]   = {0};
int   buf_index       = 0;
float buf_avg         = 0.0;

void
loop(void)
{
    // Reset the index and start over.
    //
    if (BUF_SIZE == buf_index)
    {
        buf_index = 0;
    }

    buf[buf_index++] = sensor.getTemperature();

    buf_avg = array_calculate_avg(buf, BUF_SIZE);

    Serial.print(buf_avg, 4);

    delay(2000);
}

float
array_calculate_avg(int * buf, int len)
{
    int sum = 0;

    for (int i = 0; i < len; ++i)
    {
        sum += buf[i];
    }

    return ((float) sum) / ((float) len);
}