50 lines
1.2 KiB
C++
50 lines
1.2 KiB
C++
#include <ComponentObject.h>
|
|
#include <RangeSensor.h>
|
|
#include <SparkFun_VL53L1X.h>
|
|
#include <vl53l1x_class.h>
|
|
#include <vl53l1_error_codes.h>
|
|
|
|
#include <Wire.h>
|
|
#include "SparkFun_VL53L1X.h" //Click here to get the library: http://librarymanager/All#SparkFun_VL53L1X
|
|
|
|
//Optional interrupt and shutdown pins.
|
|
#define SHUTDOWN_PIN 2
|
|
#define INTERRUPT_PIN 3
|
|
|
|
SFEVL53L1X distanceSensor;
|
|
//Uncomment the following line to use the optional shutdown and interrupt pins.
|
|
//SFEVL53L1X distanceSensor(Wire, SHUTDOWN_PIN, INTERRUPT_PIN);
|
|
|
|
void setup(void)
|
|
{
|
|
delay(100);
|
|
Wire.begin();
|
|
|
|
Serial.begin(115200);
|
|
|
|
if (distanceSensor.begin() != 0) //Begin returns 0 on a good init
|
|
{
|
|
Serial.println("Sensor failed to begin. Please check wiring. Freezing...");
|
|
while (1)
|
|
;
|
|
}
|
|
}
|
|
|
|
void loop(void)
|
|
{
|
|
distanceSensor.startRanging(); //Write configuration bytes to initiate measurement
|
|
while (!distanceSensor.checkForDataReady())
|
|
{
|
|
delay(1);
|
|
}
|
|
int distance = distanceSensor.getDistance(); //Get the result of the measurement from the sensor
|
|
distanceSensor.clearInterrupt();
|
|
distanceSensor.stopRanging();
|
|
int distance_cm = distance / 10;
|
|
|
|
Serial.print(" | ");
|
|
Serial.print(distance_cm);
|
|
Serial.println(" | ");
|
|
delay(100);
|
|
}
|