Advertisement
ossipee

Chick's pieces

Feb 14th, 2014
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.14 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
  10. #define USEcho 11
  11. #define MaxDistance 100
  12. #define LED 13
  13.  
  14. Servo leftServo; //Create Left Servo object
  15. Servo rightServo; //Create Right Servo object
  16. Servo servo; //Create sweep servo for ping
  17. NewPing sonar(USTrigger, USEcho, MaxDistance); //Create ultrasonic object
  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. //Not sure what this actualy means need to do some more googles
  21. unsigned int duration;
  22. unsigned int distance;
  23. unsigned int FrontDistance;
  24. unsigned int LeftDistance;
  25. unsigned int RightDistance;
  26. unsigned int Time;
  27. unsigned int CollisionCounter;
  28.  
  29. void setup()
  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(8); //Right servo connected to pin 8
  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); //Rotate the servo to face the front
  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 > 40 || FrontDistance == 0) //If there is nothing infront of the robot within 40cm or the distance value is 0 (which for the newping libary means no ping was returned) then...
  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. CollisionCounter = CollisionCounter + 1;
  53. moveStop(); //Go to the moveStop function
  54. navigate();
  55. }
  56. }
  57.  
  58. void moveBackward(){
  59. rightServo.write(180);
  60. leftServo.write(0);
  61. }
  62.  
  63. void moveForward(){
  64. rightServo.write(0);
  65. leftServo.write(180);
  66. }
  67.  
  68. void moveRight(){
  69. rightServo.write(0);
  70. leftServo.write(0);
  71. }
  72.  
  73. void moveLeft(){
  74. rightServo.write(180);
  75. leftServo.write(180);
  76. }
  77.  
  78. void moveStop(){
  79. rightServo.write(90);
  80. leftServo.write(95);
  81. }
  82.  
  83. void scan() //This function determines the distance things are away from the ultrasonic sensor
  84. {
  85. Serial.println("");
  86. Serial.println("Scanning");
  87. Time = sonar.ping();
  88. distance = Time / US_ROUNDTRIP_CM;
  89. delay(500);
  90. }
  91. void navigate()
  92. {
  93. Serial.println("There's an obstacle!");
  94. 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)
  95. delay(1000); //Wait half a second for the servo to get there
  96. scan(); //Go to the scan function
  97. LeftDistance = distance; //Set the variable LeftDistance to the distance on the left
  98. Serial.println("Left distance = ");
  99. Serial.print(distance);
  100. servo.write(50); //Move the servo to the right
  101. delay(1000); //Wait half a second for the servo to get there
  102. scan(); //Go to the scan function
  103. RightDistance = distance; //Set the variable RightDistance to the distance on the right
  104. Serial.println("Right distance = ");
  105. Serial.print(distance);
  106. if(abs(RightDistance - LeftDistance) < 5)
  107. {
  108. moveBackward(); //Go to the moveBackward function
  109. delay(200); //Pause the program for 200 milliseconds to let the robot reverse
  110. moveRight(); //Go to the moveRight function
  111. delay(100); //Pause the program for 200 milliseconds to let the robot turn right
  112. }
  113. else if(RightDistance < LeftDistance) //If the distance on the right is less than that on the left then...
  114. {
  115. moveLeft(); //Go to the moveLeft function
  116. delay(100); //Pause the program for half a second to let the robot turn
  117. }
  118. else if(LeftDistance < RightDistance) //Else if the distance on the left is less than that on the right then...
  119. {
  120. moveRight(); //Go to the moveRight function
  121. delay(100); //Pause the program for half a second to let the robot turn
  122. }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement