Skip to content

Commit 0ad29f9

Browse files
Merge pull request #17 from sparkfun/Intermeasurement-period
Intermeasurement period and ROI Release.
2 parents 4e22e49 + 20b5752 commit 0ad29f9

File tree

10 files changed

+456
-81
lines changed

10 files changed

+456
-81
lines changed

examples/Example1_ReadDistance/Example1_ReadDistance.ino

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,13 @@ void setup(void)
2727

2828
if (distanceSensor.begin() == false)
2929
Serial.println("Sensor offline!");
30+
3031
}
3132

3233
void loop(void)
3334
{
35+
distanceSensor.startMeasurement(); //Write configuration bytes to initiate measurement
36+
3437
//Poll for completion of measurement. Takes 40-50ms.
3538
while (distanceSensor.newDataReady() == false)
3639
delay(5);

examples/Example2_SetDistanceMode/Example2_SetDistanceMode.ino

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,13 @@ void setup(void)
3434
Serial.println("Sensor offline!");
3535
}
3636
//Call setDistanceMode with 0, 1, or 2 to change the sensing range.
37-
distanceSensor.setDistanceMode(longRange);
37+
distanceSensor.setDistanceMode(shortRange);
3838
}
3939

4040
void loop(void)
4141
{
42+
distanceSensor.startMeasurement(); //Write configuration bytes to initiate measurement
43+
4244
//Poll for completion of measurement. Takes 40-50ms.
4345
while (distanceSensor.newDataReady() == false)
4446
delay(5);

examples/Example3_StatusAndRate/Example3_StatusAndRate.ino

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ void setup(void)
4141
void loop(void)
4242
{
4343
long startTime = millis();
44+
distanceSensor.startMeasurement(); //Write configuration block of 135 bytes to setup a measurement
4445

4546
//Poll for completion of measurement. Takes 40-50ms.
4647
while (distanceSensor.newDataReady() == false)
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
void setup(void)
22+
{
23+
Wire.begin();
24+
25+
Serial.begin(9600);
26+
Serial.println("VL53L1X Qwiic Test");
27+
28+
if (distanceSensor.begin() == false)
29+
Serial.println("Sensor offline!");
30+
31+
distanceSensor.setIntermeasurementPeriod(40);
32+
Serial.println(distanceSensor.getIntermeasurementPeriod());
33+
}
34+
35+
void loop(void)
36+
{
37+
distanceSensor.startMeasurement(); //Write configuration bytes to initiate measurement
38+
39+
//Poll for completion of measurement. Takes 40-50ms.
40+
while (distanceSensor.newDataReady() == false)
41+
delay(5);
42+
43+
int distance = distanceSensor.getDistance(); //Get the result of the measurement from the sensor
44+
45+
Serial.print("Distance(mm): ");
46+
Serial.print(distance);
47+
48+
float distanceInches = distance * 0.0393701;
49+
float distanceFeet = distanceInches / 12.0;
50+
51+
Serial.print("\tDistance(ft): ");
52+
Serial.print(distanceFeet, 2);
53+
54+
Serial.println();
55+
}
56+
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+
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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 incoming;
22+
void setup(void)
23+
{
24+
Wire.begin();
25+
26+
Serial.begin(9600);
27+
Serial.println("VL53L1X Qwiic Test");
28+
29+
if (distanceSensor.begin() == false)
30+
Serial.println("Sensor offline!");
31+
32+
}
33+
34+
uint8_t OFFSET_MM = 140;
35+
36+
void loop(void)
37+
{
38+
distanceSensor.startMeasurement(); //Write configuration bytes to initiate measurement
39+
40+
//Poll for completion of measurement. Takes 40-50ms.
41+
while (distanceSensor.newDataReady() == false)
42+
delay(5);
43+
44+
while (incoming != 'c')
45+
{
46+
Serial.print("Calibrating, current offset is ");
47+
Serial.print(distanceSensor.calibrateOffset(OFFSET_MM));
48+
Serial.println(" mm");
49+
if(Serial.available())
50+
{
51+
incoming = Serial.read();
52+
}
53+
};
54+
Serial.print("Calibrated");
55+
56+
int distance = distanceSensor.getCalibratedDistance(); //Get the result of the measurement from the sensor
57+
58+
Serial.print("DistanceCal(mm): ");
59+
Serial.print(distance);
60+
61+
float distanceInches = distance * 0.0393701;
62+
float distanceFeet = distanceInches / 12.0;
63+
64+
Serial.print("\tDistance(ft): ");
65+
Serial.print(distanceFeet, 2);
66+
67+
Serial.println();
68+
}
69+

keywords.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ getRangeStatus KEYWORD2
2323
setDistanceMode KEYWORD2
2424
getDistanceMode KEYWORD2
2525

26+
setIntermeasurementPeriod KEYWORD2
27+
getIntermeasurementPeriod KEYWORD2
28+
29+
setUserRoi KEYWORD2
30+
setCenter KEYWORD2
31+
setZoneSize KEYWORD2
32+
getUserRoi KEYWORD2
33+
2634
readRegister KEYWORD2
2735
readRegister16 KEYWORD2
2836
writeRegister 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.5
2+
version=1.0.6
33
author=SparkFun Electronics <[email protected]>
44
maintainer=SparkFun Electronics <sparkfun.com>
55
sentence=Library for the SparkFun Qwiic 4m Distance Sensor - VL53L1X

0 commit comments

Comments
 (0)