need help with code

I haven't taken the time to review your whole sketch, but it seems to me there are three parts to your problem and you could test them separately to see which ones work and which ones don't.

You should be able to read the distance sensor repeatedly and see the return value change sensibly as you move obstacles around in front of the sensor. Just use Serial.println() to output the sensor results via USB.

You should be able to start and stop the motors and run them forward and backward separately and together. Just program a fixed sequence and confirm that the motors both work in both directions and you don't have any electrical problems causing the board to reset or crash.

In order to minimise the changes needed between your test code and your final sketch I suggest you encapsulate these separate operations in 'C' functions (for example forwards(), turnRight(), stop(), measureDistance()).

You need an algorithm to decide what the motors need to do based on the sensor input. This is the last part to test and relies on the other two parts already working. The code here will be relatively simple, but you should aim to structure it so that your driving algorithm is separated from your functions that interface to the sensor and motors and don't let the separate bits of code all get jumbled together.