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;
- void setup() {
- // 開機音效
- buzzer.tone(262, 200);
- buzzer.tone(294, 200);
- buzzer.tone(330, 200);
- while(!(analogRead(A7)>10);
- }
- // 這邊寫按鈕按下後要做的事, 只會做一次
- void loop() {
- int distance = UTS.distanceCm();
- 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(-150);
- motor_right.run(150);
- break;
- case 1:
- // 左轉
- motor_left.run(-50);
- motor_right.run(200);
- while (lfer.readSensors() == 1);
- break;
- case 2:
- // 右轉
- motor_left.run(-200);
- motor_right.run(50);
- while (lfer.readSensors() == 2);
- break;
- case 3:
- // 遇到白白
- motor_left.run(-150);
- motor_right.run(100);
- 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