Advertisement
ossipee

Chibajoss with assits

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