Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Arduino.h>
- #include <MeMCore.h>
- #include <SoftwareSerial.h>
- #include <Timer.h>
- #include <Wire.h>
- MBotDCMotor motor_left(9);
- MBotDCMotor motor_right(10);
- MeLineFollower lfer(2);
- MeUltrasonicSensor UTS(3);
- MeBuzzer buzzer; // 蜂鳴器
- bool Buttom = false;
- bool set = false;
- Timer t;
- void setup() {
- // 開機音效
- buzzer.tone(262, 200);
- buzzer.tone(294, 200);
- buzzer.tone(330, 200);
- while(analogRead(A7)<=10);
- t.every(20*1000,callback,1);
- }
- void callback(){
- while(lfer.readSensors() == 0) {
- motor_left.run(-230);
- motor_right.run(45);}
- motor_left.run(-255);
- motor_right.run(255);
- delay(5000);}
- void loop() {
- int distance = UTS.distanceCm();
- t.update();
- 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 {
- byte ReturnValue = lfer.readSensors();
- switch (ReturnValue) {
- case 0:
- motor_left.run(-255);
- motor_right.run(255);
- break;
- case 1:
- // 左轉
- motor_left.run(-45);
- motor_right.run(230);
- while (lfer.readSensors() == 1);
- break;
- case 2:
- // 右轉
- motor_left.run(-230);
- motor_right.run(45);
- while (lfer.readSensors() == 2);
- break;
- case 3:
- // 遇到白白
- motor_left.run(155);
- motor_right.run(-155);
- // delay(300);
- //
- // // 若無則左轉直到感測到黑
- // motor_left.run(150);
- // motor_right.run(-150);
- while (lfer.readSensors() == 3);
- break;
- }
- }
- }
- // 循線感測器
- // 以上為超音波感測器
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement