Getting 0 cm from ultrasonic sensor when nothing is in front
The documentation for the NewPing library states:


  Constructor

NewPing sonar(trigger_pin, echo_pin [, max_cm_distance]);



and


  max_cm_distance is optional [default = 500cm].


Then:


  sonar.ping_cm(); - Send a ping, returns the distance in centimeters or
  0 (zero) if no ping echo within set distance limit


So it would seem that if there is nothing in front of your sensor closer than 500cm, this library returns 0cm. Probably not the best design, but at least it is disclosed.

Thus, you need to write your code such that a 0cm measurement is treated as no obstruction visible; more like an infinite cm measurement.