Skip to content

Commit 668078c

Browse files
committed
Update examples to freeze on failure
Previous examples would only print success, but then run regardless. This stops on failure and says so. Also added link to lib and changed to 115200bps.
1 parent 3d75699 commit 668078c

File tree

7 files changed

+237
-203
lines changed

7 files changed

+237
-203
lines changed

examples/Example1_ReadDistance/Example1_ReadDistance.ino

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
*/
2121

2222
#include <Wire.h>
23-
#include "SparkFun_VL53L1X.h"
23+
#include "SparkFun_VL53L1X.h" //Click here to get the library: http://librarymanager/All#SparkFun_VL53L1X
2424

2525
//Optional interrupt and shutdown pins.
2626
#define SHUTDOWN_PIN 2
@@ -34,19 +34,23 @@ void setup(void)
3434
{
3535
Wire.begin();
3636

37-
Serial.begin(9600);
37+
Serial.begin(115200);
3838
Serial.println("VL53L1X Qwiic Test");
3939

40-
if (distanceSensor.begin() == 0) //Begin returns 0 on a good init
40+
if (distanceSensor.begin() != 0) //Begin returns 0 on a good init
4141
{
42-
Serial.println("Sensor online!");
42+
Serial.println("Sensor failed to begin. Please check wiring. Freezing...");
43+
while (1)
44+
;
4345
}
46+
Serial.println("Sensor online!");
4447
}
4548

4649
void loop(void)
4750
{
4851
distanceSensor.startRanging(); //Write configuration bytes to initiate measurement
49-
while (!distanceSensor.checkForDataReady()) {
52+
while (!distanceSensor.checkForDataReady())
53+
{
5054
delay(1);
5155
}
5256
int distance = distanceSensor.getDistance(); //Get the result of the measurement from the sensor
@@ -64,4 +68,3 @@ void loop(void)
6468

6569
Serial.println();
6670
}
67-

examples/Example2_SetDistanceMode/Example2_SetDistanceMode.ino

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616

1717
#include <Wire.h>
18-
#include "SparkFun_VL53L1X.h"
18+
#include "SparkFun_VL53L1X.h" //Click here to get the library: http://librarymanager/All#SparkFun_VL53L1X
1919

2020
//Optional interrupt and shutdown pins.
2121
#define SHUTDOWN_PIN 2
@@ -29,14 +29,17 @@ void setup(void)
2929
{
3030
Wire.begin();
3131

32-
Serial.begin(9600);
32+
Serial.begin(115200);
3333
Serial.println("VL53L1X Qwiic Test");
3434

35-
if (distanceSensor.begin() == 0) //Begin returns 0 on a good init
35+
if (distanceSensor.begin() != 0) //Begin returns 0 on a good init
3636
{
37-
Serial.println("Sensor online!");
37+
Serial.println("Sensor failed to begin. Please check wiring. Freezing...");
38+
while (1)
39+
;
3840
}
39-
41+
Serial.println("Sensor online!");
42+
4043
distanceSensor.setDistanceModeShort();
4144
//distanceSensor.setDistanceModeLong();
4245
}
@@ -45,12 +48,13 @@ void loop(void)
4548
{
4649
distanceSensor.startRanging(); //Write configuration bytes to initiate measurement
4750

48-
while (!distanceSensor.checkForDataReady()) {
51+
while (!distanceSensor.checkForDataReady())
52+
{
4953
delay(1);
5054
}
5155
int distance = distanceSensor.getDistance(); //Get the result of the measurement from the sensor
5256
distanceSensor.clearInterrupt();
53-
57+
5458
distanceSensor.stopRanging();
5559

5660
Serial.print("Distance(mm): ");
@@ -64,4 +68,3 @@ void loop(void)
6468

6569
Serial.println();
6670
}
67-

examples/Example3_StatusAndRate/Example3_StatusAndRate.ino

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
*/
1515

1616
#include <Wire.h>
17-
#include "SparkFun_VL53L1X.h"
17+
#include "SparkFun_VL53L1X.h" //Click here to get the library: http://librarymanager/All#SparkFun_VL53L1X
1818

1919
//Optional interrupt and shutdown pins.
2020
#define SHUTDOWN_PIN 2
@@ -33,36 +33,43 @@ void setup(void)
3333
{
3434
Wire.begin();
3535

36-
Serial.begin(9600);
36+
Serial.begin(115200);
3737
Serial.println("VL53L1X Qwiic Test");
3838

39-
if (distanceSensor.begin() == 0) //Begin returns 0 on a good init
40-
Serial.println("Sensor online!");
39+
if (distanceSensor.begin() != 0) //Begin returns 0 on a good init
40+
{
41+
Serial.println("Sensor failed to begin. Please check wiring. Freezing...");
42+
while (1)
43+
;
44+
}
45+
Serial.println("Sensor online!");
4146

42-
for (int x = 0 ; x < HISTORY_SIZE ; x++)
47+
for (int x = 0; x < HISTORY_SIZE; x++)
4348
history[x] = 0;
4449
}
4550

4651
void loop(void)
4752
{
4853
long startTime = millis();
4954
distanceSensor.startRanging(); //Write configuration block of 135 bytes to setup a measurement
50-
while (!distanceSensor.checkForDataReady()) {
55+
while (!distanceSensor.checkForDataReady())
56+
{
5157
delay(1);
5258
}
5359
int distance = distanceSensor.getDistance(); //Get the result of the measurement from the sensor
5460
distanceSensor.clearInterrupt();
5561
distanceSensor.stopRanging();
5662
long endTime = millis();
57-
63+
5864
Serial.print("Distance(mm): ");
5965
Serial.print(distance);
6066

6167
history[historySpot] = distance;
62-
if (++historySpot == HISTORY_SIZE) historySpot = 0;
68+
if (++historySpot == HISTORY_SIZE)
69+
historySpot = 0;
6370

6471
long avgDistance = 0;
65-
for (int x = 0 ; x < HISTORY_SIZE ; x++)
72+
for (int x = 0; x < HISTORY_SIZE; x++)
6673
avgDistance += history[x];
6774

6875
avgDistance /= HISTORY_SIZE;
@@ -85,27 +92,26 @@ void loop(void)
8592
//Make it human readable
8693
switch (rangeStatus)
8794
{
88-
case 0:
89-
Serial.print("Good");
90-
break;
91-
case 1:
92-
Serial.print("Sigma fail");
93-
break;
94-
case 2:
95-
Serial.print("Signal fail");
96-
break;
97-
case 7:
98-
Serial.print("Wrapped target fail");
99-
break;
100-
default:
101-
Serial.print("Unknown: ");
102-
Serial.print(rangeStatus);
103-
break;
95+
case 0:
96+
Serial.print("Good");
97+
break;
98+
case 1:
99+
Serial.print("Sigma fail");
100+
break;
101+
case 2:
102+
Serial.print("Signal fail");
103+
break;
104+
case 7:
105+
Serial.print("Wrapped target fail");
106+
break;
107+
default:
108+
Serial.print("Unknown: ");
109+
Serial.print(rangeStatus);
110+
break;
104111
}
105112

106113
Serial.print("\tHz: ");
107114
Serial.print(1000.0 / (float)(endTime - startTime), 2);
108115

109116
Serial.println();
110117
}
111-

examples/Example4_SetIntermeasurementPeriod/Example4_SetIntermeasurementPeriod.ino

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
*/
1515

1616
#include <Wire.h>
17-
#include "SparkFun_VL53L1X.h"
17+
#include "SparkFun_VL53L1X.h" //Click here to get the library: http://librarymanager/All#SparkFun_VL53L1X
1818

1919
//Optional interrupt and shutdown pins.
2020
#define SHUTDOWN_PIN 2
@@ -28,12 +28,17 @@ void setup(void)
2828
{
2929
Wire.begin();
3030

31-
Serial.begin(9600);
31+
Serial.begin(115200);
3232
Serial.println("VL53L1X Qwiic Test");
3333

34-
if (distanceSensor.begin() == 0) //Begin returns 0 on a good init
34+
if (distanceSensor.begin() != 0) //Begin returns 0 on a good init
35+
{
36+
Serial.println("Sensor failed to begin. Please check wiring. Freezing...");
37+
while (1)
38+
;
39+
}
3540
Serial.println("Sensor online!");
36-
41+
3742
// Intermeasurement period must be >= timing budget. Default = 100 ms.
3843
distanceSensor.setIntermeasurementPeriod(200);
3944
Serial.println(distanceSensor.getIntermeasurementPeriod());
@@ -42,7 +47,8 @@ void setup(void)
4247

4348
void loop(void)
4449
{
45-
while (!distanceSensor.checkForDataReady()) {
50+
while (!distanceSensor.checkForDataReady())
51+
{
4652
delay(1);
4753
}
4854
int distance = distanceSensor.getDistance(); //Get the result of the measurement from the sensor
@@ -59,4 +65,3 @@ void loop(void)
5965

6066
Serial.println();
6167
}
62-

examples/Example5_LCDDemo/Example5_LCDDemo.ino

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
1616
*/
1717
#include <Wire.h>
18-
#include "SparkFun_VL53L1X.h"
18+
#include "SparkFun_VL53L1X.h" //Click here to get the library: http://librarymanager/All#SparkFun_VL53L1X
1919
#include <SoftwareSerial.h>
2020

2121
//Optional interrupt and shutdown pins.
@@ -50,7 +50,7 @@ byte deltaSpot = 0; //Keeps track of where we are within the deltas array
5050
//Too quickly and it gets twitchy. Too slow and it doesn't seem like it's responding.
5151
#define LOOPTIME 50
5252

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

5656
#define maxMPH_remember 3000 //After this number of ms the system will forget the max speed
@@ -73,12 +73,15 @@ void setup(void)
7373
lcd.print("Distance: 3426 ");
7474
lcd.print("12 mph ");
7575

76-
if (distanceSensor.begin() == 0) //Begin returns 0 on a good init
76+
if (distanceSensor.begin() != 0) //Begin returns 0 on a good init
7777
{
78-
Serial.println("Sensor online!");
78+
Serial.println("Sensor failed to begin. Please check wiring. Freezing...");
79+
while (1)
80+
;
7981
}
82+
Serial.println("Sensor online!");
8083

81-
for (int x = 0 ; x < HISTORY_SIZE ; x++)
84+
for (int x = 0; x < HISTORY_SIZE; x++)
8285
history[x] = 0;
8386
}
8487

@@ -87,25 +90,26 @@ void loop(void)
8790

8891
//Write configuration block of 135 bytes to setup a measurement
8992
distanceSensor.startRanging();
90-
while (!distanceSensor.checkForDataReady()) {
93+
while (!distanceSensor.checkForDataReady())
94+
{
9195
delay(1);
9296
}
9397
int distanceMM = distanceSensor.getDistance();
9498
distanceSensor.clearInterrupt();
9599
distanceSensor.stopRanging();
96-
100+
97101
lastReading = millis();
98102

99103
history[historySpot] = distanceMM;
100-
if (historySpot++ == HISTORY_SIZE) historySpot = 0;
104+
if (historySpot++ == HISTORY_SIZE)
105+
historySpot = 0;
101106

102107
long avgDistance = 0;
103-
for (int x = 0 ; x < HISTORY_SIZE ; x++)
108+
for (int x = 0; x < HISTORY_SIZE; x++)
104109
avgDistance += history[x];
105110

106111
avgDistance /= HISTORY_SIZE;
107112

108-
109113
//Every loop let's get a reading
110114
newDistance = distanceMM / 10; //Go get distance in cm
111115

@@ -114,24 +118,26 @@ void loop(void)
114118

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

125130
//Insert this new delta into the array
126131
if (safeDelta)
127132
{
128133
deltas[deltaSpot++] = deltaDistance;
129-
if (deltaSpot > numberOfDeltas) deltaSpot = 0; //Wrap this variable
134+
if (deltaSpot > numberOfDeltas)
135+
deltaSpot = 0; //Wrap this variable
130136
}
131137

132138
//Get average of the current deltas array
133139
float avgDeltas = 0.0;
134-
for (byte x = 0 ; x < numberOfDeltas ; x++)
140+
for (byte x = 0; x < numberOfDeltas; x++)
135141
avgDeltas += (float)deltas[x];
136142
avgDeltas /= numberOfDeltas;
137143

@@ -162,10 +168,12 @@ void loop(void)
162168
else
163169
{
164170
validCount++;
165-
if (avgDistance > maxDistance) maxDistance = avgDistance;
171+
if (avgDistance > maxDistance)
172+
maxDistance = avgDistance;
166173
}
167174

168-
if (validCount > 10) readingValid = true;
175+
if (validCount > 10)
176+
readingValid = true;
169177

170178
if (readingValid == false)
171179
{
@@ -195,4 +203,3 @@ void loop(void)
195203

196204
delay(25);
197205
}
198-

0 commit comments

Comments
 (0)