About PulseIn or a better idea for 4 HC-SR04

Hi! Can you please help me with my code? I use an Arduino UNO,4 HC-SR04 ultrasonic sensors in my Sumobot for detect the opponent and 2 CNY-70 infra-red sensors to prevent the robot from falling off the Tatami (Ring).
I made a program which the robot detects the line and returns, it was easy. But when I added the ultrasonic sensors, actions delayed and the robot fell. I think it was the PulseIn function because the Timeout for default is 1 second, and if the sensors don’t detect anything the program must wait a second for the next line of program.

It’s a copy of the code:

 int IRD=2, IRI=3,pul=4, EAI=5, TAI=6, EAD=7, TAD=8, EI=9, TI=10, ED=11, TD=12, led=13, MAI=A1, MAD=A2, MRI=A3, MRD=A4;
long distancia1,distancia2,distancia3,distancia4;
long tiempo1,tiempo2,tiempo3,tiempo4;
unsigned long t;
int enc;

void setup() {
Serial.begin(9600);
pinMode(2,INPUT);   //Infrared Right
pinMode(3,INPUT);   //Infrared Left
pinMode(4,INPUT);   //pulsador
pinMode(5,INPUT);   //Echo front-left
pinMode(6,OUTPUT);  //Trigger front left
pinMode(7,INPUT);   //Echo front right
pinMode(8,OUTPUT);  //Trigger front right
pinMode(9,INPUT);   //Echo left
pinMode(10,OUTPUT); //Trigger left
pinMode(11,INPUT);  //Echo right
pinMode(12,OUTPUT); //Trigger right
pinMode(13,OUTPUT); //Led ON


enc=0;
distancia1=0;
distancia2=0;
distancia3=0;
distancia4=0;
tiempo1=0;
tiempo2=0;
tiempo3=0;
tiempo4=0;
t=0;
}

void loop() {
int lbd = digitalRead(IRD);
int lbi = digitalRead(IRI);
int d1, d2, d3, d4; 
int c=0;
//Button On
Serial.println (digitalRead(pul));

if ( digitalRead(pul)==0 && enc==0 )
   { digitalWrite(led,HIGH);
    delay(5000);
    enc=1;
   }
if (digitalRead(pul)==0 && enc==1)
  {digitalWrite(led,LOW);  
  delay(500);
  enc=0;
  }

//Program ON:
if (enc==1)  
   { avanzar();
     
     
    lbd = digitalRead(IRD);
    lbi = digitalRead(IRI);  
    
     d1=ojoizquierdo();
     
     d2=ojoderecho();
     
     d3=ojofrenteder();
     
     d4=ojofrenteizq();
     
     
  
  if (lbi==1 )                       //If detect white line, return and turn right
    { Serial.println( "blanca");
      retroceder();
      delay (700);
      giroder();
      delay (230);
    }
    
   if (lbd==1 )                       //If detect white line, return and turn left 
    { Serial.println( "blanca");
      retroceder();
      delay (700);
      giroizq();
      delay (230);
     }
       
   if (d1<40 && d1!=0 && lbi==0 && lbd==0 )   
        { giroizq();
          c=0;          
          delay(100);
          detener();
          delay(10);
        
          while((d4>40 || d3>40) && c<15 && lbd==0 && lbi==0 )
          {lbd = digitalRead(IRD);
           lbi = digitalRead(IRI);  
            giroizq();
          delay(70);
          detener();
          delay(10);
          d3= ojofrenteder();
          delay(5);
          d4=ojofrenteizq();
          delay(5);
          c++;
          }
        }
    
    if (d2<40 && d2!=0 && lbi==0 && lbd==0 )   
        { giroder(); 
          c=0;          
          delay(70);
          detener();
          delay(10);
                 
          while((d4>40 || d3>40) && c<15 && lbi==0 && lbd==0 )
          {lbd = digitalRead(IRD);
           lbi = digitalRead(IRI);  
            giroder();
           delay(100);          
           detener();
           delay(10);
           d3= ojofrenteder();
           delay(5);
           d4= ojofrenteizq();
           delay(5);
           c++;
          }
        }
    
    if ((d3<40 || d4<40) && lbd==0 && lbi==0)                                
       {avanzar();
        delay(100); 
       }
     }
      


if (enc==0)                                 //si se apaga, se detiene
 detener(); 
}

void avanzar () {                           //Forward
 analogWrite(A3,0);
 analogWrite(A4,0);
 delay(5);
 analogWrite(A1,255);
 analogWrite(A2,255);
 
 Serial.println( "avanzando");
}


void giroizq () {                                //Turn Left
  analogWrite(A1,0);
  analogWrite(A4,0);
  delay(5); 
  analogWrite(A3,255);
  analogWrite(A2,255);
  Serial.println( "girando a la izquierda");
}

void giroder () {                                      //Turn Right
  analogWrite(A3,0);
  analogWrite(A2,0);
  delay(5);
  analogWrite(A1,255);
  analogWrite(A4,255);
  Serial.println( "girando a la derecha");
}

void retroceder () {                                            //Return
 analogWrite(A1,0);
 analogWrite(A2,0);
 delay(5);
 analogWrite(A3,255);
 analogWrite(A4,255);  
 Serial.println( "retrocediendo");
 }
 
int ojoderecho() {                                            //Right Ultrasonic
 digitalWrite(TD,LOW);
 delayMicroseconds(2);
 digitalWrite(TD,HIGH);
 delayMicroseconds(10);
 digitalWrite(TD, LOW);
 tiempo2=pulseIn(ED,HIGH);
 distancia2=int(0.017*tiempo2);
 Serial.println("Distancia derecho  ");
 Serial.print(distancia2);
 Serial.println(" cm");
 
 return distancia2;
 }
 
 int ojoizquierdo() {                                         //Left Ultrasonic
 digitalWrite(TI,LOW);
 delayMicroseconds(2);
 digitalWrite(TI,HIGH);
 delayMicroseconds(10);
 digitalWrite(TI, LOW);
 tiempo1=pulseIn(EI,HIGH);
 distancia1=int(0.017*tiempo1);
 Serial.println("Distancia izquierdo  ");
 Serial.print(distancia1);
 Serial.println(" cm");
 
 return distancia1 ;
 }

int ojofrenteder() {
 digitalWrite(TAD,LOW);                                            //Front-Right Ultrasonic
 delayMicroseconds(2);
 digitalWrite(TAD,HIGH);
 delayMicroseconds(10);
 digitalWrite(TAD, LOW);
 tiempo3=pulseIn(EAD,HIGH);
 distancia3=int(0.017*tiempo3);
 Serial.println("Distancia adelante  ");
 Serial.print(distancia3);
 Serial.println(" cm");
return distancia3 ;
 }
                        
int ojofrenteizq() {                                                       //Front-Left Ultrasonic
 digitalWrite(TAI,LOW);
 delayMicroseconds(2);
 digitalWrite(TAI,HIGH);
 delayMicroseconds(10);
 digitalWrite(TAI, LOW);
 tiempo4=pulseIn(EAI,HIGH);
 distancia4=int(0.017*tiempo4);
 Serial.println("Distancia adelante  ");
 Serial.print(distancia4);
 Serial.println(" cm");
return distancia4 ;
 }

void detener ()                                                             //Stop
{analogWrite(A1,0);
  analogWrite(A2,0);
  delay(5);
  analogWrite(A3,0);
  analogWrite(A4,0);
}

Do you know if I can unit all the Trigger pins in only one? Because I don’t need know the distance, I only need know if something is there. So I could change PulseIn’s Timeout and have a HIGH or LOW value to decide to what I want to do.

Yes, it's probably best to change the timeout. The trouble is that "There's something there" still takes a defined amount of time - you want to know if there's something in range and you may even decide to change that range in the future.

Could you tell me how to change the Timeout?

"you want to know if there's something in range and you may even decide to change that range in the future." I want to that!

Tisak97: Could you tell me how to change the Timeout?

Read the manual. I call your attention to the "syntax" section, which shows there's two ways to call pulseIn().

There is also the NewPing library that does not rely on pulseIn...

MorganS:
Read the manual. I call your attention to the “syntax” section, which shows there’s two ways to call pulseIn().

Thank You. I have calculated the range with the next algorithm:

d= 0.017*t

d=distance in cm
t= time in microseconds

So, 50cm = 2.94ms

4 sensors = 11.76ms

PaulS:
There is also the NewPing library that does not rely on pulseIn…

Thank you to, I’ll read about it