How to use Interrupt to find elapsed time?

Hello. I’m a new beginner in Arduino. I am trying to do a project on “measuring speed using two ultrasonic sensors”. I wan to calculate the time between when the first echo pin is HIGH to the time when second echo pin is HIGH. At first I used the millis() to set counter but I realized that the timer starts as soon as the program runs. So I learnt about using Interrupt and timer. I used two interrupts for two echo pins. But I want the sensors to detect the object less than the particular distance.
If i try to measure distance when echo pin is high,the interrupt has already been enable. How do I solve the problem? I have tried to write a code but I’m sure there are many flaws due to which the serial monitor doesn’t show any result. Please help me solving this problem.

#include<TimerOne.h>
#define trig1 9
#define trig2 10
#define echo1 2
#define echo2 3
volatile long first_time=0;
volatile long second_time=0;
double distance1;
double distance2;
long time1;
long time2;
long elapsed_time;

void setup() {
pinMode(trig1,OUTPUT);
pinMode(trig2,OUTPUT);
pinMode(echo1,INPUT);
pinMode(echo2,INPUT);
Serial.begin(9600);

Timer1.initialize(50);                        
Timer1.attachInterrupt( timerIsr );  

attachInterrupt(digitalPinToInterrupt(2),first_Interrupt,RISING);
attachInterrupt(digitalPinToInterrupt(3),second_Interrupt,RISING);
}

void loop() {
elapsed_time=second_time-first_time;
Serial.print(elapsed_time);  
delay(500);

}

void timerIsr()
{
  trigger();
  getdistance1();
  getdistance2();
  
}
void trigger()
{
  digitalWrite(trig1,LOW);
  digitalWrite(trig1,HIGH);
  digitalWrite(trig1,LOW);
}

double getdistance1()
{
  time1=pulseIn(echo1,HIGH);
  
  distance1=time1*340/20000;
  
  if(distance1<40)
  {
    digitalWrite(echo1,HIGH);
    
  }
}


double getdistance2()
{
  time2=pulseIn(echo2,HIGH);
  distance2=time2*340/20000;
 
 if(distance2<40)
    {
      digitalWrite(echo2,HIGH);
    }
  }
}


void first_Interrupt()
{
  digitalRead(echo1);
  first_time=micros();
}


void second_Interrupt()
{
  digitalRead(echo2);
  second_time=micros();
  
}

thank you!

The fact that millis runs from the beginning doesn't stop you. Just record the value at the beginning and end and subtract to get the time in between.

How do I limit the distance while using Interrupt?

If you get a number you don't like then don't do anything with it. Keep waiting until you get one you do like.

Will it work if the interrupt is enabled?

  digitalRead(echo1);
  digitalRead(echo2);

Why ?

Ersa20: Will it work if the interrupt is enabled?

It's not going to work if it's disabled, so I would suggest leaving interrupts enabled. 8)