Arduino Loop 无法识别中断条件?

Arduino Loop not recognizing break condition?

我目前有一个嵌套的 for 循环系统,它正在向电机写入命令并从连接的编码器读取。由于某种原因,程序不会使用条件跳出循环。我使用一些打印语句进行了一些调试,它清楚地识别出 PWMcount 大于 76,因为它会打印到我允许它计数的范围内。非常感谢任何建议,谢谢!

for(int PWMcount = 58;PWMcount < 76;) { //10-20 RAMP
        for (int counter = 1; counter < 4;) {
          if (counter != 3) {
              finalTime = millis();                                                           
              float newPos;          
              newPos = encoder.read();   
              if (newPos != oldPos) {                                                    
                deltaTime = (finalTime - initialTime);                            
                deltaPos = (newPos-oldPos);                                       
                oldPos = newPos;                                                    
                initialTime = finalTime;                              
                //Serial.println(deltaPos/deltaTime);                    
              }
              counter = counter+1;
              delay(100);
             // Serial.println(PWMcount);
          }
          else {
              
              finalTime = millis();                                                    //catches time for beginning of each loop iteration           
              float newPos;          
              newPos = encoder.read();   
              if (newPos != oldPos) {                                                    //Checks for encoder movement
                deltaTime = (finalTime - initialTime);                            //calculates deltaT in milliseconds
                deltaPos = (newPos-oldPos);                                       //sets change in position from one iteration to the next
                oldPos = newPos;                                                       //Sets old to new to fail if statement unless movement is detected.
                initialTime = finalTime;                                 //sets time at beginning of loop to time of ending last loop in order to correctly calculate deltaT in the next iteration
                //Serial.println(deltaPos/deltaTime);                      //Prints rate of the encoder disk in deg/ms (Confirmed by manually taking data and seeing that 1 Rotation outputs values from 0-360
                  
              }
              counter = 1;
              PWMcount=PWMcount+1;   
              analogWrite(RPWM_Output, PWMcount); // Increases Motor PWM in accordance to loop
          }
          Serial.println(PWMcount);       // UNCOMMENT TO TROUBLESHOOT PWM
          delay(100);
        }
      } 

for 循环永远不会完成:

for (int counter = 1; counter < 4;)

循环体是一个 if 语句。在循环的最后一次迭代中,采用 else 分支,该分支将循环变量 counter 设置回 1,因此表达式 counter < 4 始终为真,这样循环就永远不会退出。

由于它永远不会退出,因此代码永远不会到达测试外循环条件的位置,也不会到达外循环根本不会分支回开头的位置。

解决这种情况的一种可能方法是结合两个循环条件,使其成为一个循环:

for(int PWMcount = 58, counter = 1; PWMcount < 76;) { //10-20 RAMP
  if (counter != 3) {
      finalTime = millis();                                                           
      float newPos;          
      newPos = encoder.read();   
      if (newPos != oldPos) {                                                    
        deltaTime = (finalTime - initialTime);                            
        deltaPos = (newPos-oldPos);                                       
        oldPos = newPos;                                                    
        initialTime = finalTime;                              
        //Serial.println(deltaPos/deltaTime);                    
      }
      counter = counter+1;
      delay(100);
     // Serial.println(PWMcount);
  }
  else {
      
      finalTime = millis();                                                    //catches time for beginning of each loop iteration           
      float newPos;          
      newPos = encoder.read();   
      if (newPos != oldPos) {                                                    //Checks for encoder movement
        deltaTime = (finalTime - initialTime);                            //calculates deltaT in milliseconds
        deltaPos = (newPos-oldPos);                                       //sets change in position from one iteration to the next
        oldPos = newPos;                                                       //Sets old to new to fail if statement unless movement is detected.
        initialTime = finalTime;                                 //sets time at beginning of loop to time of ending last loop in order to correctly calculate deltaT in the next iteration
        //Serial.println(deltaPos/deltaTime);                      //Prints rate of the encoder disk in deg/ms (Confirmed by manually taking data and seeing that 1 Rotation outputs values from 0-360
          
      }
      counter = 1;
      PWMcount=PWMcount+1;   
      analogWrite(RPWM_Output, PWMcount); // Increases Motor PWM in accordance to loop

      Serial.println(PWMcount);       // UNCOMMENT TO TROUBLESHOOT PWM
      delay(100);
  }
}

一些注意事项:

  • 循环条件不会检查 counter 值,因为循环体避免了比较。 counter 变量总是小于 4.
  • 我将外循环的最后两行移动到 if 语句的 else 分支中。它们从未在您的原始代码中执行,因此您可能只想完全删除它们。但是,如果意图是执行这两行,则内循环到达其末尾的每一行(即 counter3),则此更改符合该意图。