[javascript]
int isObstaclePin = 7;
int isObstacle = HIGH;
int randDirection=1;
void setup() {
pinMode(isObstaclePin,INPUT);
pinMode(A1,OUTPUT);
pinMode(A2,OUTPUT);
pinMode(A4,OUTPUT);
pinMode(A5,OUTPUT);
}
void loop(){
isObstacle = digitalRead(isObstaclePin);//讀取感測器的值
if (isObstacle == LOW)//有障礙物
{
goback();//後退
delay(1500);
randDirection=random(1,3);//隨機取得 1 或 2
if(randDirection==1){//1 左轉
goleft();
delay(500);
}else{//2 右轉
goright();
delay(500);
}
}else{
forward();
}
}
void forward(){//前
digitalWrite(A1,0);
digitalWrite(A2,135);
digitalWrite(A4,0);
digitalWrite(A5,135);
delay(200);
}
void goback(){//後
analogWrite(A1,135);
analogWrite(A2,0);
analogWrite(A4,135);
analogWrite(A5,0);
delay(200);
}
void goleft(){//左
analogWrite(A1,130);
analogWrite(A2,0);
analogWrite(A4,0);
analogWrite(A5,135);
}
void goright(){//右
analogWrite(A1,0);
analogWrite(A2,135);
analogWrite(A4,135);
analogWrite(A5,0);
}
void turn_off(){//停
analogWrite(A1,0);
analogWrite(A2,0);
analogWrite(A4,0);
analogWrite(A5,0);
delay(300);
}
[/javascript]
[ Arduino ] – 自動避障坦克車 程式碼
評等結果
點擊便能為這篇文章進行評等!
[評等總次數: 0,平均評等: 0]
評等結果
點擊便能為這篇文章進行評等!
[評等總次數: 0,平均評等: 0]