Arduino:为什么这两个循环会产生不同的结果?

时间:2020-01-31 20:36:23

标签: for-loop arduino arduino-uno

我遇到了一个奇怪的问题。据我所知,这两段arduino代码应该给出相同的结果,但事实并非如此。我将粘贴整个arduino草图,然后粘贴以这种方式运行的两段代码。

averageValue1和averageValue2应该用来计算两个电位计的最后5个读数的平均值。

由于某种原因,当我在一个for循环中计算BOTH变量的值时,这两个值似乎相互影响。意味着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循环版本应该给出相同的结果,但事实并非如此。怎么会这样?

谢谢大家

0 个答案:

没有答案