我正在使用 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();
}
}