You only read the sensor once and that is before setup() so the result of the reading is suspect. Try reading the sensor in the loop() function.
void loop() {
delay(50);
int d = sonar.ping_cm();
Serial.println(d);
}
You only read the sensor once and that is before setup() so the result of the reading is suspect. Try reading the sensor in the loop() function.
void loop() {
delay(50);
int d = sonar.ping_cm();
Serial.println(d);
}