Adafruit VL53L0X library destroys millis();

I'm using the following code on a Mega1284p (mighty core):

Adafruit_VL53L0X laser = Adafruit_VL53L0X();
VL53L0X_RangingMeasurementData_t laserMeasure;
bool laserCheckActive = false;
laserLimit =55;

void setup(){
Serial.begin(9600);
Wire.begin();
laser.begin(VL53L0X_I2C_ADDR,1);
Serial.print("Current Milli");
Serial.println(millis());
laserCheckActive = true;
}


void loop(){


Serial.print(laserMeasure.RangeMilliMeter);
Serial.print("   ");
Serial.println(millis());
if(laserMeasure.RangeMilliMeter <= laserLimit){
laserCheckActive = false;
}

checkLaser(laserCheckActive);

}

void checkLaser(bool active){
 if(active==true){

 laser.getSingleRangingMeasurement(&laserMeasure,1);
 }
}

This outputs the following on Serial:

VL53L0X Info: 
Device Name: VL53L0X ES1 or later, Type: VL53L0X, ID: VL53L0CBV0DH/1$1
Rev Major: 1, Minor: 1
VL53L0X: StaticInit
VL53L0X: PerformRefSpadManagement 
refSpadCount = 6, isApertureSpads = 0
VL53L0X: PerformRefCalibration
VL53L0X: SetDeviceMode

CurrentMilli 1284

sVL53L0X: PerformSingleRangingMeasurement 
Range Status: 0 : Range Valid 
RANGE IGNORE THRESHOLD: 0.79
Measured distance: 20
20 65595

sVL53L0X: PerformSingleRangingMeasurement
Range Status: 0 : Range Valid
RANGE IGNORE THRESHOLD: 0.99 
Measured distance: 20 
20   65597

sVL53L0X: PerformSingleRangingMeasurement
Range Status: 0 : Range Valid
RANGE IGNORE THRESHOLD: 0.80
Measured distance: 21 
21    34707016

As you can see, the millis() jump from 1284 after Setup right to 65595 after the first measurement, then to 65597 (plausible 2ms) and then right to 34707016!!

If i don't set laserCheckActive in setup, the millis() work fine.

Why is this happening? How can a libary inflict with millis()?

You created your own main(), so the IDE didn't add one. The one that the IDE would have added calls init(), to set up the hardware, including the timers (that millis() relies on). You don't call init(), so the hardware and timer state is completely unknown.

No sorry, this was a typo :frowning:
its actually the real "loop" :slight_smile: i corrected it. But this is not related to my issue

No sorry, this was a typo :frowning:

How do you have a typo when you only type Ctrl-A, Ctrl-C, Ctrl-V?

PaulS:
How do you have a typo when you only type Ctrl-A, Ctrl-C, Ctrl-V?

I didn't copied this, i have written this so only give the necessary things to make it better to understand

You need to post a minimal, compileable example which reproduces the problem. The posted code can neither compile nor produce the output you posted, so not easy to help :slight_smile:

Here is a version that generates the error:

#include <Adafruit_VL53L0X.h>
#include <Wire.h>


Adafruit_VL53L0X laser = Adafruit_VL53L0X();
VL53L0X_RangingMeasurementData_t laserMeasure;
bool laserCheckActive = false;
uint8_t laserLimit =55;

void setup(){
Serial.begin(9600);
Wire.begin();
laser.begin(VL53L0X_I2C_ADDR,1);
Serial.print("Current Milli");
Serial.println(millis());
laserCheckActive = true;
}


void loop(){


Serial.print(laserMeasure.RangeMilliMeter);
Serial.print("   ");
Serial.println(millis());
if(laserMeasure.RangeMilliMeter <= laserLimit){
laserCheckActive = false;
}

checkLaser(laserCheckActive);

}

void checkLaser(bool active){
 if(active==true){

 laser.getSingleRangingMeasurement(&laserMeasure,1);
 }
}

1.) Another thing i noticed is that it seems that the problem is only as long as the sensor is doing somehing. When the Sensor is disabled, the millis() will continue operating correctly (counting up) but will not reset, e.g. if the last (wrong) value from millis() is e.g. 65597 it will go ..98.99.65600 and so on.

2.) Also the problem only happens if the sensor is there, if disconnected it works fine (i thing the library is checking if the sensor is there

3.) to insure its not a hardware issue i checked the Crystal output (8Mhz) with a scope while a run, and it is spot on. So the clock should be fine.

The code is still not valid, in setup you refer "Wire" which is not included. PLEASE, make AND test a working, minimal sample which reproduces the error.

It compiles in ArduinoIDE and sloeber as well,

Wire is included in Adafruit_VL53L0X.h ... so no need to re-include? Anyway I'v edited my post so Wire is visible...

If you are certain that the code produces the problem, I'd suggest that it is either a bug in the adafruit library (less likely) or a wiring / hardware issue. The library used for the sensor uses interrupts, and if they are messing with the millis counter (which is also an interrupt) somehow that would be the cause. The sensor uses 3v, the Mega uses 5V - are you mixing different voltages, maybe?

The Mega runs on 3.3V in my case, the VL53L0X from ST comes on a "satelite" breakout board
http://www.st.com/resource/en/data_brief/53l0-satel-i1.pdf
that accepts 3-5V and does shift it.

Tomorrow i will try the polulu library to find out if that is different.

Could you explain why the library uses interrupts (where u found them?) as far as i know the sensor does only need power and i2c to work?.

Mabye the interrupts could be removed from the library.. :slight_smile:

I tried the polulu library and that one does not produce the error!
Also the polulu libarry is alot smaller than the adafruit one...