DC Motor Control with L293D Motor Driver and ultrasonic sensor

I’ve written code for mobile robot with two DC motor, L293D Motor Driver and ultrasonic sensor, which should go forward, as long as the path is clear for min 10 cm. If something is on the way it should stop.

I used a LED light to test the ultrasonic sensor and it works properly - the LED light turns ON, but the motors wouldn’t stop when there is a obstacle in less than 10 cm.

Can you please review my code (which is attached) and tell me where did I make a mistake.

DC_Motor_Control_with_L293D_Motor_Driver.ino (965 Bytes)

I solved it.

First I haven’t noticed that I’ve have written
if((distance<=10))
{
digitalWrite(leftBackward , LOW);
digitalWrite(rightBackward , LOW);
digitalWrite(LED , HIGH);
delay(250);
}
, instead of

if((distance<=10))
{
digitalWrite(leftForward , LOW);
digitalWrite(rightForward , LOW);
digitalWrite(LED , HIGH);
delay(250);
}.

Second, I added delay(250) in the second condition.

else if(distance>10)
{
digitalWrite(leftForward , HIGH);
digitalWrite(rightForward , HIGH);
digitalWrite(LED , LOW);
delay(250);
}

And now it is working properly.

I’ll attach the whole code, if someone has similar struggle. :slight_smile:

DC_Motor_Control_with_L293D_Motor_Driver.ino (979 Bytes)

tanjamarathon:
I’ve written code for mobile robot with two DC motor, L293D Motor Driver and ultrasonic sensor, which should go forward, as long as the path is clear for min 10 cm. If something is on the way it should stop.

I used a LED light to test the ultrasonic sensor and it works properly - the LED light turns ON, but the motors wouldn’t stop when there is a obstacle in less than 10 cm.

Can you please review my code (which is attached) and tell me where did I make a mistake.

Can you please share your circuit diagram? It will help me a lot.!