help with my maze car

hello:), guys, I have a competition next Thursday
and I need help with my maze car

I need speed and accurate moves

my first question which better 3 ultrasonic sensors or one ultrasonic sensor on a servo motor

my second question for 3 ultrasonic sensors this code isn’t accurate can anyone help me edit it

int EnA=5;
int M1=6;
int M2=7;
int M3=8;
int M4=9;
int EnB=10;
int X;
int Y;
int data;
float Distance;

#define TRIGGER_PINF A4 // Arduino pin tied to trigger pin on ping sensor.
#define ECHO_PINF A1 // Arduino pin tied to echo pin on ping sensor.
float DistanceF;
float DurationF;

#define TRIGGER_PINL A3 // Arduino pin tied to trigger pin on ping sensor.
#define ECHO_PINL A0 // Arduino pin tied to echo pin on ping sensor.
float DistanceL;
float DurationL;

#define TRIGGER_PINR A5 // Arduino pin tied to trigger pin on ping sensor.
#define ECHO_PINR A2 // Arduino pin tied to echo pin on ping sensor.
float DistanceR;
float DurationR;

void forward(){
Serial.println(“backward”);
digitalWrite(M1,0);
digitalWrite(M2,1);
digitalWrite(M3,0);
digitalWrite(M4,1);
analogWrite(EnA,170);
analogWrite(EnB,170);

}

void backward(){
Serial.println(“forward”);
digitalWrite(M1,1);
digitalWrite(M2,0);
digitalWrite(M3,1);
digitalWrite(M4,0);
analogWrite(EnA,X);
analogWrite(EnB,Y);
}

void right(){
Serial.println(“right”);
digitalWrite(M1,1);
digitalWrite(M2,0);
digitalWrite(M3,0);
digitalWrite(M4,1);
analogWrite(EnA,255);
analogWrite(EnB,255);
}

void left(){
Serial.println(“left”);
digitalWrite(M1,0);
digitalWrite(M2,1);
digitalWrite(M3,1);
digitalWrite(M4,0);
analogWrite(EnA,255);
analogWrite(EnB,255);
}

void Stop(){
Serial.println(“Stop”);
digitalWrite(M1,0);
digitalWrite(M2,0);
digitalWrite(M3,0);
digitalWrite(M4,0);
analogWrite(EnA,0);
analogWrite(EnB,0);
}

void setup() {
Serial.begin(9600);
pinMode(A0,INPUT);
pinMode(A1,INPUT);
pinMode(A2,INPUT);
pinMode(A3,OUTPUT);
pinMode(A4,OUTPUT);
pinMode(A5,OUTPUT);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);
pinMode(8,OUTPUT);
pinMode(9,OUTPUT);
pinMode(10,OUTPUT);
}

void loop() {
DistanceF=get_DistanceF();
DistanceL=get_DistanceL();
DistanceR=get_DistanceR();
Serial.print(DistanceF);
Serial.println(“DistanceF=”);
Serial.print(DistanceL);
Serial.println(“DistanceL=”);
Serial.print(DistanceR);
Serial.println(“DistanceR=”);

if (DistanceF<=15) {
if (DistanceR>DistanceL){
right();
}
else if (DistanceR<DistanceL){
left();
}}
else if (DistanceL <= 15){

if (DistanceF<DistanceR){
right();
}
else if(DistanceF>DistanceR){
forward();
}
}
else if (DistanceR <= 15){
if (DistanceL>DistanceF){
left();
}

else if (DistanceL<DistanceF){
forward();
}
}

}

float get_DistanceF (){
float DistanceF ,DurationF;
digitalWrite(TRIGGER_PINF,LOW);
delayMicroseconds(2);
digitalWrite(TRIGGER_PINF,HIGH);
delayMicroseconds(8);
digitalWrite(TRIGGER_PINF,LOW);
DurationF=pulseIn(ECHO_PINF,HIGH);
DistanceF=DurationF0.0340/2;
return DistanceF;
}
float get_DistanceL (){
float DistanceL ,DurationL;
digitalWrite(TRIGGER_PINL,LOW);
delayMicroseconds(2);
digitalWrite(TRIGGER_PINL,HIGH);
delayMicroseconds(8);
digitalWrite(TRIGGER_PINL,LOW);
DurationL=pulseIn(ECHO_PINL,HIGH);
DistanceL=DurationL
0.0340/2;
return DistanceL;
}
float get_DistanceR (){
float DistanceR ,DurationR;
digitalWrite(TRIGGER_PINR,LOW);
delayMicroseconds(2);
digitalWrite(TRIGGER_PINR,HIGH);
delayMicroseconds(8);
digitalWrite(TRIGGER_PINR,LOW);
DurationR=pulseIn(ECHO_PINR,HIGH);
DistanceR=DurationR*0.0340/2;
return DistanceR;
}

this the first maze

the second

here i must hit 5 or more cones

the last maze

all lengths in mm

thanks a lot

Your aggregate read rate for all three sensors shouldn't exceed about 20 to 30 Hz, otherwise you run the risk of misinterpreting late, distant returns as closer ones.

I'd say your code is too long - you won't get any style marks.

Please remember to use code tags when posting code.

AWOL:
Your aggregate read rate for all three sensors shouldn't exceed about 20 to 30 Hz, otherwise you run the risk of misinterpreting late, distant returns as closer ones.

I'd say your code is too long - you won't get any style marks.

Please remember to use code tags when posting code.

can you explain more to me I'm a beginner

As soon as you get a response from the front sensor, you're requesting a reading from the left.
Imagine that after you send the ping from the left sensor, a late echo from the front sensor's last ping arrives at the left sensor.
How does that affect your results?

so should i make delay((1/30)*1000)

and i will make the code shorter :stuck_out_tongue_closed_eyes: but when it work correctly :slightly_frowning_face: