Advertisement
Guest User

Untitled

a guest
May 24th, 2018
329
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.38 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. bool set = false;
  14. Timer t;
  15.  
  16. void setup() {
  17.     // 開機音效
  18.     buzzer.tone(262, 200);
  19.     buzzer.tone(294, 200);
  20.     buzzer.tone(330, 200);
  21.    
  22.     while(analogRead(A7)<=10);
  23.     t.every(20*1000,callback,1);
  24.     }
  25. void callback(){
  26.   while(lfer.readSensors() == 0) {
  27.        motor_left.run(-230);
  28.        motor_right.run(45);}
  29.  
  30.         motor_left.run(-255);
  31.         motor_right.run(255);
  32.         delay(5000);}
  33.  
  34. void loop() {
  35.     int distance = UTS.distanceCm();
  36.     t.update();
  37.     if (distance <= 5) {
  38.         // 避障
  39.         motor_left.run(0);
  40.         motor_right.run(0);
  41.         delay(500);
  42.  
  43.         motor_left.run(0);
  44.         motor_right.run(-100);
  45.         delay(1000);
  46.  
  47.         motor_left.run(-200);
  48.         motor_right.run(200);
  49.         delay(800);
  50.  
  51.         motor_left.run(0);
  52.         motor_right.run(200);
  53.         delay(800);
  54.  
  55.         motor_left.run(-200);
  56.         motor_right.run(200);
  57.         delay(500);
  58.  
  59.         motor_left.run(-200);
  60.         motor_right.run(-100);
  61.         delay(1000);
  62.     }
  63.     else {
  64.         byte ReturnValue = lfer.readSensors();
  65.         switch (ReturnValue) {
  66.             case 0:
  67.                 motor_left.run(-255);
  68.                 motor_right.run(255);
  69.                 break;
  70.  
  71.             case 1:
  72.                 // 左轉
  73.                 motor_left.run(-45);
  74.                 motor_right.run(230);
  75.  
  76.                 while (lfer.readSensors() == 1);
  77.                 break;
  78.  
  79.             case 2:
  80.                 // 右轉
  81.                 motor_left.run(-230);
  82.                 motor_right.run(45);
  83.  
  84.                 while (lfer.readSensors() == 2);
  85.                 break;
  86.  
  87.             case 3:
  88.                 // 遇到白白
  89.                 motor_left.run(155);
  90.                 motor_right.run(-155);
  91. //                delay(300);
  92. //
  93. //                // 若無則左轉直到感測到黑
  94. //                motor_left.run(150);
  95. //                motor_right.run(-150);
  96.  
  97.                 while (lfer.readSensors() == 3);
  98.                 break;
  99.         }
  100.     }
  101.    
  102. }
  103.         // 循線感測器
  104.    
  105.  
  106. // 以上為超音波感測器
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement