|
| 1 | +/* |
| 2 | + Reading distance from the laser based VL53L1X |
| 3 | + By: Nathan Seidle |
| 4 | + Revised by: Andy England and Ricardo Ramos |
| 5 | + SparkFun Electronics |
| 6 | + Date: January 21st, 2022 |
| 7 | + License: This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license). |
| 8 | +
|
| 9 | + SparkFun labored with love to create this code. Feel like supporting open source hardware? |
| 10 | + Buy a board from SparkFun! https://www.sparkfun.com/products/14667 |
| 11 | +
|
| 12 | + This example sets high and low thresholds for detection. |
| 13 | +
|
| 14 | + Are you getting weird readings? Be sure the vacuum tape has been removed from the sensor. |
| 15 | +*/ |
| 16 | + |
| 17 | +#include <Wire.h> |
| 18 | +#include "SparkFun_VL53L1X.h" //Click here to get the library: http://librarymanager/All#SparkFun_VL53L1X |
| 19 | + |
| 20 | +//Optional interrupt and shutdown pins. |
| 21 | +#define SHUTDOWN_PIN 2 |
| 22 | +#define INTERRUPT_PIN 3 |
| 23 | + |
| 24 | +SFEVL53L1X distanceSensor; |
| 25 | +//Uncomment the following line to use the optional shutdown and interrupt pins. |
| 26 | +//SFEVL53L1X distanceSensor(Wire, SHUTDOWN_PIN, INTERRUPT_PIN); |
| 27 | + |
| 28 | +void setup(void) |
| 29 | +{ |
| 30 | + Wire.begin(); |
| 31 | + |
| 32 | + Serial.begin(115200); |
| 33 | + Serial.println("VL53L1X Qwiic Test"); |
| 34 | + |
| 35 | + if (distanceSensor.begin() != 0) //Begin returns 0 on a good init |
| 36 | + { |
| 37 | + Serial.println("Sensor failed to begin. Please check wiring. Freezing..."); |
| 38 | + while (1) |
| 39 | + ; |
| 40 | + } |
| 41 | + Serial.println("Sensor online!"); |
| 42 | + |
| 43 | + DetectionConfig dc; // struct instance which holds the detection configuration |
| 44 | + dc.IntOnNoTarget = 1; // No longer used - just use 1 per ST |
| 45 | + dc.distanceMode = DISTANCE_SHORT; // short distance mode |
| 46 | + dc.thresholdHigh = 300; // high threshold of 300 mm |
| 47 | + dc.thresholdLow = 70; // low threshold of 70 mm |
| 48 | + dc.windowMode = WINDOW_IN; // will measure and trigger interrrupt when measurement fall between 70 and 300 mm |
| 49 | + |
| 50 | + if(distanceSensor.setThresholdConfig(&dc) == true) |
| 51 | + { |
| 52 | + Serial.println("Thresholds programmed. Reading configuration back..."); |
| 53 | + dc = {}; |
| 54 | + if(distanceSensor.getThresholdConfig(&dc) == true) |
| 55 | + { |
| 56 | + Serial.print("IntOnNoTarget: "); |
| 57 | + Serial.println(dc.IntOnNoTarget); |
| 58 | + Serial.print("Distance Mode: "); |
| 59 | + |
| 60 | + if(dc.distanceMode == DISTANCE_SHORT) |
| 61 | + Serial.println("DISTANCE_SHORT (0)"); |
| 62 | + else |
| 63 | + Serial.println("DISTANCE_LONG (1)"); |
| 64 | + |
| 65 | + Serial.print("Threshold High: "); |
| 66 | + Serial.println(dc.thresholdHigh); |
| 67 | + Serial.print("Threshold Low: "); |
| 68 | + Serial.println(dc.thresholdLow); |
| 69 | + Serial.print("Window mode: "); |
| 70 | + switch (dc.windowMode) |
| 71 | + { |
| 72 | + case WINDOW_BELOW: |
| 73 | + Serial.println("WINDOW_BELOW (0)"); |
| 74 | + break; |
| 75 | + |
| 76 | + case WINDOW_ABOVE: |
| 77 | + Serial.println("WINDOW_ABOVE (1)"); |
| 78 | + break; |
| 79 | + |
| 80 | + case WINDOW_OUT: |
| 81 | + Serial.println("WINDOW_OUT (2)"); |
| 82 | + break; |
| 83 | + |
| 84 | + case WINDOW_IN: |
| 85 | + Serial.println("WINDOW_IN (3)"); |
| 86 | + break; |
| 87 | + |
| 88 | + default: |
| 89 | + break; |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + else |
| 94 | + { |
| 95 | + Serial.println("Could not set threshold configuration."); |
| 96 | + } |
| 97 | + Serial.println("Starting measurements in 2 seconds."); |
| 98 | + delay(2000); |
| 99 | + Serial.println("Measurements started!"); |
| 100 | + |
| 101 | +} |
| 102 | + |
| 103 | +void loop(void) |
| 104 | +{ |
| 105 | + distanceSensor.startRanging(); //Write configuration bytes to initiate measurement |
| 106 | + |
| 107 | + while (!distanceSensor.checkForDataReady()) |
| 108 | + { |
| 109 | + delay(1); |
| 110 | + } |
| 111 | + int distance = distanceSensor.getDistance(); //Get the result of the measurement from the sensor |
| 112 | + distanceSensor.clearInterrupt(); |
| 113 | + |
| 114 | + distanceSensor.stopRanging(); |
| 115 | + |
| 116 | + Serial.print("Distance(mm): "); |
| 117 | + Serial.print(distance); |
| 118 | + |
| 119 | + float distanceInches = distance * 0.0393701; |
| 120 | + float distanceFeet = distanceInches / 12.0; |
| 121 | + |
| 122 | + Serial.print("\tDistance(ft): "); |
| 123 | + Serial.print(distanceFeet, 2); |
| 124 | + |
| 125 | + Serial.println(); |
| 126 | +} |
0 commit comments