How can I ignore Lidar-lite’s “nack” value and keep my program running?

I’ve started using Lidar-LITE V3hp together with an Arduino UNO to measure some distances. The sensor is hooked up according to sparkfun’s guide. It generally works, but after a few minutes of measurements the sensor returns “nack” instead of a distance. “nack” is also mentioned in the hookup guide and indicates that the I2C connection is lost.

The problem is: I only get one “nack” and then everything freezes forever. The program does not continue with measurements after the first nack. If I re-upload the same script to the Arduino again, or restart it, the measurements work again. But then the same thing happens again after some time.

So my question is: how can I tell the Arduino to ignore the nack value and try taking a new measurement? Instead of freezing everything indefinitely.

I have attached my program here. It controls 2 stepper motors which turn the lidar in two directions, measuring distance at each turn.

LidarProgram.ino (12.4 KB)

You can attach your code to a post if it's too long. Much more likely to get meaningful help if you supply the whole thing.

wildbill:
You can attach your code to a post if it's too long. Much more likely to get meaningful help if you supply the whole thing.

Good idea, thanks. I've updated the post now.

Do you have the decoupling capacitor recommended?

What do you get if you run the test code in the hookup guide?

wildbill:
Do you have the decoupling capacitor recommended?

What do you get if you run the test code in the hookup guide?

Yes, I have the capacitor installed. Using the example code in the guide also produces the nack freeze. But it’s more difficult to replicate there without the motors moving the lidar, I suspect the vibrations are causing the error.

But since they mention the error in the guide it is probably not uncommon to get the nack error, I’m just wondering how to programmatically deal with the error once thrown.