Advertisement
a5768549

Untitled

May 24th, 2018
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.06 KB | None | 0 0
  1. #include <Arduino.h>
  2. #include <Wire.h>
  3. #include <SoftwareSerial.h>
  4. #include <MeMCore.h>
  5. #include <Timer.h>
  6. MBotDCMotor motor_left(9);
  7. MBotDCMotor motor_right(10);
  8. MeLineFollower lfer(2);
  9. MeUltrasonicSensor UTS(3);
  10. MeBuzzer buzzer;                //嗡鳴器
  11. byte ReturnValue;
  12. int distance;
  13. bool Buttomif =false;            //設定按下按鈕後的判斷條件
  14. int buttom;                     //設定一個參數為buttom
  15. void setup()
  16. {  
  17.       buzzer.tone(262,200);
  18.       buzzer.tone(294,200);
  19.       buzzer.tone(330,200);        //輸出已(重)開機訊號
  20.  
  21.   }
  22. void loop()
  23. {
  24.   if(buttom<10)                //如果按鈕按下去
  25.       {
  26.           Buttomif = true;
  27.       }
  28.       if(Buttomif == true)
  29.       {
  30.   ReturnValue=lfer.readSensors();
  31.   distance=UTS.distanceCm();
  32.      buttom=analogRead(A7);         //設定buttom為板載按鈕
  33.    if(distance <= 5)          //避障
  34.    {
  35.     motor_left.run(0);
  36.     motor_right.run(0);
  37.     delay(500);
  38.     motor_left.run(0);
  39.     motor_right.run(-100);
  40.     delay(1000);
  41.     motor_left.run(-200);
  42.     motor_right.run(200);
  43.     delay(800);
  44.     motor_left.run(0);
  45.     motor_right.run(200);
  46.     delay(800);
  47.     motor_left.run(-200);
  48.     motor_right.run(200);
  49.     delay(500);
  50.     motor_left.run(-200);
  51.     motor_right.run(-100);
  52.     delay(1000);
  53.    }else
  54.    {
  55.     if(ReturnValue==0)
  56. {
  57.   motor_left.run(-150);
  58.   motor_right.run(150);
  59.   }
  60.  
  61. if(ReturnValue==1)                         //左轉
  62. {
  63.   motor_left.run(-50);
  64.   motor_right.run(200);
  65.   while(lfer.readSensors()==1);
  66.   };
  67.   if(ReturnValue==2){                       //右轉
  68.     motor_left.run(-200);
  69.     motor_right.run(50);
  70.     while(lfer.readSensors()==2);
  71.   }
  72.   if(ReturnValue==3)                         //遇到白白
  73.   {
  74.     motor_left.run(-150);
  75.     motor_right.run(100);
  76.     delay(300);
  77.     motor_left.run(150);                      //若無則左轉直到感測到黑
  78.     motor_right.run(-150);
  79.     while(lfer.readSensors()==3);
  80.   }
  81.    
  82.    //循線感測器
  83.     }
  84.       }
  85.       }
  86.   //以上為超音波感測器
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement