1. Home
  2. /
  3. IOT 物聯網
  4. /
  5. Arduino
  6. /
  7. [ Arduino ] –...

[ Arduino ] – 自動避障坦克車 程式碼

評等結果
點擊便能為這篇文章進行評等!
[評等總次數: 0,平均評等: 0]

[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]

評等結果
點擊便能為這篇文章進行評等!
[評等總次數: 0,平均評等: 0]

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *