Advertisement
Guest User

Untitled

a guest
May 22nd, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.07 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);          // ss1 ss2
  13. MeLineFollower IF2(1);          // ss3
  14. MeLineFollower IF3(4);          // ss4 ss5
  15. MeUltrasonicSensor UTS(3);      //超音波
  16. MeBuzzer buzzer;                //嗡鳴器
  17. byte ReturnIF1;                 //設定左邊感測器的回傳值
  18. byte ReturnIF2;                 //設定中間感測器的回傳值
  19. byte ReturnIF3;                 //設定右邊感測器的回傳值
  20. int buttom;                     //設定一個參數為buttom
  21. int distance;                   //設定一個參數為distance
  22. bool times = false;             //設定一個是否值
  23. const int Ttimes = 3;           //目標計算次數
  24. bool times2 = false;            //設定一個是否值
  25. const int Ttimes2 = 3;          //設定計算次數
  26. bool times3 = false;            //設定一個是否值
  27. const int Ttimes3 = 3;          //設定計算次數
  28. byte S3;                        //callback使用判斷值
  29. byte S3Output = 0;              //循線使用回傳值
  30. Timer t;                        //設定Timer為t
  31. int add = 0;                    //callback使用計算值
  32. bool Buttomif =true;            //設定按下按鈕後的判斷條件
  33.  
  34.  
  35. void setup()
  36. {
  37.       buzzer.tone(262,200);
  38.       buzzer.tone(294,200);
  39.       buzzer.tone(330,200);        //輸出已(重)開機訊號
  40.       t.every(80,callback,6);      //設定timer參數
  41. }
  42. void callback()               //timer使用的callback函數
  43. {            
  44.       if(S3 == 0)                 //S3感測為黑時
  45.       {
  46.           add++;                    //add計算值+1
  47.       }
  48.       if(add>=3)              //如果add大於等於3時
  49.       {
  50.           S3Output = 0;         //輸入訊號為黑
  51.           add = 0;              //add計算時歸零
  52.       }
  53.       else{
  54.           S3Output = 1;         //輸入訊號為白
  55.           add = 0;              //add計算時歸零
  56.           }
  57. }
  58. void loop()
  59. {
  60.   buttom=analogRead(A7);         //設定buttom為板載按鈕
  61.   ReturnIF1=IF1.readSensors();
  62.   ReturnIF2=IF2.readSensors();
  63.   ReturnIF3=IF3.readSensors();   //設定五路的感測資料位置
  64.   distance=UTS.distanceCm();     //令超音波資料為distance
  65.   S3=IF2.readSensors();          //callback判對值為中間感測器資料
  66.   S3Output=IF2.readSensors();    //循線輸入值(S3)
  67.   int Atimes = 0;                      
  68.   int Atimes2 = 0;
  69.   int Atimes3 = 0;
  70.   t.update();
  71.  
  72. if(buttom<10)                //如果按鈕按下去
  73.       {
  74.           Buttomif = false;
  75.       }
  76.       if(Buttomif == false)
  77.       {                         //按下板載按鈕後執行所有loop程式
  78.           if(distance <= 5)     //距離小於5cm  程式避障
  79.                 {          
  80.                   motor_left.run(255);
  81.                   motor_right.run(-255);
  82.                   delay(200);   //後退
  83.                   motor_left.run(-55);
  84.                   motor_right.run(255);
  85.                   delay(500);    //左轉
  86.                   motor_left.run(-255);
  87.                   motor_right.run(255);
  88.                   delay(200);    //前進
  89.                   motor_left.run(-255);
  90.                   motor_right.run(55);
  91.                   delay(700);    //右轉
  92.                   motor_left.run(-255);
  93.                   motor_right.run(255);
  94.                   delay(150);    //接線時請調150,鋰電池時請調250(前進)
  95.                   motor_left.run(255);
  96.                   motor_right.run(0);
  97.                   delay(680);    //回復路線
  98.                 }
  99.           else{                              //無感測到障礙物時
  100.      
  101.               if(ReturnIF1!=0 && S3Output!=0 && ReturnIF3!=0)
  102.               {
  103.                      while(Atimes < Ttimes)     //計算次數(次數(0~3)<目標次數)
  104.                      {        
  105.                             if (ReturnIF1!=0 && S3Output!=0 && ReturnIF3!=0) // 全感白
  106.                              {          
  107.                                 motor_left.run(-255);   //直走
  108.                                 motor_right.run(255);
  109.                                 delay(20);              //0.02秒(未測試)
  110.                                 Atimes++;               //次數+1
  111.                                
  112.                              }
  113.                          else{
  114.                                 Atimes = 0;
  115.                                 break;                  //就跳出
  116.                              }
  117.                      }
  118.  
  119.                       if(Atimes == Ttimes)                  //次數=目標次數
  120.                       {
  121.                              while(ReturnIF1!=0 && S3Output!=0 && ReturnIF3!=0) //假如是白
  122.                              {
  123.                                  Atimes = 0;
  124.                                  motor_left.run(-255);
  125.                                  motor_right.run(255);
  126.                                  //直走
  127.                              }
  128.                       }
  129.               }
  130.  
  131.               if(ReturnIF1==0 && S3Output==0 && ReturnIF3==0)
  132.               {
  133.                    while(Atimes2 < Ttimes2)                 //計算次數(次數(0~3)<目標次數)
  134.                    {
  135.                          if (ReturnIF1==0 && S3Output==0 && ReturnIF3==0) // 全感黑
  136.                              {  
  137.                                   motor_left.run(-255);   //直走
  138.                                   motor_right.run(255);
  139.                                   delay(20);              //0.02秒(未測試)
  140.                                   Atimes2++;              //次數+1
  141.                                  
  142.                              }
  143.                          else{
  144.                                   Atimes2 = 0;
  145.                                   break;                  //就跳出
  146.                              }
  147.                    }
  148.  
  149.                    if(Atimes2 == Ttimes2)                //次數=目標次數
  150.                    {    
  151.                         while(ReturnIF1==0 && S3Output==0 && ReturnIF3==0) //假如是黑
  152.                         {    
  153.                              Atimes2 = 0;
  154.                              motor_left.run(-255);
  155.                              motor_right.run(255);
  156.                             //直走
  157.                         }
  158.                    }
  159.                }
  160.  
  161.    
  162.    
  163.                 //以下為新型循線
  164.                 // ==0 = 黑  !=0 = 白
  165.                 if(ReturnIF1!=0 && S3Output==0 && ReturnIF3!=0)        //ss3感黑 其他感白
  166.                   {  
  167.                     //白黑白
  168.                      motor_left.run(-255);
  169.                      motor_right.run(255);
  170.                      //直走
  171.                   }
  172.                  else if (ReturnIF1==0 && S3Output!=0 && ReturnIF3!=0) // 一二路感黑 其他感白
  173.                   {  
  174.                      //黑白白
  175.                      motor_left.run(0);
  176.                      motor_right.run(255);
  177.                      //往左 (未測試)
  178.                   }
  179.                   else if (ReturnIF1==0 && S3Output==0 && ReturnIF3!=0) // 四五路感白 其他感黑
  180.                   {  
  181.                      //黑黑白
  182.                      motor_left.run(255);
  183.                      motor_right.run(0);
  184.                      //往右(未測試)
  185.                   }
  186.                   else if (ReturnIF1!=0 && S3Output!=0 && ReturnIF3==0) // 四五路感黑 其他感白
  187.                   {
  188.                      //白白黑
  189.                      motor_left.run(0);
  190.                      motor_right.run(255);
  191.                      //往左 (未測試)
  192.                   }
  193.                   else if (ReturnIF1!=0 && S3Output==0 && ReturnIF3==0) // 一二路感白 其他感黑
  194.                   {  
  195.                      //白黑黑
  196.                      motor_left.run(-255);
  197.                      motor_right.run(0);
  198.                      //往右(未測試)
  199.                   }
  200.              }
  201.       }
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement