I thought the NewPing library would be best as it allows me to use 3 pins instead of the sensor's 4.
Are you using the built in ultrasonic sensor? Or are you adding a sensor? The 3 pin code expects a three pin sensor. You can't just expect to ignore a pin.
If you can read data from the sensor now, using ONE pin, it hardly makes sense to change to another sensor that uses more pins.
PS, just noticed when calculating the ultrasound measurements 40 equates to roughly 20cm, not an error.
Then, that is what the comment should point it.
It still isn't clear what the robot is actually doing, or how that differs from what you want it to do. Pretend we can't see the robot, because we can't.