Advertisement
Guest User

Untitled

a guest
Jan 18th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. #include <Servo.h>
  2.  
  3. Servo Left;
  4. Servo Right;
  5. int echo = 2;
  6. int trigger = 3;
  7. long distance;
  8. long duration;
  9.  
  10. void setup(){
  11.  
  12. Serial.begin(9600);
  13. Left.attach(12);
  14. Right.attach(13);
  15. pinMode(trigger, OUTPUT);
  16. pinMode(echo, INPUT);
  17. }
  18.  
  19. void moveForward(int time)
  20. {
  21. Left.writeMicroseconds(1700);
  22. Right.writeMicroseconds(1300);
  23. delay(time);
  24. }
  25.  
  26. void moveLeft(int time)
  27. {
  28. Left.writeMicroseconds(1300);
  29. Right.writeMicroseconds(1300);
  30. delay(time);
  31. }
  32.  
  33. void moveRight(int time)
  34. {
  35. Left.writeMicroseconds(1700);
  36. Right.writeMicroseconds(1700);
  37. delay(time);
  38. }
  39.  
  40. void moveBackward(int time)
  41. {
  42. Left.writeMicroseconds(1300);
  43. Right.writeMicroseconds(1700);
  44. delay(time);
  45. }
  46.  
  47. void loop(){
  48.  
  49. digitalWrite(trigger, HIGH);
  50. delay(10);
  51. digitalWrite(trigger, LOW);
  52.  
  53. duration = pulseIn(echo, HIGH);
  54. distance = (duration/2) / 29.1;
  55.  
  56. Serial.println(distance);
  57.  
  58. if(distance < 15) {
  59. moveBackward(2000);
  60. }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement