lidar lite v3 API cause interrupt function wrong

Hi, I am use nano board to do project which collects range data of Lidar Lite V3 and time period from micro optical sensor (Photointerrupter).
The Lidar API in github: GitHub - garmin/LIDARLite_Arduino_Library: High-performance optical distance sensing.
Here is the code:

#include <Wire.h>
#include "LIDARLite.h"

LIDARLite myLidarLite;

volatile bool encoder_flag = false;
volatile unsigned long Time = 0;
volatile int Duration = 0;
volatile unsigned long LastTime = 0; 

void encoderTick(){
  encoder_flag = true;
}

void setup() {
  pinMode(3, INPUT);//monitor optical sensor read

  attachInterrupt(digitalPinToInterrupt(3), encoderTick, CHANGE);

  Serial.begin(115200);
  myLidarLite.begin(0,true);  
  myLidarLite.configure(0);
}


void loop() {

  if(encoder_flag){
    encoder_flag =false;
    
    /****record this encoder time****/
    Time = millis();
    Duration = Time-LastTime;
    LastTime = Time;
     
    Serial.println(Duration);  
  }

  //int d =myLidarLite.distance();
  
}

Basically, the photo sensor will trig a interrupt as soon as something cover it or leave it. Then the Lidar rotate 360 degree I will get 30 time durations by optical sensor. 28 of them should be almost close, 29th is small and 30th is large to determine the zero position. Because most of covers are the normal size and one is small then normal size and one is larger than normal size. As you can see the code, without
int d =myLidarLite.distance(), the durations lists below:

7
8
7
7
6
8
6
9
6
8
6
8
6
9
6
8
6
8
8
7
7
7
7
7
8
7
7
7
3
11

Then I add int d =myLidarLite.distance(), the durations become ruleless and even not periodic. like this:

8
4
13
9
4
6
8
8
7
9
7
7
10
3
7
12
7
8
9
3
7
10
5
3
7
8
5
9
4
12

Obviously, this Lidar API cause interrupt function process inaccurate so that time duration calculations are wrong. Maybe you guys don't know about Lidar Lite sensor. But I think some of you are expert of arduino controller. Maybe you can give me some suggestions about that. Is that lidar API take too much time so that interrupts process wrong?
Thanks for your help.

The library you are using has some less-than-stellar code in it.

  // Shift high byte and add to low byte
  int distance = (distanceArray[0] << 8) + distanceArray[1];

Shifting a byte by 8 bits will not result in anything useful.

Casting distanceArray[0] to an int is required, before shifting the bits.

The distance() method blocks until a reading has been made (a pulse has returned). Since the time that it takes to get an echo is a function of the distance being measured, this is reasonable.

Your interrupt handler, for the photo-interrupter, need to record when it is triggered. Noting when loop() discovered that the interrupt happened is where you are going wrong.

PaulS:
Casting distanceArray[0] to an int is required, before shifting the bits.

No. The compiler handles that.

byte distanceArray[2] = { 0x11, 0x22 };
void setup() {
  Serial.begin(250000);
  int distance = (distanceArray[0] << 8) + distanceArray[1];;
  Serial.print(F("distance is 0x"));
  Serial.print(distance, HEX);
}
void loop() {}

distance is 0x1122

Hi, thanks for your reply. As you said:Shift high byte and add to low byte. Actually it's done here :LIDARLite.cpp. And I also try record time in the interrupt, same result.

I put time record : Time = millis() in the interrupt, it's same result.
Then i use the API of ShortRangeHighSpeed example and put time record : Time = millis() in the interrupt,it's works fine.
Juts got problem to use API of myLidarLite.distance().