Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
For loop Arduino:为什么这两个循环给出不同的结果?_For Loop_Arduino_Arduino Uno - Fatal编程技术网

For loop Arduino:为什么这两个循环给出不同的结果?

For loop Arduino:为什么这两个循环给出不同的结果?,for-loop,arduino,arduino-uno,For Loop,Arduino,Arduino Uno,我遇到了一个奇怪的问题。据我所知,这两段arduino代码应该给出相同的结果,但它们没有。我将粘贴整个arduino草图,然后粘贴以这种方式运行的两段代码 averageValue1和averageValue2用于计算两个电位计最近5次读数的平均值 出于某种原因,当我在一个For循环中计算两个变量的值时,这两个值似乎会相互影响。意味着averageValue2越高,averageValue1也越高。但是,如果我在两个单独的for循环中计算这两个变量,结果是正确的,并且这些值似乎不会相互影响 我几

我遇到了一个奇怪的问题。据我所知,这两段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(给出正确的结果)

2(给出错误的结果)

在我看来,这两个版本的for循环应该给出相同的结果,但它们没有。这怎么可能


感谢各位(int loop=averageCount;loop>0;loop--){您访问的数组超过了末尾。这是不允许的。在这种情况下,有效的索引应该是
[0-4]你也可以考虑使用A来解决这个问题。谢谢@ JohnnyMopp已经解决了这个问题。这似乎是使用无效数组索引的一个非常奇怪的一致性……就像我说的,由于某种原因,一个变量的值会在修复问题之前影响到另一个变量。这对我来说毫无意义。但它确实解决了问题。nks!
  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;
  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;