Help please to improve this obstacle avoiding robot project.

It's handy to put some serial print statements in your code

Serial.print("turning left");

or

Serial.print(yourvariable);

and set your robot with its wheels up, usb cable attached, and watch what parts of the code and choices are being accessed and when. You could have it serial print out your variable, for instance, and watch whether the expected or unexpected are going on.