Advertisement
ossipee

new ping 1 pin

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