我使用Arduino製作項目的相撲機器人。我沒有使用Arduino代碼的經驗,但我確實有Java經驗。這就是說這個機器人有一個邊緣檢測器來防止它脫落,一個L298驅動器,等等。 由於缺乏經驗,我不確切知道Arduino的代碼如何與方法等一起工作。這就是說,我的主要問題是它如何執行我的方法的延遲?它是否陷入了沒有回報的方法?不進入?執行結束後?目前,似乎只是在沒有打開或關閉開關的情況下向前運行電機。我也檢查了接線。 (我對可疑的問題 - 堆棧溢出處女道歉)。旋轉電機Arduinosumo機器人
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();
}
}
我認爲這是把我在正確的軌道,謝謝。調試文本也確實有助於找出問題。我想到如何測試它,但不確定。 – user3651225
不客氣,讓我們張貼並標記爲正確的,如果它適合你!我不想幹涉這個(真實的)一點,因爲我認爲你在那裏做其他事情。 – askchipbug