0

我正在使用 Arduino 为项目制作相扑机器人。我没有使用 Arduino 代码的经验,但我确实有 java 经验。也就是说,这个机器人有一个边缘检测器来防止它掉下来,一个 L298 驱动器等等。由于我缺乏经验,我不知道 Arduino 的代码如何与方法等一起工作。也就是说,我的主要问题是它如何在延迟的情况下执行我的方法?它是否陷入了没有回报的方法中?不进?执行将在稍后结束?目前,它似乎只是在没有打开或关闭开关的情况下向前运行电机。我也检查了接线。(我为糟糕的提问道歉-stackoverflow处女)。

int motor_forward = 10;
int motor_reverse = 9;
int motor2_forward = 13;
int motor2_reverse = 12;
int edgeDec1 = 7;
//int edgeDec2 = 6;

//the setup routine runs once when you press reset;
void setup(){
 //initialize the digital pin as an output.
 pinMode(motor_forward, OUTPUT);
 pinMode(motor_reverse,OUTPUT);
 pinMode(motor2_forward,OUTPUT);
 pinMode(motor2_reverse,OUTPUT);
 pinMode(edgeDec1,INPUT);

}
//the loop routine runs over and over again forever
void loop(){
  int indicator = random(2);

  delay(5000);//5 second delay
  //loop to prevent another 5 second delay
  while(true){
    if (indicator = 0){
      for(int x = 0; x < random(200); x++){
        Forward();
        EdgeDec();
        delay(10);
      }
    }
    else if ( indicator = 1){
       for(int x = 0; x < random(200); x++){
        TurnLeft();
        EdgeDec();
        delay(10);
      }
    }
     else if ( indicator = 2){
       for(int x = 0; x < random(200); x++){
        TurnRight();
        EdgeDec();
        delay(10);
      }
    }

  }

}
void Forward(){
  //right motor
  digitalWrite(motor_forward,1);//terminal d1 will be high
  digitalWrite(motor_reverse,0);//terminal d2 will be low

  //left motor
  digitalWrite(motor2_forward,1);//terminal d1 will be high
  digitalWrite(motor2_reverse,0);//terminal d2 will be low

}
//going in reverse
void Reverse(){
  //right motor
  digitalWrite(motor_forward,0);//terminal d1 will be low
  digitalWrite(motor_reverse,1);//terminal d2 will be high

  //left motor
  digitalWrite(motor2_forward,0);//terminal d1 will be low
  digitalWrite(motor2_reverse,1);//terminal d2 will be high

}
//rotating left
void TurnLeft(){
  //right motor
  digitalWrite(motor_forward,0);//terminal d1 will be high
  digitalWrite(motor_reverse,1);//terminal d2 will be low

  //left motor
  digitalWrite(motor2_forward,1);//terminal d1 will be high
  digitalWrite(motor2_reverse,0);//terminal d2 will be low

}
void TurnRight(){
  //right motor
  digitalWrite(motor_forward,1);//terminal d1 will be high
  digitalWrite(motor_reverse,0);//terminal d2 will be low

  //left motor
  digitalWrite(motor2_forward,0);//terminal d1 will be high
  digitalWrite(motor2_reverse,1);//terminal d2 will be low

}
void EdgeDec(){
  if(edgeDec1 == 1){
     Reverse();
     delay(700);
     TurnLeft();
     delay(1000);
     Forward();

    }
}
4

2 回答 2

0

您的代码中有几个错误/误解,所以我添加了一些简单的调试,可以在您下载代码后打开工具>串行监视器。在您的 EdgeDec() 函数(函数 = Java 方法)中,您有 if(EdgeDec = 1),但 EdgeDec 只是一个值为 7 的 int。您想要的是读取编号为 7 的引脚的值 - if(digitalRead (edgeDec1) == 低)。

另外,我将您的逻辑从 HIGH (1) 反转为 LOW (0),因为您没有将 digitalRead 引脚绑定到 LOW(使用外部电阻器)或 HIGH(使用 Arduino 的内部电阻器) - 请阅读我在代码中的评论。不确定你在用 while(true) 循环做什么——也许还有一些进一步的调试?无论如何,希望它有帮助...

int motor_forward = 10;
int motor_reverse = 9;
int motor2_forward = 13;
int motor2_reverse = 12;
int edgeDec1 = 7;
//int edgeDec2 = 6;
int indicator; // pulled this out of your loop - askchipbug

String repeatstring; //debugging variable, stops a debug string from repeating in Serial monitor when watching a loop - askchipbug

//the setup routine runs once when you press reset;
void setup(){
  Serial.begin(9600);           // set up Serial library at 9600 bps, using SerialMonitor to debug - askchipbug
  //initialize the digital pin as an output.
  pinMode(motor_forward, OUTPUT);
  pinMode(motor_reverse,OUTPUT);
  pinMode(motor2_forward,OUTPUT);
  pinMode(motor2_reverse,OUTPUT);
  pinMode(edgeDec1,INPUT_PULLUP); // set this high so it doesn't float about - askchipbug
  // you can use the internal 20k pullups with INPUT_PULLUP which means 1 = off, 0 = on
  // or you have to use external pulldown resistors to have 1 = on, 0 = off
  pr("setup completed"); // askchipbug

}
//the loop routine runs over and over again forever
void loop(){
  randomSeed(analogRead(0)); // seeds the random() function with a different number each time the loop runs - askchipbug
  indicator = random(2);  
  pr("indicator: " + String(indicator)); // askchipbug
  pr("5 second delay");
  delay(5000);//5 second delay
  //loop to prevent another 5 second delay
  while(true){
    if (indicator == 0){ //you had this set as indicator = 0 - askchipbug
      pr("indicator: " + String(indicator)); // askchipbug
      for(int x = 0; x < random(200); x++){
        Forward();
        EdgeDec();
        delay(10);
      }
    }
    else if ( indicator = 1){
      pr("indicator: " + String(indicator)); //askchipbug
      for(int x = 0; x < random(200); x++){
        TurnLeft();
        EdgeDec();
        delay(10);
      }
    }
    else if ( indicator = 2){
      pr("indicator: " + String(indicator));// askchipbug
      for(int x = 0; x < random(200); x++){
        TurnRight();
        EdgeDec();
        delay(10);
      }
    }
  }

}
void Forward(){
   //right motor
  pr("forward"); //askchipbug
  digitalWrite(motor_forward,1);//terminal d1 will be high
  digitalWrite(motor_reverse,0);//terminal d2 will be low

  //left motor
  digitalWrite(motor2_forward,1);//terminal d1 will be high
  digitalWrite(motor2_reverse,0);//terminal d2 will be low
}
//going in reverse
void Reverse(){
  pr("reverse"); //askchipbug
  //right motor
  digitalWrite(motor_forward,0);//terminal d1 will be low
  digitalWrite(motor_reverse,1);//terminal d2 will be high

  //left motor
  digitalWrite(motor2_forward,0);//terminal d1 will be low
  digitalWrite(motor2_reverse,1);//terminal d2 will be high
}
//rotating left
void TurnLeft(){
  pr("turn left"); //askchipbug
  //right motor
  digitalWrite(motor_forward,0);//terminal d1 will be high
  digitalWrite(motor_reverse,1);//terminal d2 will be low

  //left motor
  digitalWrite(motor2_forward,1);//terminal d1 will be high
  digitalWrite(motor2_reverse,0);//terminal d2 will be low
}
void TurnRight(){
  pr("turn right"); //askchipbug
  //right motor
  digitalWrite(motor_forward,1);//terminal d1 will be high
  digitalWrite(motor_reverse,0);//terminal d2 will be low

  //left motor
  digitalWrite(motor2_forward,0);//terminal d1 will be high
  digitalWrite(motor2_reverse,1);//terminal d2 will be low
}
void EdgeDec(){
  pr("edge detector: " + String(digitalRead(edgeDec1))); // read pin 7 - askchipbug
  if(digitalRead(edgeDec1) == LOW){ // remember we're using the internal pullups so LOW = on - askchipbug
    pr("edge detected!"); // askchipbug
    Reverse();
    delay(700);
    TurnLeft();
    delay(1000);
    Forward();
  }
}

//simple debug technique - use pr("something"); in your code - askchipbug
void pr(String txt){
  if(repeatstring != txt){
    //if the debug text is different, print it
    Serial.println(txt); //prints the text and adds a newline
    Serial.flush(); //waits for all the data to be printed
    delay(1000); //just pauses the scrolling text for 1 second, make bigger if you want a longer pause
    repeatstring = txt;
  }
}
于 2014-05-19T10:38:10.973 回答
0

Arduino中的delay方法一般来说实现为只是nop在CPU上执行多个s足够的周期来弥补所需的延迟时间。它本质上是一个阻塞调用,因为它只是完全耗尽了 CPU 足够的时间,正如传递给delay.

例如,假设您的处理器以 100hz 运行,然后基本上延迟 1 秒将涉及生成nop处理器将执行的 100 条指令。当然,假设这样的指令恰好需要 1 个时钟周期。鉴于 Arduino Uno 使用 ATMEGA328p 微处理器,您可能需要查看该数据表以了解更多信息。

于 2014-05-19T04:36:42.377 回答