NewPing for ultrasonic sensor


im just a beginner in Arduino and i bought my ultrasonic four pin sensor with Arduino UNO and many other things.

Im using the new ping library for my ultrasonic sensor and i wonder how we can use the if statement in the
new ping library.

Here is the code:

#include <NewPing.h>

#define TRIGGER_PIN  12                // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN  11                   // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins .

void setup() {
  Serial.begin(9600); // Open serial monitor at 9600 baud to see ping results.

void loop() {
  delay(50);       // Wait 50ms between pings ( 20 pings/sec). 29ms should be the shortest delay between pings.
  unsigned int uS =; // Send ping, get ping time in microseconds (uS).
  Serial.print("Ping: ");
  Serial.print(uS / US_ROUNDTRIP_CM); // Convert ping time to distance and print result (0 = outside set distance range, no ping echo)

i tryed to google it but i did not find any simple resluts.
So someone tell me.



thanks for the reply and it actually worked! i changed the conversion from to sonar.ping_cm().

Then i wrote the if statement to write (table) in the serial monitor if the distance is 10 cm and it worked!