Advertisement
Guest User

Untitled

a guest
May 24th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.15 KB | None | 0 0
  1. #include <Arduino.h>
  2. #include <MeMCore.h>
  3. #include <SoftwareSerial.h>
  4. #include <Timer.h>
  5. #include <Wire.h>
  6.  
  7. MBotDCMotor motor_left(9);
  8. MBotDCMotor motor_right(10);
  9. MeLineFollower lfer(2);
  10. MeUltrasonicSensor UTS(3);
  11. MeBuzzer buzzer;    // 蜂鳴器
  12. bool Buttom = false;
  13.  
  14.  
  15. void setup() {
  16.     // 開機音效
  17.     buzzer.tone(262, 200);
  18.     buzzer.tone(294, 200);
  19.     buzzer.tone(330, 200);
  20.    
  21.     while(!(analogRead(A7)>10);
  22.     }
  23.     // 這邊寫按鈕按下後要做的事, 只會做一次
  24.  
  25. void loop() {
  26.     int distance = UTS.distanceCm();
  27.     if (distance <= 5) {
  28.         // 避障
  29.         motor_left.run(0);
  30.         motor_right.run(0);
  31.         delay(500);
  32.  
  33.         motor_left.run(0);
  34.         motor_right.run(-100);
  35.         delay(1000);
  36.  
  37.         motor_left.run(-200);
  38.         motor_right.run(200);
  39.         delay(800);
  40.  
  41.         motor_left.run(0);
  42.         motor_right.run(200);
  43.         delay(800);
  44.  
  45.         motor_left.run(-200);
  46.         motor_right.run(200);
  47.         delay(500);
  48.  
  49.         motor_left.run(-200);
  50.         motor_right.run(-100);
  51.         delay(1000);
  52.     }
  53.     else {
  54.         byte ReturnValue = lfer.readSensors();
  55.         switch (ReturnValue) {
  56.             case 0:
  57.                 motor_left.run(-150);
  58.                 motor_right.run(150);
  59.                 break;
  60.  
  61.             case 1:
  62.                 // 左轉
  63.                 motor_left.run(-50);
  64.                 motor_right.run(200);
  65.  
  66.                 while (lfer.readSensors() == 1);
  67.                 break;
  68.  
  69.             case 2:
  70.                 // 右轉
  71.                 motor_left.run(-200);
  72.                 motor_right.run(50);
  73.  
  74.                 while (lfer.readSensors() == 2);
  75.                 break;
  76.  
  77.             case 3:
  78.                 // 遇到白白
  79.                 motor_left.run(-150);
  80.                 motor_right.run(100);
  81.                 delay(300);
  82.  
  83.                 // 若無則左轉直到感測到黑
  84.                 motor_left.run(150);
  85.                 motor_right.run(-150);
  86.  
  87.                 while (lfer.readSensors() == 3);
  88.                 break;
  89.         }
  90.  
  91.         // 循線感測器
  92.     }
  93. }
  94. // 以上為超音波感測器
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement