如何调试为什么Arduino会打破while循环?

时间:2017-10-08 15:08:25

标签: c++ arduino-uno

我首先添加了一个无限循环。 Dugme是循环的变量。但我无法打破循环。这就是为什么,当我进入循环时,我无法退出。

void otoac()
{
    long duration, distance;
    while(dugme==1)
    { 
        int distanceR = 0;
        int distanceL =  0;
        delay(40);
        if(distance<=24)
        {
            moveStop();
            delay(100);
            moveBackward();
            delay(300);
            moveStop();
            delay(200);
            distanceR = lookRight();
            delay(200);
            distanceL = lookLeft();
            delay(200);
            if(distanceR>=distanceL)
            {
                turnRight();
                moveStop();
            }
            else
            {
                turnLeft();
                moveStop();
            }
        }
        else
        {
            moveForward();
        }
        distance = readPing();
    }
}

我有一个代码,当我点击大小写时:'X'它进入自动模式(应用程序)。我在这里有代码,当我点击'X'时,'x'(小x)它需要停止,但它不会停止。这是'x'的代码。

void otokapa()
{
    dugme=0
    motor1.setSpeed(0);
    motor2.run(RELEASE); //turn motor1 off
    motor2.setSpeed(0);
    motor2.run(RELEASE); //turn motor2 off
} 

YouTube上的某个人只给了我这个答案:

  

我在命令中添加了一个while循环,以及与命令的矛盾,这意味着,当没有按下三角形时,汽车将执行的操作,这是什么。

更多代码:

void loop(){
   if(Serial.available() > 0){ 
    command = Serial.read(); 
    Stop(); 
    switch(command){
    case 'F':  
      forward();
      break;
    case 'B':  
       back();
      break;
    case 'L':  
      left();
      break;
    case 'R':
      right();
      break;
    case 'G':  
      onsol();
      break;
    case 'I':  
      onsag();
      break;
    case 'H':  
      arkasag();
      break;
    case 'J':
      arkasol();
      break;
    case 'W':  
      onac();
      break;
    case 'w':  
      onkapa();
      break;
     case 'X':  
      otoac();
      break;
    case 'x':  
      otokapa();
      break;
    }
  } 
}

和dugme:

在所有代码int dugme=1;之上,dugme仅在void otokapa和otoac while(dugme==1)

1 个答案:

答案 0 :(得分:0)

这实质上是你的问题吗?

int dugme = 1; // one and only definition of this (ODR)

void otoac() {
    long duration, distance;
    while(dugme==1) { 
    }
}

void otokapa() {
    dugme=0;
} 

void loop(){
   if(Serial.available() > 0){ 
    command = Serial.read(); 
    Stop(); 
    switch(command){
     case 'X':  
      otoac();
      break;
    case 'x':  
      otokapa();
      break;
    }
  } 
}

可能违反ODR的错误。

如果loopotoac在同一个帖子中运行,您将永远不会再次访问该序列。