daniel_lawson9999

Distance Sensor Test

Jan 30th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1.  
  2. //This is the example program from the distance sensor library I found, it worked
  3. #include <NewPing.h>
  4.  
  5. #define TRIGGER_PIN 13 // Arduino pin tied to trigger pin on the ultrasonic sensor.
  6. #define ECHO_PIN 12 // Arduino pin tied to echo pin on the ultrasonic sensor.
  7. #define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
  8.  
  9. NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
  10.  
  11. void setup() {
  12. Serial.begin(9600); // Open serial monitor at 115200 baud to see ping results.
  13. }
  14.  
  15. void loop() {
  16. delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
  17. unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
  18. //Serial.println(uS);
  19. Serial.print("Ping: ");
  20. Serial.print(uS / US_ROUNDTRIP_CM); // Convert ping time to distance in cm and print result (0 = outside set distance range)
  21. //Serial.print(uS);
  22. Serial.println("cm");
  23. }
Advertisement
Add Comment
Please, Sign In to add comment