Advertisement
a5768549

new(20180524)

May 24th, 2018
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.86 KB | None | 0 0
  1. //匯入
  2.  
  3. #include <Arduino.h>
  4. #include <Wire.h>
  5. #include <SoftwareSerial.h>
  6. #include <MeMCore.h>
  7. #include <Timer.h>
  8. //自訂參數
  9.  
  10. MBotDCMotor motor_left(9);      //左邊馬達
  11. MBotDCMotor motor_right(10);    //右邊馬達
  12. MeLineFollower IF1(2);          // ss2
  13. MeLineFollower IF2(1);          // ss3
  14. MeLineFollower IF3(4);          // ss4
  15. MeUltrasonicSensor UTS(3);      //超音波
  16. MeBuzzer buzzer;                //嗡鳴器
  17. byte S1;                        //設定左邊感測器的回傳值
  18. byte S3;                        //callback使用判斷值
  19. byte S2;                        //設定右邊感測器的回傳值
  20. int buttom;                     //設定一個參數為buttom
  21. int distance;                   //設定一個參數為distance
  22. int S1times = 0;
  23. int S2times = 0;
  24. int S3times = 0;
  25. int Atimes = 0;                      
  26. int Atimes2 = 0;
  27. bool Buttomif =false;           //設定按下按鈕後的判斷條件
  28. bool setif = false;             //設定一開始衝刺
  29.  
  30. void setup()
  31. {
  32.       buzzer.tone(262,200);
  33.       buzzer.tone(294,200);
  34.       buzzer.tone(330,200);        //輸出已(重)
  35.       if(buttom>10)                //如果按鈕按下去
  36.       {
  37.            Buttomif = true;
  38.            setif = true;
  39.       }
  40.       while(setif ==true)
  41.       {
  42.               motor_left.run(-255);
  43.               motor_right.run(255);
  44.               delay(2232);
  45.               setif = true;
  46.       }
  47. }
  48. void loop()
  49. {
  50.   buttom=analogRead(A7);         //設定buttom為板載按鈕
  51.   S1=IF1.readSensors();          //callback判對值為中間感測器資料
  52.   S3=IF2.readSensors();          //callback判對值為中間感測器資料
  53.   S2=IF3.readSensors();          //callback判對值為中間感測器資料
  54.   distance=UTS.distanceCm();     //令超音波資料為distance
  55.  
  56.      if(buttom<10)                //如果按鈕按下去
  57.      {
  58.            Buttomif = true;
  59.            setif = true;
  60.       }
  61.  
  62.       if(Buttomif == true)
  63.       {                               //按下板載按鈕後執行所有loop程式
  64.                 if(distance <= 5)     //距離小於5cm  程式避障
  65.                 {
  66.                   motor_left.run(255);
  67.                   motor_right.run(-255);
  68.                   delay(200);    //後退
  69.                   motor_left.run(-55);
  70.                   motor_right.run(255);
  71.                   delay(500);    //左轉
  72.                   motor_left.run(-255);
  73.                   motor_right.run(255);
  74.                   delay(200);    //前進
  75.                   motor_left.run(-255);
  76.                   motor_right.run(55);
  77.                   delay(700);    //右轉
  78.                   motor_left.run(-255);
  79.                   motor_right.run(255);
  80.                   delay(150);    //接線時請調150,鋰電池時請調250(前進)
  81.                   motor_left.run(255);
  82.                   motor_right.run(0);
  83.                   delay(680);    //回復路線
  84.                 }
  85.                  else{                  //無感測到障礙物時
  86.    
  87.                           //以下為新型循線
  88.                           // ==0 = 黑  !=0 = 白
  89.                           if(S1!=0 && S3==0 && S2!=0)        //ss3感黑 一三路感白
  90.                             {  
  91.                               //白黑白
  92.                                motor_left.run(-255);
  93.                                motor_right.run(255);
  94.                                //直走
  95.                             }
  96.                            else if (S1==0 && S3!=0 && S2!=0) // 一路感黑 二三路感白
  97.                             {  
  98.                                //黑白白
  99.                                motor_left.run(0);
  100.                                motor_right.run(100);
  101.                                //往左 (未測試)
  102.                             }
  103.                             else if (S1==0 && S3==0 && S2!=0) // 三路感白 一二路感黑
  104.                             {  
  105.                                //黑黑白
  106.                                motor_left.run(0);
  107.                                motor_right.run(100);
  108.                                //往右(未測試)
  109.                             }
  110.                             else if (S1!=0 && S3!=0 && S2==0) // 三路感黑 一二路感白
  111.                             {
  112.                                //白白黑
  113.                                motor_left.run(-100);
  114.                                motor_right.run(0);
  115.                                //往左 (未測試)
  116.                             }
  117.                             else if (S1!=0 && S3==0 && S2==0) // 一路感白 二三路感黑
  118.                             {  
  119.                                //白黑黑
  120.                                motor_left.run(-100);
  121.                                motor_right.run(0);
  122.                                //往右(未測試)
  123.                             }
  124.              }
  125.       }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement