Graymist

Sweep and Ping

Feb 22nd, 2014
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.90 KB | None | 0 0
  1. #include <Servo.h>
  2. #include <NewPing.h>
  3.  
  4. // Combining servo control with sensor ping
  5.  
  6. #define trigPin 5
  7. #define echoPin 4
  8. #define Max_Dist 200
  9.  // Servo pin 9
  10.  
  11. NewPing sonar(trigPin, echoPin, Max_Dist);
  12.  
  13. Servo myservo;
  14.  
  15. int pos = 90;
  16.  
  17. void setup()
  18. {
  19.   Serial.begin (9600);
  20.   myservo.attach(11);
  21.   myservo.write(90);
  22.   delay(1000);
  23.  
  24. }
  25.  
  26. void loop()
  27. {
  28.     myservo.write(36);  // check distance to the right
  29.     delay(500);
  30.     int rightDistance = sonar.ping_cm(); //set right distance
  31.     delay(500);
  32.     myservo.write(144);  // check distace to the left
  33.     delay(700);
  34.     int leftDistance = sonar.ping_cm(); //set left distance
  35.     delay(500);
  36.     myservo.write(90); //return to center
  37.     delay(100);
  38.    
  39.   Serial.print("Right: ");
  40.   Serial.print(rightDistance);
  41.   Serial.print(" cm  ");
  42.   Serial.print("Left: ");
  43.   Serial.print(leftDistance);
  44.   Serial.println(" cm");
  45. }
Advertisement
Add Comment
Please, Sign In to add comment