Advertisement
ossipee

Chix_Oss

Feb 21st, 2014
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.03 KB | None | 0 0
  1. // Taken from ChickenParmis fantastic site http://www.duino-robotics.com/
  2.  
  3.  
  4.  
  5. // add some libraries for ping and servo
  6. #include <Servo.h>
  7. #include <NewPing.h>
  8.  
  9. #define USTrigger 10       //picks ping trigger pin
  10. #define USEcho 11         //picks ping recievor pin, signal in
  11. #define MaxDistance 100  // sets pings maximum range
  12. #define LED 13           // picks pin 13 for included led on board
  13.  
  14. Servo leftServo;       //Create Left Servo object, lets arduino program know it has a left servo
  15. Servo rightServo;      //Create Right Servo object, lets arduino program know it has right servo
  16. Servo servo;          //Create sweep Servo object, lets arduino program know it has a servo to sweep ping
  17. NewPing sonar(USTrigger, USEcho, MaxDistance); //Create ultrasonic object, arduino now know it got a ping
  18.  
  19. //Below we are creating unsigned integer variables which we will use later on in the code. They are unsigned as they will only have postive values
  20. //if you sign them they can have positive and negitive values, thanks 6677, Birdmun also says that just using positive ones saves program space
  21. unsigned int duration;                //this is going to be time between sending and getting signal back from ping, gonna use it to make a distance
  22. unsigned int distance;                //this is going to be the distance in cm we get from ping
  23. unsigned int FrontDistance;           //this is the number we get from a ping reading looking straight ahead
  24. unsigned int LeftDistance;            //this is the number we get from a ping reading looking to the left, check servo is really looking left
  25. unsigned int RightDistance;           // same as above but looking right
  26. unsigned int Time   ;
  27.  
  28. void setup()                  //This part of the program runs 1 time at start up
  29. {
  30.   leftServo.attach(9);        //Left servo connected to pin 9
  31.   leftServo.write(90) ;       //Write the neutral position to that servo
  32.   rightServo.attach(7);       //Right servo connected to pin 7
  33.   rightServo.write(90);       //Write the neutral position to that servo
  34.   servo.attach (6);          // puts ping servo on pin 6
  35. }
  36.  
  37.  
  38. void loop()                                           //This block repeats itself while the Arduino is turned on
  39. {
  40.   servo.write(90);                                    //Rotate the servo to face the front                  
  41.   scan();                                             //Go to the scan function
  42.   FrontDistance = distance;                           //Set the variable FrontDistance to the value of the distance returned from the scan function
  43.   Serial.println("Front distance = ");
  44.   Serial.print(distance);
  45.   if(FrontDistance > 20 || FrontDistance == 0){       //If there is nothing infront of the robot within 20cm or the distance value is 0 (which for the newping libary means no ping was returned) then.
  46.  
  47.  
  48.    moveForward();                                     //Go to the moveForward function
  49.   }
  50.   else                                                //Else (if there is something infront of the robot within 40cm) then...
  51.   {
  52.     moveStop();                                       //Go to the moveStop function
  53.     navigate();                                       //Go to navigate function
  54.   }
  55. }
  56.  
  57. void moveBackward(){
  58.   rightServo.write(180);
  59.   leftServo.write(0);
  60. }
  61.  
  62. void moveForward(){
  63.   rightServo.write(0);
  64.   leftServo.write(180);
  65. }
  66.  
  67. void moveRight(){
  68.   rightServo.write(0);
  69.   leftServo.write(0);  
  70. }
  71.  
  72. void moveLeft(){
  73.   rightServo.write(180);
  74.   leftServo.write(180);
  75. }
  76.  
  77. void moveStop(){
  78.   rightServo.write(90);
  79.   leftServo.write(90);
  80. }
  81.  
  82. void scan(){                                        //This function determines the distance things are away from the ultrasonic sensor{
  83.   Serial.println("");
  84.   Serial.println("Scanning");
  85.   Time = sonar.ping();
  86.   distance = Time / US_ROUNDTRIP_CM;
  87.   delay(500);
  88. }
  89. void navigate()
  90. {
  91.     Serial.println("There's an obstacle!");
  92.     servo.write(130);                                 //Move the servo to the left (my little servos didn't like going to 180 so I played around with the value until it worked nicely)
  93.     delay(1000);                                       //Wait half a second for the servo to get there
  94.     scan();                                           //Go to the scan function
  95.     LeftDistance = distance;                          //Set the variable LeftDistance to the distance on the left
  96.     Serial.println("Left distance = ");
  97.     Serial.print(distance);
  98.     servo.write(50);                                   //Move the servo to the right
  99.     delay(1000);                                       //Wait half a second for the servo to get there
  100.     scan();                                           //Go to the scan function
  101.     RightDistance = distance;                         //Set the variable RightDistance to the distance on the right
  102.     Serial.println("Right distance = ");
  103.     Serial.print(distance);
  104.     if(abs(RightDistance - LeftDistance) < 5)
  105.     {
  106.       moveBackward();                                  //Go to the moveBackward function
  107.       delay(200);                                      //Pause the program for 200 milliseconds to let the robot reverse
  108.       moveRight();                                     //Go to the moveRight function
  109.       delay(100);                                      //Pause the program for 200 milliseconds to let the robot turn right
  110.     }
  111.     else if(RightDistance < LeftDistance)                  //If the distance on the right is less than that on the left then...
  112.     {
  113.      moveLeft();                                      //Go to the moveLeft function
  114.      delay(100);                                      //Pause the program for half a second to let the robot turn
  115.     }
  116.     else if(LeftDistance < RightDistance)             //Else if the distance on the left is less than that on the right then...
  117.     {
  118.      moveRight();                                     //Go to the moveRight function
  119.      delay(100);                                      //Pause the program for half a second to let the robot turn
  120.     }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement