Advertisement
Guest User

Untitled

a guest
May 20th, 2018
113
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.  
  8. //自訂參數
  9. MBotDCMotor motor_left(9);
  10. MBotDCMotor motor_right(10);
  11. MeLineFollower IF1(2);    // ss1 ss2
  12. MeLineFollower IF2(1);    // ss3
  13. MeLineFollower IF3(4);    // ss4 ss5
  14. MeUltrasonicSensor UTS(3);//超音波
  15. MeBuzzer buzzer; //嗡鳴器
  16. byte ReturnIF1;
  17. byte ReturnIF2;
  18. byte ReturnIF3;
  19. int buttom;
  20. int distance;   //設定一個參數為distance
  21. bool times = false;       //設定一個是否值
  22. const int Ttimes = 3;      //目標計算次數
  23. bool times2 = false;       //設定一個是否值
  24. const int Ttimes2 = 3;      //設定計算次數
  25. unsigned long StartTime = 0;
  26. unsigned long WhiteTime = 0;
  27. unsigned long BlackTime = 0;
  28. void setup()
  29. {
  30.   Serial.begin(115200);     //串列阜(之後要刪除)
  31.   buzzer.tone(262,200);
  32.   buzzer.tone(294,200);
  33.   buzzer.tone(330,200);    //輸出已開機訊號
  34.   }
  35. void loop()
  36. {
  37.   buttom=analogRead(A7);         //設定buttom為板載按鈕
  38.   ReturnIF1=IF1.readSensors();
  39.   ReturnIF2=IF2.readSensors();
  40.   ReturnIF3=IF3.readSensors();   //設定五路的感測資料位置
  41.   distance=UTS.distanceCm();     //令超音波資料為distance
  42.   int Atimes = 0;
  43.   int Atimes2 = 0;
  44.   Serial.println(distance);       //串列阜(之後要刪除)
  45.  
  46.    if(buttom<10){                //按鈕按下去後執行
  47.  
  48.    if(!(distance) < 5){          //距離小於5cm
  49.     Serial.println("closer");     //輸出距離太小(之後要刪除)
  50.     motor_left.run(255);
  51.     motor_right.run(-255);
  52.     delay(200);   //後退
  53.     motor_left.run(-55);
  54.     motor_right.run(255);
  55.     delay(500);    //左轉
  56.     motor_left.run(-255);
  57.     motor_right.run(255);
  58.     delay(200);    //前進
  59.     motor_left.run(-255);
  60.     motor_right.run(55);
  61.     delay(700);    //右轉
  62.     motor_left.run(-255);
  63.     motor_right.run(255);
  64.     delay(150);    //接線時請調150,鋰電池時請調250(前進)
  65.     motor_left.run(255);
  66.     motor_right.run(0);
  67.     delay(680);    //回復路線
  68.     }else{
  69.     Serial.println("far");                 //輸出距離太遠(之後要刪除)
  70.  
  71.  
  72.     if(times){          
  73.     times = true;       //開始計算
  74.     while(Atimes <Ttimes){        //計算次數(次數(0~3)<目標次數)
  75.     if (ReturnIF1==0 && ReturnIF2==0 && ReturnIF3==0) // 全感黑
  76.    
  77.      {  
  78.        break;       //就跳出
  79.      }
  80.      else
  81.      {
  82.         motor_left.run(-255);   //直走
  83.         motor_right.run(255);
  84.         delay(20);              //0.02毫秒(未測試)
  85.         Atimes++;               //次數+1
  86.       }
  87.       }
  88.  
  89.       if(Atimes == Ttimes){     //次數=目標次數
  90.         while(ReturnIF1!=0 && ReturnIF2!=0 && ReturnIF3!=0) //假如是白
  91.         {
  92.           motor_left.run(-255);
  93.           motor_right.run(255);
  94.          //直走
  95.           }
  96.       }
  97.     times = false;       //結束計算
  98.     }
  99.  
  100.  
  101.  
  102.     if(times2){          
  103.     times2 = true;       //開始計算
  104.     while(Atimes2 <Ttimes2){        //計算次數(次數(0~3)<目標次數)
  105.     if (ReturnIF1!=0 && ReturnIF2!=0 && ReturnIF3!=0) // 全感白
  106.    
  107.      {  
  108.        break;       //就跳出
  109.      }
  110.      else
  111.      {
  112.         motor_left.run(-255);   //直走
  113.         motor_right.run(255);
  114.         delay(20);              //0.02毫秒(未測試)
  115.         Atimes2++;               //次數+1
  116.       }
  117.       }
  118.  
  119.       if(Atimes2 == Ttimes2){     //次數=目標次數
  120.         while(ReturnIF1==0 && ReturnIF2==0 && ReturnIF3==0) //假如是黑
  121.         {
  122.           motor_left.run(-255);
  123.           motor_right.run(255);
  124.          //直走
  125.           }
  126.       }
  127.     times2 = false;       //結束計算
  128.     }
  129.    
  130.     //以下為新型循線
  131.     // ==0 = 黑  !=0 = 白
  132.     if(ReturnIF1!=0 && ReturnIF2==0 && ReturnIF3!=0)        //ss3感黑 其他感白
  133.       {  
  134.         //白黑白
  135.          motor_left.run(-255);
  136.          motor_right.run(255);
  137.          //直走
  138.       }
  139.      else if (ReturnIF1==0 && ReturnIF2!=0 && ReturnIF3!=0) // 一二路感黑 其他感白
  140.      {  
  141.          //黑白白
  142.          motor_left.run(0);
  143.          motor_right.run(255);
  144.          //往左 (未測試)
  145.       }
  146.       else if (ReturnIF1==0 && ReturnIF2==0 && ReturnIF3!=0) // 四五路感白 其他感黑
  147.      {  
  148.          //黑黑白
  149.          motor_left.run(-255);
  150.          motor_right.run(0);
  151.          //往右(未測試)
  152.       }
  153.       else if (ReturnIF1!=0 && ReturnIF2!=0 && ReturnIF3==0) // 四五路感黑 其他感白
  154.      {
  155.          //白白黑
  156.          motor_left.run(0);
  157.          motor_right.run(255);
  158.          //往左 (未測試)
  159.       }
  160.       else if (ReturnIF1!=0 && ReturnIF2==0 && ReturnIF3==0) // 一二路感白 其他感黑
  161.      {  
  162.          //白黑黑
  163.          motor_left.run(-255);
  164.          motor_right.run(0);
  165.          //往右(未測試)
  166.       };
  167.       }
  168.     }
  169.     };
  170.   //以上為超音波感測器
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement