查看: 234|回復: 0
打印 上一主題 下一主題

[未解決] 避障小車多路超聲波傳感器干擾問題

[復制鏈接]

該用戶從未簽到

跳轉到指定樓層
樓主
發表于 2019-10-5 15:53 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
本人小萌新,接觸Arduino2個多月了   前面多多少少也用arduino板子做過一些東西   這次想做一個避障小車搭載4路超聲波傳感器和兩路紅外傳感器。
本來應該讓Z方向的超聲波傳感器測量距離并達到條件后右轉的   但是小車在Y方向距離條件下左轉了。
1.多路超聲波傳感器反饋的值只要達到條件是不是就會被執行,而不是區分X\Y\Z?
2.多路傳感器是不是會互相干擾?
跪求師傅們解惑!
部分代碼:
void forward();
void _stop();
void turnright();
void turnleft();
void back();
void leftgo();
void rightgo();
int Y;
int Z;
void setup()
{
  pinMode(2, OUTPUT);//OUT1
  pinMode(3, OUTPUT);//OUT2
  pinMode(4, OUTPUT);//OUT3
  pinMode(5, OUTPUT);//OUT4
  pinMode(6, OUTPUT);//OUT1*
  pinMode(7, OUTPUT);//OUT2*
  pinMode(8, OUTPUT);//OUT3*
  pinMode(9, OUTPUT);//OUT1
  Serial.begin(115200);
  pinMode(22, INPUT);
  pinMode(24, OUTPUT);
  pinMode(26, INPUT);
  pinMode(28, OUTPUT);
  pinMode(30,INPUT);
  pinMode(32,OUTPUT);
  pinMode(34,INPUT);
  pinMode(36,OUTPUT);
}
void loop()
{
/*********Y方向的超聲波測距************/
digitalWrite(24, LOW);
delayMicroseconds(2);
digitalWrite(24, HIGH);
delayMicroseconds(10);
digitalWrite(24, LOW);
int Y = pulseIn(22, HIGH);
Y= Y/58;
Serial.print(" Y= ");
Serial.println(Y);
delay(1);
/********Z方向的超聲波測距************/
digitalWrite(28, LOW);
delayMicroseconds(2);
digitalWrite(28, HIGH);
delayMicroseconds(10);
digitalWrite(28, LOW);
int Z = pulseIn(26, HIGH);
Z= Z/58;
Serial.print(" Z= ");
Serial.println(Z);
delay(1);
/**********X方向的超聲波測距***********/
//digitalWrite(32, LOW);
//delayMicroseconds(2);
//digitalWrite(32, HIGH);
//delayMicroseconds(10);
//digitalWrite(32, LOW);
//int X = pulseIn(30, HIGH);
//X= X/58;
//Serial.print(" X= ");
//Serial.println(X);
//delay(1);
if(Y<=15)
{
  _stop();
  turnleft();
  delay(900);
  }
else if(Z>=70&&Z<=90)
{
  _stop();
  turnright;
  delay(900);
  }
  else
{
  forward();
  }
}
void forward()
{
  analogWrite(2,60);//右前
  analogWrite(3,0);//
  analogWrite(4,0);//右后
  analogWrite(5,60);
  analogWrite(6,0);//左后
  analogWrite(7,60);
  analogWrite(8,0);//左前
  analogWrite(9,60);
}
void _stop()
{
  analogWrite(2,0);
  analogWrite(3,0);
  analogWrite(4,0);
  analogWrite(5,0);
  analogWrite(6,0);
  analogWrite(7,0);
  analogWrite(8,0);
  analogWrite(9,0);  
}
void turnleft()
{
  analogWrite(2,60);
  analogWrite(3,0);
  analogWrite(4,0);
  analogWrite(5,60);
  analogWrite(6,60);
  analogWrite(7,0);
  analogWrite(8,60);
  analogWrite(9,0);  
}
void turnright()
{
  analogWrite(2,0);
  analogWrite(3,60);
  analogWrite(4,60);
  analogWrite(5,0);
  analogWrite(6,0);
  analogWrite(7,60);
  analogWrite(8,0);
  analogWrite(9,60);   
}
void leftgo()
{
  analogWrite(2,60);//右前
  analogWrite(3,0);
  analogWrite(4,60);//右后
  analogWrite(5,0);
  analogWrite(6,0);//左后
  analogWrite(7,60);
  analogWrite(8,60);//左前
  analogWrite(9,0);
}
void rightgo()
{
  analogWrite(2,0);
  analogWrite(3,60);
  analogWrite(4,0);
  analogWrite(5,60);
  analogWrite(6,60);
  analogWrite(7,0);
  analogWrite(8,0);
  analogWrite(9,60);
}
void back()
{
  analogWrite(2,0);
  analogWrite(3,60);
  analogWrite(4,60);
  analogWrite(5,0);
  analogWrite(6,60);
  analogWrite(7,0);
  analogWrite(8,60);
  analogWrite(9,0);
}

您需要登錄后才可以回帖 登錄 | 立即注冊  

本版積分規則

熱門推薦

【Arduino】108種傳感器模塊系列實驗(104)---MAX30102手腕心率
【Arduino】108種傳感器模
37款傳感器與模塊的提法,在網絡上廣泛流傳,其實Arduino能夠兼容的傳感器模塊肯定是
萌新報道 剛購買了一個watchx
萌新報道 剛購買了一個wat
就這貨 但是網上讓下載庫 我點加載以后他提示沒法用。。 求大神幫忙 謝謝了
【Arduino】108種傳感器系列實驗(15)---手指偵測心跳傳感...
【Arduino】108種傳感器系
37款傳感器與模塊的提法,在網絡上廣泛流傳,其實Arduino能夠兼容的傳感器模塊肯定是
【原創】全球最小口袋3D打印機mini one直播教程貼
【原創】全球最小口袋3D打
最近閑得蛋疼,沒事搞個掌上3D打印機,先放效果圖吧。 搞了半天,終于能正常打印,
求助!GM65二維碼掃描模塊怎么用?
求助!GM65二維碼掃描模塊
求助!GM65二維碼掃描模塊怎么用? 賣家給的資料真的看不懂該怎么弄。。( ̄▽ ̄)~*
Copyright   ©2015-2016  Arduino中文社區  Powered by©Discuz!   ( 蜀ICP備14017632號-3 )
快速回復 返回頂部 返回列表
六肖六码中特