You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've had issues with this library where the I2C failed to communicate and the programme hung. I have forked the library and this is a version of readRange that handles timeouts gracefully. I would imagine you'd want a better approach than just returning zero but it does work.
uint8_t Adafruit_VL6180X::readRange(void) {
unsigned long time = millis();
// wait for device to be ready for range measurement
while (!(read8(VL6180X_REG_RESULT_RANGE_STATUS) & 0x01) ) {
if ( millis() - time > 5 ) { return 0; }
}
// Start a range measurement
write8(VL6180X_REG_SYSRANGE_START, 0x01);
// Poll until bit 2 is set
while (!(read8(VL6180X_REG_RESULT_INTERRUPT_STATUS_GPIO) & 0x04)) {
if ( millis() - time > 5 ) { return 0; }
}
// read range in mm
uint8_t range = read8(VL6180X_REG_RESULT_RANGE_VAL);
I have had a similar experience, but curiously, even after introducing similar timeouts, I find that the Arduino will often hang when communicating with the VL6180X. If I comment out the VL6180X calls it will run for ever. On inspecting the code I was sure the issue was the while loops never terminating so I introduced a timeout similar to yours on all calls that use that construction; however, the device still hung communicating with the VL6180X device.
I've had issues with this library where the I2C failed to communicate and the programme hung. I have forked the library and this is a version of readRange that handles timeouts gracefully. I would imagine you'd want a better approach than just returning zero but it does work.
uint8_t Adafruit_VL6180X::readRange(void) {
unsigned long time = millis();
// wait for device to be ready for range measurement
while (!(read8(VL6180X_REG_RESULT_RANGE_STATUS) & 0x01) ) {
if ( millis() - time > 5 ) { return 0; }
}
// Start a range measurement
write8(VL6180X_REG_SYSRANGE_START, 0x01);
// Poll until bit 2 is set
while (!(read8(VL6180X_REG_RESULT_INTERRUPT_STATUS_GPIO) & 0x04)) {
if ( millis() - time > 5 ) { return 0; }
}
// read range in mm
uint8_t range = read8(VL6180X_REG_RESULT_RANGE_VAL);
// clear interrupt
write8(VL6180X_REG_SYSTEM_INTERRUPT_CLEAR, 0x07);
return range;
}
The text was updated successfully, but these errors were encountered: