the code to get you started
#include <Wire.h>
#include <VL6180X.h>
#include <AccelStepper.h>
VL6180X sensor;
// Define pin connections & motor's steps per revolution
const int dirPin = 2;
const int stepPin = 3;
const int stepsPerRevolution = 500;
float dist, sens;
int count;
void setup()
{
Wire.begin();
// Declare pins as Outputs
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
sensor.init();
sensor.configureDefault();
// Reduce range max convergence time and ALS integration
// time to 30 ms and 50 ms, respectively, to allow 10 Hz
// operation (as suggested by Table 6 ("Interleaved mode
// limits (10 Hz operation)" in the datasheet).
sensor.writeReg(VL6180X:YSRANGE__MAX_CONVERGENCE_TIME, 05);
//sensor.writeReg16Bit(VL6180X:YSALS__INTEGRATION_PERIOD, 10);
sensor.setTimeout(1000);
// stop continuous mode if already active
sensor.stopContinuous();
// in case stopContinuous() triggered a single-shot
// measurement, wait for it to complete
delay(300);
//READOUT__AVERAGING_SAMPLE_PERIOD
sensor.writeReg(0x10A, 0x08);
// start interleaved continuous mode with period of 100 ms
//sensor.startInterleavedContinuous(100);
sensor.startRangeContinuous(10);
Serial.begin(57600);
//ideas
// Look at docs to see how to improve accuracy
// only use samples that converged
// look into whether samples are used twice or other issues with the uncoltrolled loop
// allow more variability in the output of the sensor and average in the mcu
// measure drift by connecting to the CNC machine to get precise moves
count=1;
}
void loop()
{
//Serial.print("Ambient: "
//Serial.print(sensor.readAmbientContinuous());
//if (sensor.timeoutOccurred()) { Serial.print(" TIMEOUT" }
// Set motor direction clockwise
digitalWrite(dirPin, HIGH);
if (dist>60)
{ digitalWrite(dirPin, LOW);
}
sens=sensor.readRangeContinuous();
if(sens<255)
{
if( abs(sens-dist)>10.0) {dist=sens;}
dist = .995* dist + .005 * sens;
}
// Spin motor slowly
for(int x = 0; x < stepsPerRevolution; x++)
{
digitalWrite(stepPin, HIGH);
delayMicroseconds(2000);
digitalWrite(stepPin, LOW);
delayMicroseconds(2000);
}
// delay(1000); // Wait a second
count=count-1;
if (count ==0)
{
count=1;
//Serial.print("t Range: "
Serial.print(dist);
Serial.println();
}
if (sensor.timeoutOccurred()) { Serial.print(" TIMEOUT" Serial.println(); }
}