robot race

hello
i’m joining to a robot race competition which the robot go on the maze . i’m using simple two motors , motor drive shield L298 , arduino uno and two ultrasonic sensors HC-SR04
i have wrote this code but i can’t turn right and left . can you help me to solve the maze

const int IN1=3;
const int IN2=5;
const int IN3=6;
const int IN4=9;

const int trigPin1 = 13;
const int echoPin1 = 12;
const int trigPin2 = 2;
const int echoPin2 = 4;

void setup() {
Serial.begin(115200);
pinMode( IN1 ,OUTPUT);
pinMode( IN2 ,OUTPUT);
pinMode( IN3 ,OUTPUT);
pinMode( IN4 ,OUTPUT);

}
void go(){
analogWrite(IN1,60);
analogWrite(IN2,0);
analogWrite(IN3,65);
analogWrite(IN4,0);
}
void ClockWise(){
analogWrite(IN1,0);
analogWrite(IN2,100);
analogWrite(IN3,100);
analogWrite(IN4,0);
}
void counterClockWise(){
analogWrite(IN1,100);
analogWrite(IN2,0);
analogWrite(IN3,0);
analogWrite(IN4,100);
}
void Stop(){
analogWrite(IN1,0);
analogWrite(IN2,0);
analogWrite(IN3,0);
analogWrite(IN4,0);
}
void back (){
analogWrite(IN1,0);
analogWrite(IN2,100);
analogWrite(IN3,0);
analogWrite(IN4,100);
}

void loop() {
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration1 , duration2 , cm1 , cm2;

// The sensor is triggered by a HIGH pulse of 10 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(trigPin1, OUTPUT);
digitalWrite(trigPin1, LOW);
delayMicroseconds(2);
digitalWrite(trigPin1, HIGH);
delayMicroseconds(5);
digitalWrite(trigPin1, LOW);

// Read the signal from the sensor: a HIGH pulse whose
// duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(echoPin1, INPUT);
duration1 = pulseIn(echoPin1, HIGH);

if (microsecondsToCentimeters(duration1)<=20&&microsecondsToCentimeters(duration1)>0){
go();
}

else if(microsecondsToCentimeters(duration1)>20 ){
ClockWise();
}
else if (microsecondsToCentimeters(duration1)>40){
Stop();
}

// convert the time into a distance
cm1 = microsecondsToCentimeters(duration1);

Serial.print(cm1);
Serial.print(“cm”);
Serial.println();

delay(100);

pinMode(trigPin2, OUTPUT);
digitalWrite(trigPin2, LOW);
delayMicroseconds(2);
digitalWrite(trigPin2, HIGH);
delayMicroseconds(5);
digitalWrite(trigPin2, LOW);

// Read the signal from the sensor: a HIGH pulse whose
// duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(echoPin2, INPUT);
duration2 = pulseIn(echoPin2, HIGH);

if (microsecondsToCentimeters(duration2)<=20&&microsecondsToCentimeters(duration2)>0){
go();
}
else if (microsecondsToCentimeters(duration2)>20 && microsecondsToCentimeters(duration1)<=20){
counterClockWise();
}

else if (microsecondsToCentimeters(duration2)>40){
Stop();
}

// convert the time into a distance
cm2 = microsecondsToCentimeters(duration2);

Serial.print(cm2);
Serial.print(“cm”);
Serial.println();

delay(100);

}

long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.

return microseconds / 29 / 2;
}

Do the motors work correctly without the sensors? If you use a test sketch and just run say your clockwise or counterclockwise functions, do the motors do what you expect? Does the robot actually turn?

Do the ultrasonic sensors work correctly?- are they giving you the values you expect? There have been threads discussing the use of two of those things due to concerns of them interfering with each other.

Btw there are libraries for those: makes life much easier.

Your code would be a lot simpler if you used the cm values in your if statements: you only use the cm values at the end for printing, and the code is cluttered with “microsecondsToCentimeters(duration2)” and so in, in the ifs.

Lastly it would be a lot easier to read if you mod the post, select the code, hit the </> button so that

your code
{
lookslike
}
this

The delay() calls are contributing nothing to the program. Get rid of them.

Read both sensors and THEN decide what to do. Going forward because one sensor is unblocked may not make sense, depending on how the sensors are positioned (which you didn't mention).

i have modified my program but i have a problem with turning left or right
i have found that when the robot has finished the straight pass and begin to turn right or left , the robot crush to the maze and didn’t complete the turning
when i used delay function , the robot sometimes turn left or right correctly but not all times
i want a solution to complete the turning correctly in all times
this is my new code

const int IN1=3;
const int IN2=5;
const int IN3=6;
const int IN4=9;

const int trigPin1 = 13;
const int echoPin1 = 12;
const int trigPin2 = 2;
const int echoPin2 = 4;

void setup() {
Serial.begin(115200);
pinMode( IN1 ,OUTPUT);
pinMode( IN2 ,OUTPUT);
pinMode( IN3 ,OUTPUT);
pinMode( IN4 ,OUTPUT);

}
void go(){
analogWrite(IN1,60);
analogWrite(IN2,0);
analogWrite(IN3,65);
analogWrite(IN4,0);
}
void ClockWise(){
analogWrite(IN1,0);
analogWrite(IN2,0);
analogWrite(IN3,50);
analogWrite(IN4,0);
}
void counterClockWise(){
analogWrite(IN1,50);
analogWrite(IN2,0);
analogWrite(IN3,0);
analogWrite(IN4,0);
}
void Stop(){
analogWrite(IN1,0);
analogWrite(IN2,0);
analogWrite(IN3,0);
analogWrite(IN4,0);
}
void back (){
analogWrite(IN1,0);
analogWrite(IN2,100);
analogWrite(IN3,0);
analogWrite(IN4,100);
}

void loop() {
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration1 , duration2 , cm1 , cm2;

// The sensor is triggered by a HIGH pulse of 10 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(trigPin1, OUTPUT);
digitalWrite(trigPin1, LOW);
delayMicroseconds(2);
digitalWrite(trigPin1, HIGH);
delayMicroseconds(5);
digitalWrite(trigPin1, LOW);

// Read the signal from the sensor: a HIGH pulse whose
// duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(echoPin1, INPUT);
duration1 = pulseIn(echoPin1, HIGH);

pinMode(trigPin2, OUTPUT);
digitalWrite(trigPin2, LOW);
delayMicroseconds(2);
digitalWrite(trigPin2, HIGH);
delayMicroseconds(5);
digitalWrite(trigPin2, LOW);

// Read the signal from the sensor: a HIGH pulse whose
// duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(echoPin2, INPUT);
duration2 = pulseIn(echoPin2, HIGH);

if (microsecondsToCentimeters(duration1)<=20&&microsecondsToCentimeters(duration2)<=20){
go();
}

else if (microsecondsToCentimeters(duration2)>20){
go();
delay(30);
counterClockWise();
delay(400);

}else if (microsecondsToCentimeters(duration1)>20){
go();
delay(30);
ClockWise();
delay(400);
}

// convert the time into a distance
cm1 = microsecondsToCentimeters(duration1);
Serial.print(cm1);
Serial.print(“cm”);
Serial.println();

delay(100);

// convert the time into a distance
cm2 = microsecondsToCentimeters(duration2);

Serial.print(cm2);
Serial.print(“cm”);
Serial.println();

delay(100);

}

long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.

return microseconds / 29 / 2;
}

@zizo_said250, did you not see JimboZA's suggestion to use code tags? You can't post your code inline. It's not too late to edit your post(s) and correct this oversight.

Also, you should correctly format it before posting, to make it more readable. (You can use "Auto-format" in the IDE.) Removing the unnecessary blank lines would help too. Make it easy for people to help you, not hard.

i want a solution to complete the turning correctly in all times

Without some way for the robot to know how much it has actually turned, you haven't a hope in hell of making this happen reliably.