|
0.png (11.06 KB, 下載次數(shù): 44)
下載附件
2019-7-14 01:30 上傳
單片機源程序如下:
- #include <IRremote.h>
- #include <Servo.h>
- //***********************定義馬達腳位*************************
- int MotorRight1=5;
- int MotorRight2=6;
- int MotorLeft1=10;
- int MotorLeft2=11;
- int counter=0;
- const int irReceiverPin = 2; //紅外線接收器 OUTPUT 訊號接在 pin 2
- //***********************設(shè)定所偵測到的IRcode*************************
- long IRfront= 0x00FFA25D; //前進碼
- long IRback=0x00FF629D; //後退
- long IRturnright=0x00FFC23D; //右轉(zhuǎn)
- long IRturnleft= 0x00FF02FD; //左轉(zhuǎn)
- long IRstop=0x00FFE21D; //停止
- long IRcny70=0x00FFA857; //CNY70自走模式
- long IRAutorun=0x00FF906F; //超音波自走模式
- long IRturnsmallleft= 0x00FF22DD;
- //*************************定義CNY70腳位************************************
- const int SensorLeft = 7; //左感測器輸入腳
- const int SensorMiddle= 4 ; //中感測器輸入腳
- const int SensorRight = 3; //右感測器輸入腳
- int SL; //左感測器狀態(tài)
- int SM; //中感測器狀態(tài)
- int SR; //右感測器狀態(tài)
- IRrecv irrecv(irReceiverPin); // 定義 IRrecv 物件來接收紅外線訊號
- decode_results results; // 解碼結(jié)果將放在 decode_results 結(jié)構(gòu)的 result 變數(shù)裏
- //*************************定義超音波腳位******************************
- int inputPin =13 ; // 定義超音波信號接收腳位rx
- int outputPin =12; // 定義超音波信號發(fā)射腳位'tx
- int Fspeedd = 0; // 前方距離
- int Rspeedd = 0; // 右方距離
- int Lspeedd = 0; // 左方距離
- int directionn = 0; // 前=8 後=2 左=4 右=6
- Servo myservo; // 設(shè) myservo
- int delay_time = 250; // 伺服馬達轉(zhuǎn)向後的穩(wěn)定時間
- int Fgo = 8; // 前進
- int Rgo = 6; // 右轉(zhuǎn)
- int Lgo = 4; // 左轉(zhuǎn)
- int Bgo = 2; // 倒車
- //********************************************************************(SETUP)
- void setup()
- {
- Serial.begin(9600);
- pinMode(MotorRight1, OUTPUT); // 腳位 8 (PWM)
- pinMode(MotorRight2, OUTPUT); // 腳位 9 (PWM)
- pinMode(MotorLeft1, OUTPUT); // 腳位 10 (PWM)
- pinMode(MotorLeft2, OUTPUT); // 腳位 11 (PWM)
- irrecv.enableIRIn(); // 啟動紅外線解碼
- pinMode(SensorLeft, INPUT); //定義左感測器
- pinMode(SensorMiddle, INPUT);//定義中感測器
- pinMode(SensorRight, INPUT); //定義右感測器
- digitalWrite(2,HIGH);
- pinMode(inputPin, INPUT); // 定義超音波輸入腳位
- pinMode(outputPin, OUTPUT); // 定義超音波輸出腳位
- myservo.attach(9); // 定義伺服馬達輸出第5腳位(PWM)
- }
- //******************************************************************(Void)
- void advance(int a) // 前進
- {
- digitalWrite(MotorRight1,LOW);
- digitalWrite(MotorRight2,HIGH);
- digitalWrite(MotorLeft1,LOW);
- digitalWrite(MotorLeft2,HIGH);
- delay(a * 100);
- }
- void right(int b) //右轉(zhuǎn)(單輪)
- {
- digitalWrite(MotorLeft1,LOW);
- digitalWrite(MotorLeft2,HIGH);
- digitalWrite(MotorRight1,LOW);
- digitalWrite(MotorRight2,LOW);
- delay(b * 100);
- }
- void left(int c) //左轉(zhuǎn)(單輪)
- {
- digitalWrite(MotorRight1,LOW);
- digitalWrite(MotorRight2,HIGH);
- digitalWrite(MotorLeft1,LOW);
- digitalWrite(MotorLeft2,LOW);
- delay(c * 100);
- }
- void turnR(int d) //右轉(zhuǎn)(雙輪)
- {
- digitalWrite(MotorRight1,HIGH);
- digitalWrite(MotorRight2,LOW);
- digitalWrite(MotorLeft1,LOW);
- digitalWrite(MotorLeft2,HIGH);
- delay(d * 100);
- }
- void turnL(int e) //左轉(zhuǎn)(雙輪)
- {
- digitalWrite(MotorRight1,LOW);
- digitalWrite(MotorRight2,HIGH);
- digitalWrite(MotorLeft1,HIGH);
- digitalWrite(MotorLeft2,LOW);
- delay(e * 100);
- }
- void stopp(int f) //停止
- {
- digitalWrite(MotorRight1,LOW);
- digitalWrite(MotorRight2,LOW);
- digitalWrite(MotorLeft1,LOW);
- digitalWrite(MotorLeft2,LOW);
- delay(f * 100);
- }
- void back(int g) //後退
- {
- digitalWrite(MotorRight1,HIGH);
- digitalWrite(MotorRight2,LOW);
- digitalWrite(MotorLeft1,HIGH);
- digitalWrite(MotorLeft2,LOW);;
- delay(g * 100);
- }
- void detection() //測量3個角度(前.左.右)
- {
- int delay_time = 250; // 伺服馬達轉(zhuǎn)向後的穩(wěn)定時間
- ask_pin_F(); // 讀取前方距離
- if(Fspeedd < 10) // 假如前方距離小於10公分
- {
- stopp(1); // 清除輸出資料
- back(2); // 後退 0.2秒
- }
- if(Fspeedd < 25) // 假如前方距離小於25公分
- {
- stopp(1); // 清除輸出資料
- ask_pin_L(); // 讀取左方距離
- delay(delay_time); // 等待伺服馬達穩(wěn)定
- ask_pin_R(); // 讀取右方距離
- delay(delay_time); // 等待伺服馬達穩(wěn)定
- if(Lspeedd > Rspeedd) //假如 左邊距離大於右邊距離
- {
- directionn = Lgo; //向左走
- }
- if(Lspeedd <= Rspeedd) //假如 左邊距離小於或等於右邊距離
- {
- directionn = Rgo; //向右走
- }
- if (Lspeedd < 15 && Rspeedd < 15) //假如 左邊距離和右邊距離皆小於10公分
- {
- directionn = Bgo; //向後走
- }
- }
- else //加如前方大於25公分
- {
- directionn = Fgo; //向前走
- }
- }
- //*********************************************************************************
- void ask_pin_F() // 量出前方距離
- {
- myservo.write(90);
- digitalWrite(outputPin, LOW); // 讓超聲波發(fā)射低電壓2μs
- delayMicroseconds(2);
- digitalWrite(outputPin, HIGH); // 讓超聲波發(fā)射高電壓10μs,這裡至少是10μs
- delayMicroseconds(10);
- digitalWrite(outputPin, LOW); // 維持超聲波發(fā)射低電壓
- float Fdistance = pulseIn(inputPin, HIGH); // 讀差相差時間
- Fdistance= Fdistance/5.8/10; // 將時間轉(zhuǎn)為距離距離(單位:公分)
- Serial.print("F distance:"); //輸出距離(單位:公分)
- Serial.println(Fdistance); //顯示距離
- Fspeedd = Fdistance; // 將距離 讀入Fspeedd(前速)
- }
- //********************************************************************************
- void ask_pin_L() // 量出左邊距離
- {
- myservo.write(177);
- delay(delay_time);
- digitalWrite(outputPin, LOW); // 讓超聲波發(fā)射低電壓2μs
- delayMicroseconds(2);
- digitalWrite(outputPin, HIGH); // 讓超聲波發(fā)射高電壓10μs,這裡至少是10μs
- delayMicroseconds(10);
- digitalWrite(outputPin, LOW); // 維持超聲波發(fā)射低電壓
- float Ldistance = pulseIn(inputPin, HIGH); // 讀差相差時間
- Ldistance= Ldistance/5.8/10; // 將時間轉(zhuǎn)為距離距離(單位:公分)
- Serial.print("L distance:"); //輸出距離(單位:公分)
- Serial.println(Ldistance); //顯示距離
- Lspeedd = Ldistance; // 將距離 讀入Lspeedd(左速)
- }
- //******************************************************************************
- void ask_pin_R() // 量出右邊距離
- {
- myservo.write(5);
- delay(delay_time);
- digitalWrite(outputPin, LOW); // 讓超聲波發(fā)射低電壓2μs
- delayMicroseconds(2);
- digitalWrite(outputPin, HIGH); // 讓超聲波發(fā)射高電壓10μs,這裡至少是10μs
- delayMicroseconds(10);
- digitalWrite(outputPin, LOW); // 維持超聲波發(fā)射低電壓
- float Rdistance = pulseIn(inputPin, HIGH); // 讀差相差時間
- Rdistance= Rdistance/5.8/10; // 將時間轉(zhuǎn)為距離距離(單位:公分)
- Serial.print("R distance:"); //輸出距離(單位:公分)
- Serial.println(Rdistance); //顯示距離
- Rspeedd = Rdistance; // 將距離 讀入Rspeedd(右速)
- }
- //******************************************************************************(LOOP)
- void loop()
- {
- SL = digitalRead(SensorLeft);
- SM = digitalRead(SensorMiddle);
- SR = digitalRead(SensorRight);
- //***************************************************************************正常遙控模式
- if (irrecv.decode(&results))
- { // 解碼成功,收到一組紅外線訊號
- /***********************************************************************/
- if (results.value == IRfront)//前進
- {
- advance(10);//前進
- }
- /***********************************************************************/
- if (results.value == IRback)//後退
- {
- back(10);//後退
- }
- /***********************************************************************/
- if (results.value == IRturnright)//右轉(zhuǎn)
- {
- right(6); // 右轉(zhuǎn)
- }
- /***********************************************************************/
- if (results.value == IRturnleft)//左轉(zhuǎn)
- {
- left(6); // 左轉(zhuǎn));
- }
- /***********************************************************************/
- if (results.value == IRstop)//停止
- {
- digitalWrite(MotorRight1,LOW);
- digitalWrite(MotorRight2,LOW);
- digitalWrite(MotorLeft1,LOW);
- digitalWrite(MotorLeft2,LOW);
- }
- //***********************************************************************cny70模式自走模式 黑:LOW 白:
- if (results.value == IRcny70)
- {
- while(IRcny70)
- {
- SL = digitalRead(SensorLeft);
- SM = digitalRead(SensorMiddle);
- SR = digitalRead(SensorRight);
-
- if (SM == HIGH)//中感測器在黑色區(qū)域
- {
- if (SL == LOW & SR == HIGH) // 左黑右白, 向左轉(zhuǎn)彎
- {
- digitalWrite(MotorRight1,LOW);
- digitalWrite(MotorRight2,HIGH);
- analogWrite(MotorLeft1,0);
- analogWrite(MotorLeft2,80);
- }
- else if (SR == LOW & SL == HIGH) //左白右黑, 向右轉(zhuǎn)彎
- {
- analogWrite(MotorRight1,0);//右轉(zhuǎn)
- analogWrite(MotorRight2,80);
- digitalWrite(MotorLeft1,LOW);
- digitalWrite(MotorLeft2,HIGH);
- }
- else // 兩側(cè)均為白色, 直進
- {
- digitalWrite(MotorRight1,LOW);
- digitalWrite(MotorRight2,HIGH);
- digitalWrite(MotorLeft1,LOW);
- digitalWrite(MotorLeft2,HIGH);
- analogWrite(MotorLeft1,200);
- analogWrite(MotorLeft2,200);
- analogWrite(MotorRight1,200);
- analogWrite(MotorRight2,200);
- }
- }
- else // 中感測器在白色區(qū)域
- {
- if (SL == LOW & SR == HIGH)// 左黑右白, 快速左轉(zhuǎn)
- {
- digitalWrite(MotorRight1,LOW);
- digitalWrite(MotorRight2,HIGH);
- digitalWrite(MotorLeft1,LOW);
- digitalWrite(MotorLeft2,LOW);
- }
- else if (SR == LOW & SL == HIGH) // 左白右黑, 快速右轉(zhuǎn)
- {
- digitalWrite(MotorRight1,LOW);
- digitalWrite(MotorRight2,LOW);
- digitalWrite(MotorLeft1,LOW);
- digitalWrite(MotorLeft2,HIGH);
- }
- else // 都是白色, 停止
- {
- digitalWrite(MotorRight1,HIGH);
- digitalWrite(MotorRight2,LOW);
- digitalWrite(MotorLeft1,HIGH);
- digitalWrite(MotorLeft2,LOW);;
- }
- }
- if (irrecv.decode(&results))
- {
- irrecv.resume();
- Serial.println(results.value,HEX);
- if(results.value ==IRstop)
- {
- digitalWrite(MotorRight1,HIGH);
- digitalWrite(MotorRight2,HIGH);
- digitalWrite(MotorLeft1,HIGH);
- digitalWrite(MotorLeft2,HIGH);
- break;
- }
- }
- }
- results.value=0;
- }
- //***********************************************************************超音波自走模式
- if (results.value ==IRAutorun )
- {
- while(IRAutorun)
- {
- myservo.write(90); //讓伺服馬達回歸 預(yù)備位置 準(zhǔn)備下一次的測量
- detection(); //測量角度 並且判斷要往哪一方向移動
- if(directionn == 8) //假如directionn(方向) = 8(前進)
- {
- if (irrecv.decode(&results))
- {
- irrecv.resume();
- Serial.println(results.value,HEX);
- if(results.value ==IRstop)
- {
- digitalWrite(MotorRight1,LOW);
- digitalWrite(MotorRight2,LOW);
- digitalWrite(MotorLeft1,LOW);
- digitalWrite(MotorLeft2,LOW);
- break;
- }
- }
- results.value=0;
- advance(1); // 正常前進
- Serial.print(" Advance "); //顯示方向(前進)
- Serial.print(" ");
- }
- if(directionn == 2) //假如directionn(方向) = 2(倒車)
- {
- if (irrecv.decode(&results))
- {
- irrecv.resume();
- Serial.println(results.value,HEX);
- if(results.value ==IRstop)
- {
- digitalWrite(MotorRight1,LOW);
- digitalWrite(MotorRight2,LOW);
- digitalWrite(MotorLeft1,LOW);
- digitalWrite(MotorLeft2,LOW);
- break;
- }
- }
- results.value=0;
- back(8); // 倒退(車)
- turnL(3); //些微向左方移動(防止卡在死巷裡)
- Serial.print(" Reverse "); //顯示方向(倒退)
- }
- if(directionn == 6) //假如directionn(方向) = 6(右轉(zhuǎn))
- {
- if (irrecv.decode(&results))
- {
- irrecv.resume();
- Serial.println(results.value,HEX);
- if(results.value ==IRstop)
- {
- digitalWrite(MotorRight1,LOW);
- digitalWrite(MotorRight2,LOW);
- digitalWrite(MotorLeft1,LOW);
- digitalWrite(MotorLeft2,LOW);
- break;
- }
- }
- results.value=0;
- back(1);
- turnR(6); // 右轉(zhuǎn)
- Serial.print(" Right "); //顯示方向(左轉(zhuǎn))
- }
- if(directionn == 4) //假如directionn(方向) = 4(左轉(zhuǎn))
- {
- if (irrecv.decode(&results))
- {
- irrecv.resume();
- Serial.println(results.value,HEX);
- if(results.value ==IRstop)
- {
- digitalWrite(MotorRight1,LOW);
- digitalWrite(MotorRight2,LOW);
- digitalWrite(MotorLeft1,LOW);
- digitalWrite(MotorLeft2,LOW);
- break;
- }
- }
- results.value=0;
- back(1);
- turnL(6); // 左轉(zhuǎn)
- Serial.print(" Left "); //顯示方向(右轉(zhuǎn))
- }
-
- if (irrecv.decode(&results))
- {
- irrecv.resume();
- Serial.println(results.value,HEX);
- if(results.value ==IRstop)
- {
- digitalWrite(MotorRight1,LOW);
- digitalWrite(MotorRight2,LOW);
- digitalWrite(MotorLeft1,LOW);
- digitalWrite(MotorLeft2,LOW);
- break;
- }
- }
- }
- results.value=0;
- }
- /***********************************************************************/
- else
- {
- digitalWrite(MotorRight1,LOW);
- digitalWrite(MotorRight2,LOW);
- digitalWrite(MotorLeft1,LOW);
- digitalWrite(MotorLeft2,LOW);
- }
-
- irrecv.resume(); // 繼續(xù)收下一組紅外線訊號
- }
- }
復(fù)制代碼
所有資料51hei提供下載:
高級自走車完整資料.7z
(11.55 MB, 下載次數(shù): 73)
2019-7-14 01:33 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|
|