Advertisement
a5768549

new(20180523)

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