Adafruit_VL53L0X.h and Stepper.h seem incompatible

I am attempting to use Adafruit_VL53L0X.h and Stepper.h to take a single measurement, and then move a stepper motor 1 step.

What I expect to happen is the measurement is correct, and the stepper motor moves upon completion of the measurement.

what happens is, the measurement never changes, it remains at the initial value, and then the stepper motor moves.

#include <Stepper.h>
#include "Adafruit_VL53L0X.h"

#define STEPS 100

Stepper stepper(STEPS, 9, 10, 11, 12);
Adafruit_VL53L0X frontTOF = Adafruit_VL53L0X();

float ranges[100] = {0};
int p = 0;

void setup(){

  stepper.setSpeed(130);

}

void loop(){

  VL53L0X_RangingMeasurementData_t measure;
  
  frontTOF.rangingTest(&measure, false); 
  if (measure.RangeStatus != 4) {  
    
    while(p < 99) {
      ranges[p] = (float)measure.RangeMilliMeter/1000.0;
      stepper.step(+1);
      ++p;
    }

    while(p >= 0) {
      stepper.step(-1); 
      --p;
    }
  }
}

when I run the code and comment out the stepper.step(), I can turn the TOF sensor by hand and it produces correct values, its only when the TOF sesnsor and stepper motor are both initialized that the TOF Sensor produces incorrect values.

I have tried this with VL53L0X.h libaray as well and recieve the same incorrect measurements.

Hoping someone knows a way to allow both stepper.h and Adafruit_VL53L0X.h to function together.

*Edited to fix code block.

What is Adafruit_VL53L0X.h? Post a link to its documentation.

...R

You don't need to use the stepper library to turn a stepper motor. Here is an example I found somewhere on this forum:

/* This Arduino example demonstrates bidirectional operation of a 
 28BYJ-48, using a ULN2003 interface board to drive the stepper.
 The 28BYJ-48 motor is a 4-phase, 8-beat motor, geared down by
 a factor of 68. One bipolar winding is on motor pins 1 & 3 and
 the other on motor pins 2 & 4. The step angle is 5.625/64 and the 
 operating Frequency is 100pps. Current draw is 92mA. 
*/

//declare variables for the motor pins
int motorPin1 = 2;    // Blue   - 28BYJ48 pin 1
int motorPin2 = 3;    // Pink   - 28BYJ48 pin 2
int motorPin3 = 4;    // Yellow - 28BYJ48 pin 3
int motorPin4 = 5;    // Orange - 28BYJ48 pin 4
                        // Red    - 28BYJ48 pin 5 (VCC)

int motorSpeed = 1000;  //variable to set stepper speed
int count = 0;          // count of steps made
int countsperrev = 512; // number of steps per full revolution
int lookup[8] = {B01000, B01100, B00100, B00110, B00010, B00011, B00001, B01001};

void setup() {
  //declare the motor pins as outputs
  pinMode(motorPin1, OUTPUT);
  pinMode(motorPin2, OUTPUT);
  pinMode(motorPin3, OUTPUT);
  pinMode(motorPin4, OUTPUT);
//  Serial.begin(9600);
}

void loop(){
  if(count < countsperrev )
    clockwise();
  else if (count == countsperrev * 2)
    count = 0;
  else
    counterClockwise();
  count++;
}

//set pins to ULN2003 high in sequence from 1 to 4
//delay "motorSpeed" between each pin setting (to determine speed)
void counterClockwise()
{
  for(int i = 0; i < 8; i++)
  {
    setOutput(i);
    delayMicroseconds(motorSpeed);
  }
}

void clockwise()
{
  for(int i = 7; i >= 0; i--)
  {
    setOutput(i);
    delayMicroseconds(motorSpeed);
  }
}

void setOutput(int out)
{
  digitalWrite(motorPin1, bitRead(lookup[out], 0));
  digitalWrite(motorPin2, bitRead(lookup[out], 1));
  digitalWrite(motorPin3, bitRead(lookup[out], 2));
  digitalWrite(motorPin4, bitRead(lookup[out], 3));
}

Where is ranges defined? Is it sized properly?

Posting code that actually compiles is important.

PaulS:
Where is ranges defined? Is it sized properly?

Posting code that actually compiles is important.

If you are addressing me Paul, my code compiles and runs. If you are addressing the OP, I suspect it may not compile because the correct library has not been installed. I found v3 of the library, installed it, and it compiled. I couldn't test it, because I don't have a VL53L0X distance sensor handy.

The float array, ranges[100], is defined on line 10. The OP's sketch appears to put 100 floats into the array.

So I suspect that is not the problem. I do suspect its a resource conflict with Stepper.h, I dunno.

@peteh, have you made the "micro lidar" work with the Adafruit example code? Have you followed the procedure found in Adafruit's tutorial for hooking up, and using the device? Have you removed the protective plastic cover before using the device?

And finally, the range of the device is 1 foot to 1 meter. Do you have objects in the scanned area at that distance? If they are all out of range, the reading will never change, which is what you are seeing.

ChrisTenone:
If you are addressing me Paul

I was not.

PaulS:
I was not.

I figured, but you know ... internet communication! I wanted to cover all the bases.

Hopefully I addressed the possible problems the OP maybe having. Peteh is a one post wonder so far, but perhaps is still reading this thread. I suspect the problem with the project is that there is nothing in range. The VL53LOX device is referred to as a "micro LIDAR". There is a very impressive demo out there of a LIDAR being spun by a stepper motor creating a 3D map of the local space that it's placed in. An Arduino is used in that demo to spin the apparatus. I suspect that is what the OP is going for. With this Adafruit device, the mapped space would have to fit into a small box. If the space were too large for the device, the output would be random, which is what the OP is seeing.

Still here, I ended up ditching the Adafruit Library and using VL53L0X Library as its not as watered down and I was able to get it to report correct measurements with some work.

@Chris, I am indeed trying to use it as a short distance Lidar, for short range object avoidance, I also do believe there was a resource conflict in the Adafruit library, that does not exist in the VL530LX Library.

I'm glad you got it working.