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