Skip to content

Commit 764bc7d

Browse files
adding distanceMode, updating examples
1 parent 90cf2d1 commit 764bc7d

File tree

8 files changed

+418
-3
lines changed

8 files changed

+418
-3
lines changed

.gitattributes

Lines changed: 0 additions & 2 deletions
This file was deleted.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
Reading distance from the laser based VL53L1X
3+
By: Nathan Seidle
4+
SparkFun Electronics
5+
Date: April 4th, 2018
6+
License: This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license).
7+
8+
SparkFun labored with love to create this code. Feel like supporting open source hardware?
9+
Buy a board from SparkFun! https://www.sparkfun.com/products/14667
10+
11+
This example prints the distance to an object.
12+
13+
Are you getting weird readings? Be sure the vacuum tape has been removed from the sensor.
14+
*/
15+
16+
#include <Wire.h>
17+
18+
#include "SparkFun_VL53L1X_Arduino_Library.h"
19+
VL53L1X distanceSensor;
20+
21+
uint8_t shortRange = 0;
22+
uint8_t midRange = 1;
23+
uint8_t longRange = 2;
24+
25+
void setup(void)
26+
{
27+
Wire.begin();
28+
29+
Serial.begin(9600);
30+
Serial.println("VL53L1X Qwiic Test");
31+
32+
if (distanceSensor.begin() == false)
33+
{
34+
Serial.println("Sensor offline!");
35+
}
36+
//Call setDistanceMode with 0, 1, or 2 to change the sensing range.
37+
distanceSensor.setDistanceMode(shortRange);
38+
}
39+
40+
void loop(void)
41+
{
42+
distanceSensor.startMeasurement(); //Write configuration bytes to initiate measurement
43+
44+
//Poll for completion of measurement. Takes 40-50ms.
45+
while (distanceSensor.newDataReady() == false)
46+
delay(5);
47+
48+
int distance = distanceSensor.getDistance(); //Get the result of the measurement from the sensor
49+
50+
Serial.print("Distance(mm): ");
51+
Serial.print(distance);
52+
53+
float distanceInches = distance * 0.0393701;
54+
float distanceFeet = distanceInches / 12.0;
55+
56+
Serial.print("\tDistance(ft): ");
57+
Serial.print(distanceFeet, 2);
58+
59+
Serial.println();
60+
}
61+
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*
2+
Reading distance from the laser based VL53L1X
3+
By: Nathan Seidle
4+
SparkFun Electronics
5+
Date: April 4th, 2018
6+
License: This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license).
7+
8+
SparkFun labored with love to create this code. Feel like supporting open source hardware?
9+
Buy a board from SparkFun! https://www.sparkfun.com/products/14667
10+
11+
This example demonstrates how to read and average distance, the measurement status, and the signal rate.
12+
13+
Are you getting weird readings? Be sure the vacuum tape has been removed from the sensor.
14+
*/
15+
16+
#include <Wire.h>
17+
18+
#include "SparkFun_VL53L1X_Arduino_Library.h"
19+
VL53L1X distanceSensor;
20+
21+
//Store distance readings to get rolling average
22+
#define HISTORY_SIZE 10
23+
int history[HISTORY_SIZE];
24+
byte historySpot;
25+
26+
void setup(void)
27+
{
28+
Wire.begin();
29+
Wire.setClock(400000); //Increase I2C bus speed to 400kHz
30+
31+
Serial.begin(9600);
32+
Serial.println("VL53L1X Qwiic Test");
33+
34+
if (distanceSensor.begin() == false)
35+
Serial.println("Sensor offline!");
36+
37+
for (int x = 0 ; x < HISTORY_SIZE ; x++)
38+
history[x] = 0;
39+
}
40+
41+
void loop(void)
42+
{
43+
long startTime = millis();
44+
distanceSensor.startMeasurement(); //Write configuration block of 135 bytes to setup a measurement
45+
46+
//Poll for completion of measurement. Takes 40-50ms.
47+
while (distanceSensor.newDataReady() == false)
48+
delay(5);
49+
50+
long endTime = millis();
51+
52+
int distance = distanceSensor.getDistance(); //Get the result of the measurement from the sensor
53+
54+
Serial.print("Distance(mm): ");
55+
Serial.print(distance);
56+
57+
history[historySpot] = distance;
58+
if (historySpot++ == HISTORY_SIZE) historySpot = 0;
59+
60+
long avgDistance = 0;
61+
for (int x = 0 ; x < HISTORY_SIZE ; x++)
62+
avgDistance += history[x];
63+
64+
avgDistance /= HISTORY_SIZE;
65+
Serial.print("\tavgDistance: ");
66+
Serial.print(avgDistance);
67+
68+
float distanceInches = avgDistance * 0.0393701;
69+
float distanceFeet = distanceInches / 12;
70+
71+
Serial.print("\tavgDistance(ft): ");
72+
Serial.print(distanceFeet, 2);
73+
74+
int signalRate = distanceSensor.getSignalRate();
75+
Serial.print("\tSignal rate: ");
76+
Serial.print(signalRate);
77+
78+
byte rangeStatus = distanceSensor.getRangeStatus();
79+
Serial.print("\tRange Status: ");
80+
81+
//Make it human readable
82+
switch (rangeStatus)
83+
{
84+
case 0:
85+
Serial.print("Good");
86+
break;
87+
case 1:
88+
Serial.print("Signal fail");
89+
break;
90+
case 2:
91+
Serial.print("Sigma fail");
92+
break;
93+
case 7:
94+
Serial.print("Wrapped target fail");
95+
break;
96+
default:
97+
Serial.print("Unknown: ");
98+
Serial.print(rangeStatus);
99+
break;
100+
}
101+
102+
Serial.print("\tHz: ");
103+
Serial.print(1000.0 / (float)(endTime - startTime), 2);
104+
105+
Serial.println();
106+
}
107+
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
/*
2+
Reading distance from the laser based VL53L1X
3+
By: Nathan Seidle
4+
SparkFun Electronics
5+
Date: April 4th, 2018
6+
License: This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license).
7+
8+
SparkFun labored with love to create this code. Feel like supporting open source hardware?
9+
Buy a board from SparkFun! https://www.sparkfun.com/products/14667
10+
11+
Reading distance and outputting the distance and speed to a SerLCD.
12+
This is based on the Speed Trap project: https://www.youtube.com/watch?v=uC9CkhJiIaQ
13+
14+
Are you getting weird readings? Make sure the vacuum tape has been removed.
15+
16+
*/
17+
#include <Wire.h>
18+
19+
#include "SparkFun_VL53L1X_Arduino_Library.h"
20+
VL53L1X distanceSensor;
21+
22+
#include <SoftwareSerial.h>
23+
24+
SoftwareSerial lcd(10, A3); // RX, TX
25+
26+
//Store distance readings to get rolling average
27+
#define HISTORY_SIZE 8
28+
int history[HISTORY_SIZE];
29+
byte historySpot;
30+
31+
long lastReading = 0;
32+
long lastDistance = 0;
33+
float newDistance;
34+
int maxDistance = 0;
35+
36+
const byte numberOfDeltas = 8;
37+
float deltas[numberOfDeltas];
38+
byte deltaSpot = 0; //Keeps track of where we are within the deltas array
39+
40+
//This controls how quickly the display updates
41+
//Too quickly and it gets twitchy. Too slow and it doesn't seem like it's responding.
42+
#define LOOPTIME 50
43+
44+
int maxMPH = 0; //Keeps track of what the latest fastest speed is
45+
long maxMPH_timeout = 0; //Forget the max speed after some length of time
46+
47+
#define maxMPH_remember 3000 //After this number of ms the system will forget the max speed
48+
49+
boolean readingValid = false;
50+
long validCount = 0;
51+
52+
void setup(void)
53+
{
54+
Wire.begin();
55+
Wire.setClock(400000);
56+
57+
Serial.begin(115200);
58+
Serial.println("VL53L1X Qwiic Test");
59+
60+
lcd.begin(9600);
61+
62+
lcd.write(254); //Move cursor to beginning of first line
63+
lcd.write(128);
64+
lcd.print("Distance: 3426 ");
65+
lcd.print("12 mph ");
66+
67+
if (distanceSensor.begin() == false)
68+
{
69+
Serial.println("Sensor offline!");
70+
}
71+
72+
for (int x = 0 ; x < HISTORY_SIZE ; x++)
73+
history[x] = 0;
74+
}
75+
76+
void loop(void)
77+
{
78+
79+
//Write configuration block of 135 bytes to setup a measurement
80+
distanceSensor.startMeasurement();
81+
82+
//Poll for completion of measurement. Takes 40-50ms.
83+
while (distanceSensor.newDataReady() == false)
84+
delay(5);
85+
86+
int distanceMM = distanceSensor.getDistance();
87+
88+
lastReading = millis();
89+
90+
history[historySpot] = distanceMM;
91+
if (historySpot++ == HISTORY_SIZE) historySpot = 0;
92+
93+
long avgDistance = 0;
94+
for (int x = 0 ; x < HISTORY_SIZE ; x++)
95+
avgDistance += history[x];
96+
97+
avgDistance /= HISTORY_SIZE;
98+
99+
100+
//Every loop let's get a reading
101+
newDistance = distanceMM / 10; //Go get distance in cm
102+
103+
int deltaDistance = lastDistance - newDistance;
104+
lastDistance = newDistance;
105+
106+
//Scan delta array to see if this new delta is sane or not
107+
boolean safeDelta = true;
108+
for (int x = 0 ; x < numberOfDeltas ; x++)
109+
{
110+
//We don't want to register jumps greater than 30cm in 50ms
111+
//But if we're less than 1000cm then maybe
112+
//30 works well
113+
if ( abs(deltaDistance - deltas[x]) > 40) safeDelta = false;
114+
}
115+
116+
//Insert this new delta into the array
117+
if (safeDelta)
118+
{
119+
deltas[deltaSpot++] = deltaDistance;
120+
if (deltaSpot > numberOfDeltas) deltaSpot = 0; //Wrap this variable
121+
}
122+
123+
//Get average of the current deltas array
124+
float avgDeltas = 0.0;
125+
for (byte x = 0 ; x < numberOfDeltas ; x++)
126+
avgDeltas += (float)deltas[x];
127+
avgDeltas /= numberOfDeltas;
128+
129+
//22.36936 comes from a big coversion from cm per 50ms to mile per hour
130+
float instantMPH = 22.36936 * (float)avgDeltas / (float)LOOPTIME;
131+
132+
instantMPH = abs(instantMPH); //We want to measure as you walk away
133+
134+
ceil(instantMPH); //Round up to the next number. This is helpful if we're not displaying decimals.
135+
136+
if (instantMPH > maxMPH)
137+
{
138+
maxMPH = instantMPH;
139+
maxMPH_timeout = millis();
140+
}
141+
142+
if (millis() - maxMPH_timeout > maxMPH_remember)
143+
{
144+
maxMPH = 0;
145+
}
146+
147+
int signalRate = distanceSensor.getSignalRate();
148+
if (signalRate < 10)
149+
{
150+
readingValid = false;
151+
validCount = 0;
152+
}
153+
else
154+
{
155+
validCount++;
156+
if (avgDistance > maxDistance) maxDistance = avgDistance;
157+
}
158+
159+
if (validCount > 10) readingValid = true;
160+
161+
if (readingValid == false)
162+
{
163+
instantMPH = 0;
164+
avgDistance = 0;
165+
}
166+
167+
//Convert mm per millisecond to miles per hour
168+
//float mph = distanceDelta * 2.236936 / (float)timeDelta;
169+
//mph *= -1; //Flip sign as we will be traveling towards sensor, decreasing the distance
170+
171+
lcd.write(254); //Move cursor
172+
lcd.write(138);
173+
lcd.print(" ");
174+
lcd.write(254); //Move cursor
175+
lcd.write(138);
176+
177+
if (readingValid == true)
178+
lcd.print(avgDistance);
179+
else
180+
lcd.print(maxDistance);
181+
182+
lcd.write(254); //Move cursor
183+
lcd.write(192);
184+
lcd.print(instantMPH, 0);
185+
lcd.print(" mph ");
186+
187+
delay(25);
188+
}
189+

keywords.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ newDataReady KEYWORD2
2020
getDistance KEYWORD2
2121
getSignalRate KEYWORD2
2222
getRangeStatus KEYWORD2
23+
setDistanceMode KEYWORD2
24+
getDistanceMode KEYWORD2
2325

2426
readRegister KEYWORD2
2527
readRegister16 KEYWORD2

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=SparkFun VL53L1X 4m Laser Distance Sensor
2-
version=1.0.1
2+
version=1.0.2
33
author=SparkFun Electronics <techsupport@sparkfun.com>
44
maintainer=SparkFun Electronics <sparkfun.com>
55
sentence=Library for the SparkFun Qwiic 4m Distance Sensor - VL53L1X

0 commit comments

Comments
 (0)