for-loop - Arduino:为什么这两个循环会给出不同的结果?
问题描述
我遇到了一个奇怪的问题。据我所知,这两段 arduino 代码应该给出相同的结果,但事实并非如此。我将粘贴整个 arduino 草图,然后粘贴以这种方式运行的两段代码。
averageValue1 和 averageValue2 应该计算两个电位器的最后 5 个读数的平均值。
出于某种原因,当我在一个 for 循环中计算两个变量的值时,这两个值似乎相互影响。这意味着averageValue2 越高,averageValue1 也越高。但是,如果我在两个单独的 for 循环中计算两个变量,则结果是正确的,并且这些值似乎不会相互影响。
我几乎可以肯定代码中的这种更改不应该影响结果,但是有人可以告诉我为什么会这样吗?
这是整个代码草图:
/*
Analog input, analog output, serial output
Reads an analog input pin, maps the result to a range from 0 to 255
and uses the result to set the pulsewidth modulation (PWM) of an output pin.
Also prints the results to the serial monitor.
The circuit:
potentiometer connected to analog pin 0.
Center pin of the potentiometer goes to the analog pin.
side pins of the potentiometer go to +5V and ground
LED connected from digital pin 9 to ground
created 29 Dec. 2008
modified 9 Apr 2012
by Tom Igoe
This example code is in the public domain.
*/
// These constants won't change. They're used to give names
// to the pins used:
const int analogInPin1 = A0; // Analog input pin that the potentiometer is attached to
const int analogInPin2 = A1; // Analog input pin that the potentiometer is attached to
const int analogInPin3 = A2; // Analog input pin that the potentiometer is attached to
const int analogOutPin = 9; // Analog output pin that the LED is attached to
int sensorValue1 = 0; // value read from the pot
int sensorValue2 = 0; // value read from the pot
int sensorValue3 = 0; // value read from the pot
int outputValue1 = 0; // value output to the PWM (analog out)
int outputValue2 = 0; // value output to the PWM (analog out)
int outputValue3 = 0; // value output to the PWM (analog out)
void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
}
int averageCount = 5;
int lastFiveValues1 [5];
int lastFiveValues2 [5];
int averageValue1;
int averageValue2;
void loop() {
// read the analog in value:
sensorValue1 = analogRead(analogInPin1);
sensorValue2 = analogRead(analogInPin2);
sensorValue3 = analogRead(analogInPin3);
// map it to the range of the analog out:
outputValue1 = map(sensorValue1, 0, 1023, 140, -140);
outputValue2 = map(sensorValue2, 0, 1023, 140, -140);
outputValue3 = map(sensorValue3, 0, 1023, 140, -140);
averageValue1 = 0;
averageValue2 = 0;
for (int loop = averageCount; loop > 0 ; loop--) {
lastFiveValues1[loop] = lastFiveValues1[loop - 1];
averageValue1 += lastFiveValues1[averageCount - loop];
}
for (int loop = averageCount; loop > 0 ; loop--) {
lastFiveValues2[loop] = lastFiveValues2[loop - 1];
averageValue2 += lastFiveValues2[averageCount - loop];
}
lastFiveValues1[0] = outputValue1;
lastFiveValues2[0] = outputValue2;
averageValue1 = averageValue1 / averageCount;
averageValue2 = averageValue2 / averageCount;
/* // change the analog out value:
analogWrite(analogOutPin, outputValue1);
analogWrite(analogOutPin, outputValue2);
analogWrite(analogOutPin, outputValue3);*/
// print the results to the serial monitor:
String outputString = (String)averageValue1 + " " + averageValue2;
Serial.println(outputString);
// wait 2 milliseconds before the next loop
// for the analog-to-digital converter to settle
// after the last reading:
delay(50);
}
这是 for 循环的两个版本:
1(给出正确的结果)
averageValue1 = 0;
averageValue2 = 0;
for (int loop = averageCount; loop > 0 ; loop--) {
lastFiveValues1[loop] = lastFiveValues1[loop - 1];
averageValue1 += lastFiveValues1[averageCount - loop];
lastFiveValues2[loop] = lastFiveValues2[loop - 1];
averageValue2 += lastFiveValues2[averageCount - loop];
}
lastFiveValues1[0] = outputValue1;
lastFiveValues2[0] = outputValue2;
averageValue1 = averageValue1 / averageCount;
averageValue2 = averageValue2 / averageCount;
2(给出错误的结果)
averageValue1 = 0;
averageValue2 = 0;
for (int loop = averageCount; loop > 0 ; loop--) {
lastFiveValues1[loop] = lastFiveValues1[loop - 1];
averageValue1 += lastFiveValues1[averageCount - loop];
lastFiveValues2[loop] = lastFiveValues2[loop - 1];
averageValue2 += lastFiveValues2[averageCount - loop];
}
lastFiveValues1[0] = outputValue1;
lastFiveValues2[0] = outputValue2;
averageValue1 = averageValue1 / averageCount;
averageValue2 = averageValue2 / averageCount;
对我来说,这两个版本的 for 循环似乎应该给出相同的结果,但事实并非如此。怎么会这样?
多谢你们
解决方案
推荐阅读
- webrtc - Azure 上的 Coturn 负载均衡
- web - 为已发布二进制文件的公共存储库寻找解决方案,可使用 wget 工具恢复
- laravel - Laravel Lumen 雄辩的左连接返回连接表数据而不是主数据
- python - Snakemake 使用所有样本作为 porechop 的一个输入
- python - Dict:按多个键+降序/升序排序
- gdb - 在 gdb 中运行带有全屏选项(例如 pudb)的 python 脚本失败
- python - 在 Windows 10 Powershell 上使用 Python 3.9.2 在 VS Code 中的源 Pylance 无法解析/无法解析导入
- python - Airflow + Dask:任务环境识别
- apache - 是否可以在将处理程序添加到 Apache conf 文件之前检查它是否存在?
- python - 在嵌套列表中查找多个重复项,然后在保留最新条目的同时删除它们