maze solver with 3 ultrasonic help in code

Hey,
I have to code maze solver robot using 3 ultrasonic…

I want to code with “random maze” that can solve any maze
with simple code … if the robot detects wall in front or right or left it turns left,right,…
can anyone help me with it, please?

I use a motor drive, 3 ultrasonic sensors,

MY CODE

#include <Ultrasonic.h>
Ultrasonic ultrasonic1(12,13);//front sensor
Ultrasonic ultrasonic2(2,4);//left sensor
Ultrasonic ultrasonic3(7,8);//right sensor

const int IN1=3;
const int IN2=5;
const int IN3=6;
const int IN4=9;
 int  SPEED_Control=100;
void setup() 
{
  // fIRST , define the Motor's pin as an OUTPUT
  
pinMode( IN1 ,OUTPUT);// Right Motor forward
pinMode( IN2 ,OUTPUT);// Right Motor back
pinMode( IN3 ,OUTPUT);// left Motor back
pinMode( IN4 ,OUTPUT);// left Motor forward
Serial.begin(9600);
}
////////////////////////////////////////////////////////////////

void BACKWARD(int Speed)
{
 
  analogWrite(IN1,0);
  analogWrite(IN2,Speed);
  analogWrite(IN3,Speed);
  analogWrite(IN4,0);
}
////////////////////////////////////////////////////////////////

void FORWARD(int Speed){

  analogWrite(IN1,Speed);
  analogWrite(IN2,0);
  analogWrite(IN3,0);
  analogWrite(IN4,Speed);
}
////////////////////////////////////////////////////////////////

void LEFT(int Speed){
 
  analogWrite(IN1,Speed);
  analogWrite(IN2,0);
  analogWrite(IN3,0);
  analogWrite(IN4,0);
}
////////////////////////////////////////////////////////////////

void RIGHT(int Speed){
 
  analogWrite(IN1,0);
  analogWrite(IN2,0);
  analogWrite(IN3,0);
  analogWrite(IN4,Speed);
}
////////////////////////////////////////////////////////////////

void Stop(){
  
  analogWrite(IN1,0);
  analogWrite(IN2,0);
  analogWrite(IN3,0);
  analogWrite(IN4,0);
}
////////////////////////////////////////////////////////////////

void loop() 
{
   FORWARD(SPEED_Control);
 byte  distance1 = ultrasonic1.Ranging(CM) ;// waits 15ms for the servo to reach the position
 byte  distance2 = ultrasonic2.Ranging(CM) ;
 byte  distance3 = ultrasonic3.Ranging(CM) ;
  
   if (distance1 <= 20 && distance2 <= 20 && distance3 > 20){
      LEFT(SPEED_Control);
      delay(500);
      }
   else if (distance1 <= 20 && distance3 <= 20 && distance2 >20){
      RIGHT(SPEED_Control);
      delay(500);
      } 
   else if (distance2 <= 20 && distance3 <= 20 && distance1 >20){
      FORWARD(SPEED_Control);
      delay(500);
      } 
  
 
}

What does the code do that you don't want it to? What doesn't the code do that you do want it to?

What stops a late return from an earlier ping being interpreted as a nearer return?

Why don't the comments describe the code?

...and posting the same question in different parts of the forum is a shortcut to a forum timeout, so I deleted your duplicate.

AWOL:
What does the code do that you don’t want it to?
What doesn’t the code do that you do want it to?

What stops a late return from an earlier ping being interpreted as a nearer return?

Why don’t the comments describe the code?

first I’m still a beginner in programming arduino.
The robot just rotates, I don’t know if it’s from my code or what
I want the robot solves the random maze, avoid the walls when the front and right is true , robot turns left …
and the other cases
using “distance” with ultrasonic sensors is correct?

AWOL: ...and posting the same question in different parts of the forum is a shortcut to a forum timeout, so I deleted your duplicate.

sorry for that, I didn't know the correct section to post so I posted it in 2 different sections..

The simplest way to debug something like this is to get your code to tell you what it is seeing and therefore doing.

AWOL:
The simplest way to debug something like this is to get your code to tell you what it is seeing and therefore doing.

the code used is what I mentioned in the post
can you help me in the loop function

void loop() 
{
   FORWARD(SPEED_Control);
 byte  distance1 = ultrasonic1.Ranging(CM) ;// FRONT ultrasonic
 byte  distance2 = ultrasonic2.Ranging(CM) ;// LEFT ultrasonic
 byte  distance3 = ultrasonic3.Ranging(CM) ;// RIGHT ultrasonic
  
   if (distance1 <= 20 && distance2 <= 20 && distance3 > 20){
      RIGHT(SPEED_Control);
      delay(500);
      }
   else if (distance1 <= 20 && distance3 <= 20 && distance2 >20){
      LEFT(SPEED_Control);
      delay(500);
      } 
   else if (distance2 <= 20 && distance3 <= 20 && distance1 >20){
      FORWARD(SPEED_Control);
      delay(500);
      } 
  
 
}

I can't help you if I don't know what the problem is. Adding some debug prints will help you find out what the code is seeing and doing.

 byte  distanceF = ultrasonicF.Ranging(CM) ;
 byte  distanceL = ultrasonicL.Ranging(CM) ;
 byte  distanceR = ultrasonicR.Ranging(CM) ;

6 letters different, but no useless comments needed.

if (distance1 <= 20 && distance2 <= 20 && distance3 > 20){
RIGHT(SPEED_Control);
delay(500);
}
If there is something in front of the 3rd sensor (quick, which side was that?), blindly turn right for 1/2 a second. Do you walk that way? Are you covered in bruises all the time?

By the way, by convention, all capital letter names are reserved for constants. Functions are NOT constants.

PaulS:

 byte  distanceF = ultrasonicF.Ranging(CM) ;

byte  distanceL = ultrasonicL.Ranging(CM) ;
byte  distanceR = ultrasonicR.Ranging(CM) ;



6 letters different, but no useless comments needed.

if (distance1 <= 20 && distance2 <= 20 && distance3 > 20){
RIGHT(SPEED_Control);
delay(500);
}
If there is something in front of the 3rd sensor (quick, which side was that?), blindly turn right for 1/2 a second. Do you walk that way? Are you covered in bruises all the time?

By the way, by convention, all capital letter names are reserved for constants. Functions are NOT constants.

thanks for your reply… Ok I think that I understand what you mean, sir…
the 3rd sensor for the right
the distance if larger than 20 cm so nothing in front of the right sensor that what I mean … Is it correct?