Created
May 1, 2018 13:46
-
-
Save battis/fb508f122d7f7926cae57d1f82e481b4 to your computer and use it in GitHub Desktop.
Laser Time-of-Flight Sensor Test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Wire.h> | |
#include <VL53L0X.h> | |
const long SENSOR_TIMEOUT = 500; | |
const long MAX_TESTS = 1000; | |
const long MAX_DURATION = 300000; //ms | |
VL53L0X sensor; | |
int count = 0; | |
int errors = 0; | |
int timeouts = 0; | |
void setup() { | |
pinMode(LED_BUILTIN, OUTPUT); | |
Serial.begin(9600); | |
Serial.println(); | |
divider(); | |
Serial.println("Serial interface initialized"); | |
Wire.begin(); | |
Serial.println("Wire interface initialized"); | |
sensor.init(); | |
Serial.println("Sensor initialized"); | |
sensor.setTimeout(SENSOR_TIMEOUT); | |
Serial.println("Sensor timeout set to " + (String) SENSOR_TIMEOUT); | |
divider(); | |
Serial.println("Beginning " + (String) MAX_TESTS + " tests over a max of " + (String) MAX_DURATION + " milliseconds"); | |
divider(); | |
digitalWrite(LED_BUILTIN, HIGH); | |
long start = millis(); | |
while (millis() < start + MAX_DURATION && count < MAX_TESTS) { | |
Serial.print("Test " + (String) (++count) + ": "); | |
int measurement = sensor.readRangeSingleMillimeters(); | |
if (measurement > 0) Serial.print((String) measurement + "mm"); | |
if (sensor.timeoutOccurred()) { | |
Serial.print("TIMEOUT"); | |
timeouts++; | |
} else if (measurement <= 0) { | |
Serial.print ((String) measurement + "mm ERROR"); | |
errors++; | |
} | |
Serial.println(" (" + (String) (count - errors - timeouts) + " / " + (String) count + ")"); | |
} | |
long end = millis(); | |
divider(); | |
digitalWrite(LED_BUILTIN, LOW); | |
Serial.println((String) count + " tests in " + (String) (end - start) + " milliseconds"); | |
Serial.println((String) (count - errors - timeouts) + " successful measurements"); | |
Serial.println((String) errors + " erroneous measurements"); | |
Serial.println((String) timeouts + " timeouts"); | |
divider(); | |
} | |
void divider() { | |
Serial.println("----------------------------------------------------------------------"); | |
} | |
void loop() { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment