VL53L1X Blocking Encoder Interrupts for PID

I've been working with Brett Beauregard's PID library to control the speed of two motors by measuring the pulses from attached encoders. The pulses are counted with an interrupt every time a rising edge is detected. I pretty much followed the code/explanation here.

After some tinkering, the PID/motor control part of things worked. But when I tried to add it to my main loop, which includes a function for reading three VL53L1X tof sensors, everything went kaput. I eventually figured out that the VL53L1X library is set to block the loop while it waits for a new sensor value by default. So my theory is that the loop was getting stuck at the sensor reading function, causing the PID function to shoot off into infinity because it wasn't getting any interrupts (or something like that).

After some failed attempts at moving things around and modifying PID parameters, I discovered that there is a way to make the VL53L1X non-blocking. In this scenario, the function polls the sensor to see if new data is ready and, if not, just moves on instead of sitting there and waiting for the new reading. More on this here.

So now with the non-blocking code everything seems to be up and running again. I am able to get data from the VL53L1X sensors over I2C while still running the PID functions in the main loop. Fingers crossed I can go a couple days before breaking something else!

Original blocking VL53L1X read function:

int tof_read(class VL53L1X tof_sensor, int &tof_reading)
  tof_reading = tof_sensor.read();

Non-blocking VL53L1X read function:

void tof_read(class VL53L1X tof_sensor, int &tof_reading)
  if (tof_sensor.dataReady()) //If a reading is ready then take it
        tof_reading = tof_sensor.read(false); 

Is there a question? If not, there is a "Showcase" forum for that.

Ah, I suppose not. Just wanted to post this in case others are having a similar issue. Is there a way for me to move this over to "Showcase"?

Click on the report to moderator underneath your post and request the move.