Advertisement
Guest User

Untitled

a guest
Apr 25th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.30 KB | None | 0 0
  1.  
  2.  
  3. #include <Servo.h>                                  //includes the servo library
  4.  
  5. int motor_pin1 = 4;
  6. int motor_pin2 = 5;
  7. int motor_pin3 = 6;
  8. int motor_pin4 = 7;
  9. int servopin = 8;
  10. int sensorpin = 0;
  11. int dist = 0;
  12. int leftdist = 0;
  13. int rightdist = 0;
  14. int object = 500;             //distance at which the robot should look for another route                          
  15.  
  16. Servo myservo;
  17.  
  18. void setup ()
  19. {
  20.   pinMode(motor_pin1,OUTPUT);
  21.   pinMode(motor_pin2,OUTPUT);
  22.   pinMode(motor_pin3,OUTPUT);
  23.   pinMode(motor_pin4,OUTPUT);
  24.   myservo.attach(servopin);
  25.   myservo.write(90);
  26.   delay(700);
  27. }
  28. void loop()
  29. {
  30.   dist = analogRead(sensorpin);               //reads the sensor
  31.  
  32.   if(dist < object) {                         //if distance is less than 550
  33.    forward();                                  //then move forward
  34.   }
  35.   if(dist >= object) {               //if distance is greater than or equal to 550
  36.     findroute();
  37.   }
  38. }
  39.  
  40. void forward() {                            // use combination which works for you
  41.    digitalWrite(motor_pin1,HIGH);
  42.    digitalWrite(motor_pin2,LOW);
  43.    digitalWrite(motor_pin3,HIGH);
  44.    digitalWrite(motor_pin4,LOW);
  45.    return;
  46.  }
  47.  
  48. void findroute() {
  49.   halt();                                             // stop
  50.   backward();                                       //go backwards
  51.   lookleft();                                      //go to subroutine lookleft
  52.   lookright();                                   //go to subroutine lookright
  53.                                      
  54.   if ( leftdist < rightdist )
  55.   {
  56.     turnleft();
  57.   }
  58.  else
  59.  {
  60.    turnright ();
  61.  }
  62. }
  63.  
  64. void backward() {
  65.   digitalWrite(motor_pin1,LOW);
  66.   digitalWrite(motor_pin2,HIGH);
  67.   digitalWrite(motor_pin3,LOW);
  68.   digitalWrite(motor_pin4,HIGH);
  69.   delay(500);
  70.   halt();
  71.   return;
  72. }
  73.  
  74. void halt () {
  75.   digitalWrite(motor_pin1,LOW);
  76.   digitalWrite(motor_pin2,LOW);
  77.   digitalWrite(motor_pin3,LOW);
  78.   digitalWrite(motor_pin4,LOW);
  79.   delay(500);                          //wait after stopping
  80.   return;
  81. }
  82.  
  83. void lookleft() {
  84.   myservo.write(150);
  85.   delay(700);                                //wait for the servo to get there
  86.   leftdist = analogRead(sensorpin);
  87.   myservo.write(90);
  88.   delay(700);                                 //wait for the servo to get there
  89.   return;
  90. }
  91.  
  92. void lookright () {
  93.   myservo.write(30);
  94.   delay(700);                           //wait for the servo to get there
  95.   rightdist = analogRead(sensorpin);
  96.   myservo.write(90);                                  
  97.   delay(700);                        //wait for the servo to get there
  98.   return;
  99. }
  100.  
  101. void turnleft () {
  102.   digitalWrite(motor_pin1,HIGH);       //use the combination which works for you
  103.   digitalWrite(motor_pin2,LOW);      //right motor rotates forward and left motor backward
  104.   digitalWrite(motor_pin3,LOW);
  105.   digitalWrite(motor_pin4,HIGH);
  106.   delay(1000);                     // wait for the robot to make the turn
  107.   halt();
  108.   return;
  109. }
  110.  
  111. void turnright () {
  112.   digitalWrite(motor_pin1,LOW);       //use the combination which works for you
  113.   digitalWrite(motor_pin2,HIGH);    //left motor rotates forward and right motor backward
  114.   digitalWrite(motor_pin3,HIGH);
  115.   digitalWrite(motor_pin4,LOW);
  116.   delay(1000);                              // wait for the robot to make the turn
  117.   halt();
  118.   return;
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement