Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //匯入
- #include <Arduino.h>
- #include <Wire.h>
- #include <SoftwareSerial.h>
- #include <MeMCore.h>
- #include <Timer.h>
- //自訂參數
- MBotDCMotor motor_left(9); //左邊馬達
- MBotDCMotor motor_right(10); //右邊馬達
- MeLineFollower IF1(2); // ss2
- MeLineFollower IF2(1); // ss3
- MeLineFollower IF3(4); // ss4
- MeUltrasonicSensor UTS(3); //超音波
- MeBuzzer buzzer; //嗡鳴器
- byte S1; //設定左邊感測器的回傳值
- byte S3; //callback使用判斷值
- byte S2; //設定右邊感測器的回傳值
- int buttom; //設定一個參數為buttom
- int distance; //設定一個參數為distance
- int S1times = 0;
- int S2times = 0;
- int S3times = 0;
- int Atimes = 0;
- int Atimes2 = 0;
- bool Buttomif =false; //設定按下按鈕後的判斷條件
- bool setif = false; //設定一開始衝刺
- void setup()
- {
- buzzer.tone(262,200);
- buzzer.tone(294,200);
- buzzer.tone(330,200); //輸出已(重)
- if(buttom>10) //如果按鈕按下去
- {
- Buttomif = true;
- setif = true;
- }
- while(setif ==true)
- {
- motor_left.run(-255);
- motor_right.run(255);
- delay(2232);
- setif = true;
- }
- }
- void loop()
- {
- buttom=analogRead(A7); //設定buttom為板載按鈕
- S1=IF1.readSensors(); //callback判對值為中間感測器資料
- S3=IF2.readSensors(); //callback判對值為中間感測器資料
- S2=IF3.readSensors(); //callback判對值為中間感測器資料
- distance=UTS.distanceCm(); //令超音波資料為distance
- if(buttom<10) //如果按鈕按下去
- {
- Buttomif = true;
- setif = true;
- }
- if(Buttomif == true)
- { //按下板載按鈕後執行所有loop程式
- if(distance <= 5) //距離小於5cm 程式避障
- {
- motor_left.run(255);
- motor_right.run(-255);
- delay(200); //後退
- motor_left.run(-55);
- motor_right.run(255);
- delay(500); //左轉
- motor_left.run(-255);
- motor_right.run(255);
- delay(200); //前進
- motor_left.run(-255);
- motor_right.run(55);
- delay(700); //右轉
- motor_left.run(-255);
- motor_right.run(255);
- delay(150); //接線時請調150,鋰電池時請調250(前進)
- motor_left.run(255);
- motor_right.run(0);
- delay(680); //回復路線
- }
- else{ //無感測到障礙物時
- //以下為新型循線
- // ==0 = 黑 !=0 = 白
- if(S1!=0 && S3==0 && S2!=0) //ss3感黑 一三路感白
- {
- //白黑白
- motor_left.run(-255);
- motor_right.run(255);
- //直走
- }
- else if (S1==0 && S3!=0 && S2!=0) // 一路感黑 二三路感白
- {
- //黑白白
- motor_left.run(0);
- motor_right.run(100);
- //往左 (未測試)
- }
- else if (S1==0 && S3==0 && S2!=0) // 三路感白 一二路感黑
- {
- //黑黑白
- motor_left.run(0);
- motor_right.run(100);
- //往右(未測試)
- }
- else if (S1!=0 && S3!=0 && S2==0) // 三路感黑 一二路感白
- {
- //白白黑
- motor_left.run(-100);
- motor_right.run(0);
- //往左 (未測試)
- }
- else if (S1!=0 && S3==0 && S2==0) // 一路感白 二三路感黑
- {
- //白黑黑
- motor_left.run(-100);
- motor_right.run(0);
- //往右(未測試)
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement