Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions examples/Example1_ReadDistance/Example1_ReadDistance.ino
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,13 @@ void setup(void)

if (distanceSensor.begin() == false)
Serial.println("Sensor offline!");

}

void loop(void)
{
distanceSensor.startMeasurement(); //Write configuration bytes to initiate measurement

//Poll for completion of measurement. Takes 40-50ms.
while (distanceSensor.newDataReady() == false)
delay(5);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@ void setup(void)
Serial.println("Sensor offline!");
}
//Call setDistanceMode with 0, 1, or 2 to change the sensing range.
distanceSensor.setDistanceMode(longRange);
distanceSensor.setDistanceMode(shortRange);
}

void loop(void)
{
distanceSensor.startMeasurement(); //Write configuration bytes to initiate measurement

//Poll for completion of measurement. Takes 40-50ms.
while (distanceSensor.newDataReady() == false)
delay(5);
Expand Down
1 change: 1 addition & 0 deletions examples/Example3_StatusAndRate/Example3_StatusAndRate.ino
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ void setup(void)
void loop(void)
{
long startTime = millis();
distanceSensor.startMeasurement(); //Write configuration block of 135 bytes to setup a measurement

//Poll for completion of measurement. Takes 40-50ms.
while (distanceSensor.newDataReady() == false)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
Reading distance from the laser based VL53L1X
By: Nathan Seidle
SparkFun Electronics
Date: April 4th, 2018
License: This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license).

SparkFun labored with love to create this code. Feel like supporting open source hardware?
Buy a board from SparkFun! https://www.sparkfun.com/products/14667

This example prints the distance to an object.

Are you getting weird readings? Be sure the vacuum tape has been removed from the sensor.
*/

#include <Wire.h>

#include "SparkFun_VL53L1X_Arduino_Library.h"
VL53L1X distanceSensor;

void setup(void)
{
Wire.begin();

Serial.begin(9600);
Serial.println("VL53L1X Qwiic Test");

if (distanceSensor.begin() == false)
Serial.println("Sensor offline!");

distanceSensor.setIntermeasurementPeriod(40);
Serial.println(distanceSensor.getIntermeasurementPeriod());
}

void loop(void)
{
distanceSensor.startMeasurement(); //Write configuration bytes to initiate measurement

//Poll for completion of measurement. Takes 40-50ms.
while (distanceSensor.newDataReady() == false)
delay(5);

int distance = distanceSensor.getDistance(); //Get the result of the measurement from the sensor

Serial.print("Distance(mm): ");
Serial.print(distance);

float distanceInches = distance * 0.0393701;
float distanceFeet = distanceInches / 12.0;

Serial.print("\tDistance(ft): ");
Serial.print(distanceFeet, 2);

Serial.println();
}

189 changes: 189 additions & 0 deletions examples/Example5_LCDDemo/Example5_LCDDemo.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
/*
Reading distance from the laser based VL53L1X
By: Nathan Seidle
SparkFun Electronics
Date: April 4th, 2018
License: This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license).

SparkFun labored with love to create this code. Feel like supporting open source hardware?
Buy a board from SparkFun! https://www.sparkfun.com/products/14667

Reading distance and outputting the distance and speed to a SerLCD.
This is based on the Speed Trap project: https://www.youtube.com/watch?v=uC9CkhJiIaQ

Are you getting weird readings? Make sure the vacuum tape has been removed.

*/
#include <Wire.h>

#include "SparkFun_VL53L1X_Arduino_Library.h"
VL53L1X distanceSensor;

#include <SoftwareSerial.h>

SoftwareSerial lcd(10, A3); // RX, TX

//Store distance readings to get rolling average
#define HISTORY_SIZE 8
int history[HISTORY_SIZE];
byte historySpot;

long lastReading = 0;
long lastDistance = 0;
float newDistance;
int maxDistance = 0;

const byte numberOfDeltas = 8;
float deltas[numberOfDeltas];
byte deltaSpot = 0; //Keeps track of where we are within the deltas array

//This controls how quickly the display updates
//Too quickly and it gets twitchy. Too slow and it doesn't seem like it's responding.
#define LOOPTIME 50

int maxMPH = 0; //Keeps track of what the latest fastest speed is
long maxMPH_timeout = 0; //Forget the max speed after some length of time

#define maxMPH_remember 3000 //After this number of ms the system will forget the max speed

boolean readingValid = false;
long validCount = 0;

void setup(void)
{
Wire.begin();
Wire.setClock(400000);

Serial.begin(115200);
Serial.println("VL53L1X Qwiic Test");

lcd.begin(9600);

lcd.write(254); //Move cursor to beginning of first line
lcd.write(128);
lcd.print("Distance: 3426 ");
lcd.print("12 mph ");

if (distanceSensor.begin() == false)
{
Serial.println("Sensor offline!");
}

for (int x = 0 ; x < HISTORY_SIZE ; x++)
history[x] = 0;
}

void loop(void)
{

//Write configuration block of 135 bytes to setup a measurement
distanceSensor.startMeasurement();

//Poll for completion of measurement. Takes 40-50ms.
while (distanceSensor.newDataReady() == false)
delay(5);

int distanceMM = distanceSensor.getDistance();

lastReading = millis();

history[historySpot] = distanceMM;
if (historySpot++ == HISTORY_SIZE) historySpot = 0;

long avgDistance = 0;
for (int x = 0 ; x < HISTORY_SIZE ; x++)
avgDistance += history[x];

avgDistance /= HISTORY_SIZE;


//Every loop let's get a reading
newDistance = distanceMM / 10; //Go get distance in cm

int deltaDistance = lastDistance - newDistance;
lastDistance = newDistance;

//Scan delta array to see if this new delta is sane or not
boolean safeDelta = true;
for (int x = 0 ; x < numberOfDeltas ; x++)
{
//We don't want to register jumps greater than 30cm in 50ms
//But if we're less than 1000cm then maybe
//30 works well
if ( abs(deltaDistance - deltas[x]) > 40) safeDelta = false;
}

//Insert this new delta into the array
if (safeDelta)
{
deltas[deltaSpot++] = deltaDistance;
if (deltaSpot > numberOfDeltas) deltaSpot = 0; //Wrap this variable
}

//Get average of the current deltas array
float avgDeltas = 0.0;
for (byte x = 0 ; x < numberOfDeltas ; x++)
avgDeltas += (float)deltas[x];
avgDeltas /= numberOfDeltas;

//22.36936 comes from a big coversion from cm per 50ms to mile per hour
float instantMPH = 22.36936 * (float)avgDeltas / (float)LOOPTIME;

instantMPH = abs(instantMPH); //We want to measure as you walk away

ceil(instantMPH); //Round up to the next number. This is helpful if we're not displaying decimals.

if (instantMPH > maxMPH)
{
maxMPH = instantMPH;
maxMPH_timeout = millis();
}

if (millis() - maxMPH_timeout > maxMPH_remember)
{
maxMPH = 0;
}

int signalRate = distanceSensor.getSignalRate();
if (signalRate < 10)
{
readingValid = false;
validCount = 0;
}
else
{
validCount++;
if (avgDistance > maxDistance) maxDistance = avgDistance;
}

if (validCount > 10) readingValid = true;

if (readingValid == false)
{
instantMPH = 0;
avgDistance = 0;
}

//Convert mm per millisecond to miles per hour
//float mph = distanceDelta * 2.236936 / (float)timeDelta;
//mph *= -1; //Flip sign as we will be traveling towards sensor, decreasing the distance

lcd.write(254); //Move cursor
lcd.write(138);
lcd.print(" ");
lcd.write(254); //Move cursor
lcd.write(138);

if (readingValid == true)
lcd.print(avgDistance);
else
lcd.print(maxDistance);

lcd.write(254); //Move cursor
lcd.write(192);
lcd.print(instantMPH, 0);
lcd.print(" mph ");

delay(25);
}

69 changes: 69 additions & 0 deletions examples/Example6_Calibration/Example6_Calibration.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
Reading distance from the laser based VL53L1X
By: Nathan Seidle
SparkFun Electronics
Date: April 4th, 2018
License: This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license).

SparkFun labored with love to create this code. Feel like supporting open source hardware?
Buy a board from SparkFun! https://www.sparkfun.com/products/14667

This example prints the distance to an object.

Are you getting weird readings? Be sure the vacuum tape has been removed from the sensor.
*/

#include <Wire.h>

#include "SparkFun_VL53L1X_Arduino_Library.h"
VL53L1X distanceSensor;

uint8_t incoming;
void setup(void)
{
Wire.begin();

Serial.begin(9600);
Serial.println("VL53L1X Qwiic Test");

if (distanceSensor.begin() == false)
Serial.println("Sensor offline!");

}

uint8_t OFFSET_MM = 140;

void loop(void)
{
distanceSensor.startMeasurement(); //Write configuration bytes to initiate measurement

//Poll for completion of measurement. Takes 40-50ms.
while (distanceSensor.newDataReady() == false)
delay(5);

while (incoming != 'c')
{
Serial.print("Calibrating, current offset is ");
Serial.print(distanceSensor.calibrateOffset(OFFSET_MM));
Serial.println(" mm");
if(Serial.available())
{
incoming = Serial.read();
}
};
Serial.print("Calibrated");

int distance = distanceSensor.getCalibratedDistance(); //Get the result of the measurement from the sensor

Serial.print("DistanceCal(mm): ");
Serial.print(distance);

float distanceInches = distance * 0.0393701;
float distanceFeet = distanceInches / 12.0;

Serial.print("\tDistance(ft): ");
Serial.print(distanceFeet, 2);

Serial.println();
}

8 changes: 8 additions & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ getRangeStatus KEYWORD2
setDistanceMode KEYWORD2
getDistanceMode KEYWORD2

setIntermeasurementPeriod KEYWORD2
getIntermeasurementPeriod KEYWORD2

setUserRoi KEYWORD2
setCenter KEYWORD2
setZoneSize KEYWORD2
getUserRoi KEYWORD2

readRegister KEYWORD2
readRegister16 KEYWORD2
writeRegister KEYWORD2
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=SparkFun VL53L1X 4m Laser Distance Sensor
version=1.0.5
version=1.0.6
author=SparkFun Electronics <techsupport@sparkfun.com>
maintainer=SparkFun Electronics <sparkfun.com>
sentence=Library for the SparkFun Qwiic 4m Distance Sensor - VL53L1X
Expand Down
Loading