Advertisement
N3b3x

Setting a servo same value as a 3 pin ping sensor reading

Mar 29th, 2015
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. /* Ping))) Sensor
  2. This sketch reads a PING))) ultrasonic rangefinder and returns the
  3. distance to the closest object in range. To do this, it sends a pulse
  4. to the sensor to initiate a reading, then listens for a pulse
  5. to return. The length of the returning pulse is proportional to
  6. the distance of the object from the sensor.
  7.  
  8. The circuit:
  9. * +V connection of the PING))) attached to +5V
  10. * GND connection of the PING))) attached to ground
  11. * SIG connection of the PING))) attached to digital pin 2
  12.  
  13. http://www.arduino.cc/en/Tutorial/Ping
  14.  
  15. created 3 Nov 2008
  16. by David A. Mellis
  17. modified 30 Jun 2009
  18. by Tom Igoe
  19. modified Nov 2010
  20. by Joe Saavedra
  21. Modified 29 March 2015
  22. by Nebiyu Tadesse
  23. */
  24.  
  25. #include <Servo.h>
  26.  
  27. /* Ping distance measurement - global variables */
  28. int pingPin = 2; //the pin the ping is attached to (change it to whichever you're plugging it to)
  29. long int duration, distanceInches;
  30. int val = 0;
  31. long distanceFront=0; //cm
  32.  
  33. Servo myservo; //state your servo
  34.  
  35. long distanceCm()
  36. {
  37. pinMode(pingPin, OUTPUT); //set the ping sensor as output
  38. digitalWrite(pingPin, LOW); //just to be careful of the values
  39. delayMicroseconds(2); // wait 2 microsecond
  40. digitalWrite(pingPin, HIGH); //send pulses for 5 microsecond
  41. delayMicroseconds(5);
  42. digitalWrite(pingPin, LOW); //set the ping sensor low
  43.  
  44. pinMode(pingPin, INPUT); //set the ping sensor as input to get the readings
  45. duration = pulseIn(pingPin, HIGH); //the duration is the pulse that came back when the ping was one
  46.  
  47. distanceInches = microsecondsToInches(duration); // result from calculations
  48. return microsecondsToCentimeters(duration); // result from calculations
  49. }
  50.  
  51.  
  52. long microsecondsToInches(long microseconds)
  53. {
  54. return microseconds / 74 / 2; //The calculation to get the microsecond and change it to inches
  55. }
  56.  
  57. long microsecondsToCentimeters(long microseconds)
  58. {
  59. return microseconds / 29 / 2; //the calculations to get the microsecond and change it to centimeters
  60. }
  61.  
  62.  
  63. void setup()
  64. {
  65. myservo.attach(8); //the pin the servo is attached to (change the to whichever your pin is attached to)
  66. }
  67.  
  68. void loop()
  69. {
  70. val = distanceCm();
  71. if (val > 1){ // Filters out any stray 0.00 error readings
  72. if (val <= 180) { // The sensor can bring a reading more than 180, but the servo can't go higher than 180
  73. myservo.write(val); //move the servo to the same value as the ping sensor
  74. }
  75.  
  76. else
  77. {
  78. myservo.write(180); //no need for this. Only if you want the servo to stay at 180 is the ping sensor get higher than 180
  79. }
  80. }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement