3 ultrasonic sensor with L293 Dual motor driver

I have 3 Ultrasonic and L293 motor driver controller. I want to do autonomous robot toy car to run. Sensors are Front, left and right please help. Here is my program but only front works, and right side only speed up.

#define vel_motor_left 10 // left motor speed control ;
#define vel_motor_right 11 // right motor speed control ;
#define in3 8 // It controls the direction of rotation of the left motor ;
#define in4 9 // It controls the direction of rotation of the left motor ;
#define in1 12 // controls direction of rotation of the right engine;
#define in2 7 // controls direction of rotation of the right engine;

int trigger_front = A4; // controls the impulse sent from the front sensor
int echo_front = A5; // controls the pulse received from the front sensor

int trigger_left = A2; // controls the impulse sent from the left sensor
int echo_left = A3; // controls the pulse received from the left sensor

int trigger_right = A0;// controls the impulse sent from the right sensor
int echo_right = A1;//controls the pulse received from the right sensor

// configuration of kinds of input variables declared ;
void setup()
{
pinMode(trigger_front, OUTPUT); //signal output from the Arduino trigger_front
pinMode(echo_front, INPUT);// signal input of Arduino echo_font

pinMode(trigger_left, OUTPUT);//signal output from the Arduino trigger_left
pinMode(echo_left, INPUT);// signal input of Arduino echo_left

pinMode(trigger_right, OUTPUT); // signal output from the Arduino trigger_right
pinMode(echo_right, INPUT); // signal input of Arduino echo_right

pinMode(vel_motor_left, OUTPUT);// signal output from the Arduino 's left engine speed
pinMode(vel_motor_right, OUTPUT);// signal output of the right motor speed Arduino

pinMode(in1, OUTPUT);// Arduino signal output control the direction of rotation of the left engine
pinMode(in2, OUTPUT);//Arduino signal output control the direction of rotation of the left engine
pinMode(in3, OUTPUT);// Arduino signal output control the direction of rotation of the right engine
pinMode(in4, OUTPUT);// Arduino signal output control the direction of rotation of the right engine
delay(5000);
}

// code in the endless repetition;
void loop()
{
// declaration of variables used for project control;
long duration_front, duration_left, duration_right, front, left, right;

digitalWrite(trigger_front, LOW); // It is declared their inputs and signal outputs
delayMicroseconds(2); // ultrasonic sensor and stored by the variable sensor
digitalWrite(trigger_front, HIGH); // converting the speed of sound is 340 m / s or
delayMicroseconds(5); // 29 microseconds per centimeter , as the signal comes and goes
digitalWrite(trigger_front, LOW); // this time is the sensor half being = Time / 29 /2;
duration_front = pulseIn(echo_front, HIGH); // so also follows the other two sensors.
front = duration_front/29/2;

digitalWrite(trigger_left, LOW);
delayMicroseconds(2);
digitalWrite(trigger_left, HIGH);
delayMicroseconds(5);
digitalWrite(trigger_left, LOW);
duration_left = pulseIn(echo_left, HIGH);
left = duration_left/29/2;

digitalWrite(trigger_right, LOW);
delayMicroseconds(2);
digitalWrite(trigger_right, HIGH);
delayMicroseconds(5);
digitalWrite(trigger_right, LOW);
duration_right = pulseIn(echo_right, HIGH);
right = duration_right/29/2;

analogWrite(vel_motor_left, 0); //block to initialize the entries with pulse 0 or off ;
analogWrite(vel_motor_right, 0); //
analogWrite(in1, 0); //
analogWrite(in2, 0); //
analogWrite(in3, 0); //
analogWrite(in4, 0); //

if(front >8) // if in case there is clear path forward he follows this logic below:
{
// the use of the four down inside if’s if this is to control the handling robot ,
// in order to keep it following the right wall straight;

if(right >7 && left< 8) // Forward
{
analogWrite(vel_motor_left, 120);
analogWrite(vel_motor_right,200);

analogWrite(in1, 150);
analogWrite(in2, 0);
analogWrite(in3, 0);
analogWrite(in4,150);
}

if(left >=9) //Left
{
analogWrite(vel_motor_left,120);
analogWrite(vel_motor_right, 200);

analogWrite(in1, 150);
analogWrite(in2, 0);
analogWrite(in3, 0);
analogWrite(in4, 150);
}

if(right <=12) //Right
{
analogWrite(vel_motor_left, 120);
analogWrite(vel_motor_right, 200);

analogWrite(in1, 150);
analogWrite(in2, 0);
analogWrite(in3, 0);
analogWrite(in4,150);
}
}

if(left <=20 && right>20 && front <=8) dir(); //is forward if the distance is less than or equal to 8 cm
//the right distance is greater than 20 cm and the distance
//the left is less than or equal to 20 cm it calls the dir () function;

if(left >20 && right>20 && front <=8) dir(); //s forward if the distance is less than or equal to 8 cm
//the right distance is greater than 20 cm and the distance
//the left is greater than 20 cm it calls the dir () function;

if(right <=20 && left>20 && front <=8) esq(); //is forward if the distance is less than or equal to 8 cm
//the right distance is less than or equal to 20 cm and the distance
//the left is larger than 20 cm it calls the Left () function;

if(right<=20 && left<=20 && front<=8) voltar(); // forward if the distance is less than or equal to 8 cm
//the right distance is less than or equal to 20 cm and the distance
//he left is less than or equal to 20 cm it calls the function back ();

}

void esq() // function for making the robot 90 rotate to the left if there is output from the front and right ;
{

analogWrite(vel_motor_left, 180);
analogWrite(vel_motor_right, 150);

analogWrite(in1, 0);
analogWrite(in2, 150);
analogWrite(in3, 0);
analogWrite(in4, 150);
delay(700);

}

void dir() // function to make the robot turn 90 degrees to the right if you have no way out the front or the left;
{

analogWrite(vel_motor_left, 150);
analogWrite(vel_motor_right, 150);

analogWrite(in1, 150);
analogWrite(in2, 0);
analogWrite(in3, 150);
analogWrite(in4, 0);
delay(800);

}

void voltar() // function to make the robot turn 180 if he did not have exit the front , right and left;
{

analogWrite(vel_motor_left,150);
analogWrite(vel_motor_right, 150);

analogWrite(in1, 150);
analogWrite(in2, 0);
analogWrite(in3, 150);
analogWrite(in4, 0);
delay(1200);

}

I am new to this please help

Which Arduino board are you using? On any board that uses the Mega328 (Uno), only pins 3, 5, 6, 9, 10 and 11 are capable of PWM (analogWrite).

Please read the 'how to use the forum' stickies. Especially the post code using code tags part.

if(front >8) // if in case there is clear path forward he follows this logic below:
will read correctly in code tags

if(front >8) // if in case there is clear path forward he follows this logic below:
     digitalWrite(trigger_front, LOW);  // It is declared their inputs and signal outputs
     delayMicroseconds(2);               // ultrasonic sensor and stored by the variable sensor
     digitalWrite(trigger_front, HIGH); // converting the speed of sound is 340 m / s or 
     delayMicroseconds(5);               // 29 microseconds per centimeter , as the signal comes and goes
     digitalWrite(trigger_front, LOW);  // this time is the sensor half being = Time / 29 /2;
     duration_front = pulseIn(echo_front, HIGH); // so also follows the other two sensors.
     front = duration_front/29/2;

Any time you start cutting and pasting code, you need a function.

Do NOT go any further until you have created a function that takes two pin numbers and returns a distance.

The distance values should not be global. They are used ONLY by loop.

Hi :slight_smile:

I think you misinterpreted the usage of the L293.

The in1, in2, ... pins are digital outputs

controlling the direction of the attached motor(s).

PWM can be used via the enable pin.

There should be librarys and examples for that chip.

(I'm using a L298 wich seems to be nearly identical).

The in1, in2, ... pins are digital outputs

In1 and in2 are digital inputs to the 293.
PWM is digital. You can PWM the in pins and/or the enable pin. I have done may motor projects and have never used the enable pin (tied HIGH). In fact if you write the code right you can use one PWM pin to one input for speed and an other non-PWM pin for direction. So you can have 6 motors controlled by PWM, on an Uno, using 12 pins (versus 18 pins to use in1 and in2 with PWM to enable).

Using PWM on the direction pins seems to me quite risky,
how will you avoid having both pins HIGH at the same time?

Why avoid having high on both inputs to the 293? That just puts motor supply voltage to both of the motor inputs, so the voltage across the motor is zero and it is stopped. The motors only drive if the two inputs are different.

I still do not see an advantage in using 2 PWM's per motor
(you want to go speed controlled in both directions, don't you?)
to the standard usage with one PWM per motor.

I am using only one PWM pin and one digital non-PWM pin per motor. The enable is tied high and not connected to a pin. So, two pins per motor, not three. So I have speed control through the PWM pin and direction control with the other pin.

You cant go speed controlled in both direction with your setup
without swapping the pins (or using 2 PWM pins per motor).

But you can do as and what you like,
I only wanted to help.

I see what you were saying about the in pins being digital. I apologize for misunderstanding what you were saying in post 3. Using analogWrite on non-PWM pins (digital) is wrong. He should be using digitalWrite to the in pins and do speed control only with the enables.