Advertisement
Guest User

Untitled

a guest
May 25th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.14 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);        //循線(插槽2)
  10. MeUltrasonicSensor UTS(3);     //超音波(插槽3)
  11. MeBuzzer buzzer;               //蜂鳴器
  12. unsigned long StartTime;       //設定開機時間(按下板載按鈕)
  13. unsigned long Ftime;           //設定目標時間
  14. bool time1 = true;             //設定開關函式
  15.  
  16. void setup()
  17.     {
  18.     // 開機音效
  19.     buzzer.tone(262, 200);
  20.     buzzer.tone(294, 200);
  21.     buzzer.tone(330, 200);
  22.    
  23.     while(analogRead(A7)>10);  //按下板載按鈕(執行所有程式)
  24.     StartTime = millis();      //設定起始時間
  25.     }
  26.  
  27. void loop()
  28. {
  29.      int distance = UTS.distanceCm();    //設定避障回傳值
  30.      Ftime = StartTime + 20000;          //設定Ftime為起始時間加20秒
  31.      if(time1 && (millis()-StartTime >= Ftime))  
  32.      //如果time1是true,現在時間>=最終時間
  33.      {
  34.            motor_left.run(-230);
  35.            motor_right.run(45);
  36.           while(lfer.readSensors() == 0)
  37.           {
  38.                 motor_left.run(-255);
  39.                 motor_right.run(255);
  40.                 delay(5000);
  41.           }
  42.           time1 = false;
  43.       }
  44.  
  45.        
  46.        
  47.        if (distance <= 5)
  48.        {
  49.             // 避障
  50.             motor_left.run(0);
  51.             motor_right.run(0);
  52.             delay(500);
  53.    
  54.             motor_left.run(0);
  55.             motor_right.run(-100);
  56.             delay(1000);
  57.    
  58.             motor_left.run(-200);
  59.             motor_right.run(200);
  60.             delay(800);
  61.    
  62.             motor_left.run(0);
  63.             motor_right.run(200);
  64.             delay(800);
  65.    
  66.             motor_left.run(-200);
  67.             motor_right.run(200);
  68.             delay(500);
  69.    
  70.             motor_left.run(-200);
  71.             motor_right.run(-100);
  72.             delay(1000);
  73.     }
  74.     else {
  75.             byte ReturnValue = lfer.readSensors();
  76.             switch (ReturnValue)
  77.             {
  78.                 case 0:
  79.                     motor_left.run(-255);
  80.                     motor_right.run(255);
  81.                    
  82.                     break;
  83.    
  84.                 case 1:
  85.                     // 左轉
  86.                     motor_left.run(-40);
  87.                     motor_right.run(235);
  88.    
  89.                     while (lfer.readSensors() == 1);
  90.                     break;
  91.    
  92.                 case 2:
  93.                     // 右轉
  94.                     motor_left.run(-235);
  95.                     motor_right.run(40);
  96.    
  97.                     while (lfer.readSensors() == 2);
  98.                     break;
  99.    
  100.                 case 3:
  101.                     // 遇到白白
  102.                     motor_left.run(150);
  103.                     motor_right.run(-150);
  104. //                delay(300);
  105. //
  106. //                // 若無則左轉直到感測到黑
  107. //                motor_left.run(150);
  108. //                motor_right.run(-150);
  109.  
  110.                 while (lfer.readSensors() == 3);
  111.                 break;
  112.         }
  113.     }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement