|
現在沒有頭緒再用紅外線寫出避障的程式下面是試寫的,好像有問題
請教高手指教
int IR_right=9;//定義右側避障傳感器接口
int IR_left=8;//定義左側避障傳感器接口
int E_right =5; //連接電機1的使能端口到數字接口5
int M_right =4; //連接電機1的轉向端口到數字接口4
int E_left = 6;//連接電機2的使能端口到數字接口6
int M_left=7; //連接電機2的轉向端口到數字接口5
int Right=0;
int Left=0;
int pwm_a = 3;
int pwm_b = 11;
void setup()
{
//給各數字接口設置輸入輸出狀態
pinMode (M_right, OUTPUT);
pinMode(E_right, OUTPUT);
pinMode(M_left, OUTPUT);
pinMode(E_left, OUTPUT);
pinMode(IR_right,INPUT);
pinMode(IR_left, INPUT);
analogWrite(pwm_a, 100);
analogWrite(pwm_b, 100);
}
void advance()//前進
{
digitalWrite(M_right,HIGH);
digitalWrite(M_left,HIGH);
}
void back()//後退
{
digitalWrite(M_right,HIGH);
digitalWrite(M_left,LOW);
}
void right()//右轉
{
digitalWrite(M_right,LOW) ;
digitalWrite(M_left,HIGH);
;
}
void left()//左 ??轉
{
digitalWrite(M_right,HIGH);
digitalWrite(M_left,LOW);
}
void Stop()//停止
{
digitalWrite(E_right, LOW); //右電機停
digitalWrite(E_left, LOW); //左電機停
}
void loop()
{
//讀紅外蔽障傳感器的信號
Right=digitalRead(IR_right);
Left=digitalRead(IR_left);
//左右為高電位
if(Right==HIGH && Left==HIGH)
advance(); //前進
//如果左紅外低電平,左邊有障礙
if(Right==HIGH && Left==LOW )
right(); //右轉
//如果左紅外為低電平,左邊有障礙
if(Right==LOW && Left==HIGH )
left(); //左轉
if(Right==HIGH && Left==LOW)
Stop(); //停止
} |
|