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

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

!議建法想的你下留是或分享加讚按我幫請,話的您到助幫有章文的我果如 如果我的文章有幫助到您的話,請幫我按讚加分享或是留下你的想法建議!

發佈留言

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