Ultrasonic Sensor code "pulsein()" causing delay

Hello,

I have a code that i wrote for a line following robot. I decided to add a Ultrasonic Sensor HC-SR04 to stop the motor when an object is within a certain range. I have successfully done that however the pulsein() within the code has slowed everything down. This is causing my line following robot to not follow the line since its slowing everything down. Can i possibly use something different rather than pulsein(). Any suggestions?

float duration, distance;
  digitalWrite(trigPin, LOW); 
  delayMicroseconds(2);
 
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  
  duration = pulseIn(echoPin, HIGH);
  distance = (duration / 2) * 0.0344;



 if (distance <=50){

  md.setM1Speed(0);
      md.setM2Speed(0);
      md.setSpeeds(0,0); //stop
      Motor[1].Speed = 0;
    Motor[2].Speed = 0;
      Line.RegulaceHodnota = 0;
      Line.RegulaceKrok = 0;
      GP = 0;
      GI = 0;
      GD = 0;
      Line.Zmena = 0;
      Line.ZmenaN = 0;

however the pulsein() within the code has slowed everything down.

Well, duh.

Can i possibly use something different rather than pulsein().

The NewPing library is non-blocking.

ImANoobAtThis:
Can i possibly use something different rather than pulsein(). Any suggestions?

You can use interrupts to measure pulse durations. Use a CHANGE interrupt and use micros() to note the time that a pulse started (pin went HIGH) and ends (pin went LOW). Subtract one time from the other to calculate pulse length.