Calling the VL53L0X library from an ISR

Hi, i using you example in function() when i call function() from mainLoop everything Works well. Now i want call this function() not from main loop but from timer1.attachInterrupt(funtion).
Problem is at first call of interupt measure 0 and don’t repeat again no more. Do you know Why?

Please answere :slight_smile:

Hello, Saimonko7.

Can you post the code that is not working?

-Nathan

Lidar.ino.ino (1.5 KB)

When you put timer out and try to call function-“Lidar()” from main loop it is working well !!! :slight_smile:

But when code look like this (uploaded) it didn´t work.
I want call function Lidar() at Timer1.attachInterrupt(Lidar); when i can set period of timer.

Thank you for quick answer Nathan :slight_smile:

Hello.

What Arduino board are you using?

Also, I moved your posts to a new thread since they do not appear to have anything to do with the original poster’s question.

-Nathan

I using arduino nano. But not original piece. Cheaper copy.
I never have problem with this board.

The Servo library also uses Timer1, so there could be a conflict there. You can find some example code to change the timer the servo library uses in the “Controlling a servo with an Arduino Uno” section of our Zumo User’s guide.

-Nathan

When you put out everything about Servo, it still does´t work. Try this (LidarNoServo.ino.ino (1.8 KB)
) code. I only commented lines which controle or initialize servo, from code uploaded two day ago.

Can you upload code, where you using sensor.startContinuous(100); and measuring is in separate function? But this function is called from Timer1.attachInterrupt(Function).
Like this (dysfunctional) code VL53L0X_call_with_timer.ino (667 Bytes)

Hi, Saimonko7.

Nathan and I reproduced your problem and looked into it more, and we found that you are having exactly the same issue as described in this StackExchange question. To summarize, the Wire library relies on interrupts to work, but the microcontroller disables interrupts before running an interrupt handler (and your Lidar() function is called from the Timer1 interrupt handler).

Both of the answers in the link above should work for you: you can either call sei() (or interrupts()) at the beginning of Lidar() to enable nested interrupts, or you avoid using a timer interrupt so that nested interrupts aren’t necessary.

Kevin