SR04 basic robot (avoids obstacles)

Hi guys, sorry if I do something wrong but I am a beginner. I has a basic chasis with 2 motors, a SR04 and a Nano board. I am trying to spin only one wheel if the distance to an object is less than 10 cm. I can read the distance in the Serial Monitor by its self but when I connect the motors the distance goes to 0 and the wheels keep spinning forever. I am not sure what is wrong with my code.

I attached my code and a picture similar to my setup.

In my code this should be seen:
D3 pin is trigger for SR04
D2 pin is echo for SR04

D7 and D8 pin goes to the middle pin of the 2222A transistor, so it's an output.

Thanks.

robot.ino (1.11 KB)

Powering motors from the arduino 5v pin is bad.
Much better to power the motors from a separate power supply, like 4 AA batteries.

I am not sure what is wrong with my code.

The delay in loop() is pointless. It is only necessary to set the echo pin to INPUT mode once, not on every pass through loop().

Does writing HIGH to the motor pin really turn the motor off? Where do you turn them on?

Your Fritzing diagram does not agree with your description of the connections or the code.

You need to fix it, and the source of power that drives the motors.