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 lfer(2);
- MeUltrasonicSensor UTS(3);
- MeBuzzer buzzer; //嗡鳴器
- byte ReturnValue;
- int distance;
- bool Buttomif =false; //設定按下按鈕後的判斷條件
- int buttom; //設定一個參數為buttom
- void setup()
- {
- buzzer.tone(262,200);
- buzzer.tone(294,200);
- buzzer.tone(330,200); //輸出已(重)開機訊號
- }
- void loop()
- {
- if(buttom<10) //如果按鈕按下去
- {
- Buttomif = true;
- }
- if(Buttomif == true)
- {
- ReturnValue=lfer.readSensors();
- distance=UTS.distanceCm();
- buttom=analogRead(A7); //設定buttom為板載按鈕
- if(distance <= 5) //避障
- {
- motor_left.run(0);
- motor_right.run(0);
- delay(500);
- motor_left.run(0);
- motor_right.run(-100);
- delay(1000);
- motor_left.run(-200);
- motor_right.run(200);
- delay(800);
- motor_left.run(0);
- motor_right.run(200);
- delay(800);
- motor_left.run(-200);
- motor_right.run(200);
- delay(500);
- motor_left.run(-200);
- motor_right.run(-100);
- delay(1000);
- }else
- {
- if(ReturnValue==0)
- {
- motor_left.run(-150);
- motor_right.run(150);
- }
- if(ReturnValue==1) //左轉
- {
- motor_left.run(-50);
- motor_right.run(200);
- while(lfer.readSensors()==1);
- };
- if(ReturnValue==2){ //右轉
- motor_left.run(-200);
- motor_right.run(50);
- while(lfer.readSensors()==2);
- }
- if(ReturnValue==3) //遇到白白
- {
- motor_left.run(-150);
- motor_right.run(100);
- delay(300);
- motor_left.run(150); //若無則左轉直到感測到黑
- motor_right.run(-150);
- while(lfer.readSensors()==3);
- }
- //循線感測器
- }
- }
- }
- //以上為超音波感測器
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement