|
| 1 | +/* This example shows how to use continuous mode to take |
| 2 | +range measurements with the VL53L0X. It is based on |
| 3 | +vl53l0x_ContinuousRanging_Example.c from the VL53L0X API. |
| 4 | +
|
| 5 | +The range readings are in units of mm. */ |
| 6 | + |
| 7 | +#include <Wire.h> |
| 8 | +#include <VL53L0X.h> |
| 9 | +#include <SPI.h> |
| 10 | +#include <Adafruit_GFX.h> |
| 11 | +#include <Adafruit_SSD1306.h> |
| 12 | + |
| 13 | +#include <Wire.h> |
| 14 | +#include "Adafruit_VL6180X.h" |
| 15 | + |
| 16 | +Adafruit_VL6180X vl = Adafruit_VL6180X(); |
| 17 | + |
| 18 | +Adafruit_SSD1306 display = Adafruit_SSD1306(); |
| 19 | + |
| 20 | +#if (SSD1306_LCDHEIGHT != 32) |
| 21 | + #error("Height incorrect, please fix Adafruit_SSD1306.h!"); |
| 22 | +#endif |
| 23 | + |
| 24 | +void setup() |
| 25 | +{ |
| 26 | + Serial.begin(9600); |
| 27 | + |
| 28 | + display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3C (for the 128x32) |
| 29 | + // init done |
| 30 | + display.display(); |
| 31 | + delay(1000); |
| 32 | + |
| 33 | + Serial.println("Adafruit VL6180x test!"); |
| 34 | + if (! vl.begin()) { |
| 35 | + Serial.println("Failed to find sensor"); |
| 36 | + while (1); |
| 37 | + } |
| 38 | + Serial.println("Sensor found!"); |
| 39 | + |
| 40 | + // text display big! |
| 41 | + display.setTextSize(4); |
| 42 | + display.setTextColor(WHITE); |
| 43 | +} |
| 44 | + |
| 45 | +void loop() |
| 46 | +{ |
| 47 | + float lux = vl.readLux(VL6180X_ALS_GAIN_5); |
| 48 | + |
| 49 | + Serial.print("Lux: "); Serial.println(lux); |
| 50 | + |
| 51 | + uint8_t range = vl.readRange(); |
| 52 | + uint8_t status = vl.readRangeStatus(); |
| 53 | + |
| 54 | + if (status == VL6180X_ERROR_NONE) { |
| 55 | + Serial.print("Range: "); Serial.println(range); |
| 56 | + display.clearDisplay(); |
| 57 | + display.setCursor(0,0); |
| 58 | + display.print(range); |
| 59 | + display.print("mm"); |
| 60 | + display.display(); |
| 61 | + Serial.println(); |
| 62 | + } else { |
| 63 | + display.display(); |
| 64 | + display.clearDisplay(); |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + // Some error occurred, print it out! |
| 69 | + |
| 70 | + if ((status >= VL6180X_ERROR_SYSERR_1) && (status <= VL6180X_ERROR_SYSERR_5)) { |
| 71 | + Serial.println("System error"); |
| 72 | + } |
| 73 | + else if (status == VL6180X_ERROR_ECEFAIL) { |
| 74 | + Serial.println("ECE failure"); |
| 75 | + } |
| 76 | + else if (status == VL6180X_ERROR_NOCONVERGE) { |
| 77 | + Serial.println("No convergence"); |
| 78 | + } |
| 79 | + else if (status == VL6180X_ERROR_RANGEIGNORE) { |
| 80 | + Serial.println("Ignoring range"); |
| 81 | + } |
| 82 | + else if (status == VL6180X_ERROR_SNR) { |
| 83 | + Serial.println("Signal/Noise error"); |
| 84 | + } |
| 85 | + else if (status == VL6180X_ERROR_RAWUFLOW) { |
| 86 | + Serial.println("Raw reading underflow"); |
| 87 | + } |
| 88 | + else if (status == VL6180X_ERROR_RAWOFLOW) { |
| 89 | + Serial.println("Raw reading overflow"); |
| 90 | + } |
| 91 | + else if (status == VL6180X_ERROR_RANGEUFLOW) { |
| 92 | + Serial.println("Range reading underflow"); |
| 93 | + } |
| 94 | + else if (status == VL6180X_ERROR_RANGEOFLOW) { |
| 95 | + Serial.println("Range reading overflow"); |
| 96 | + } |
| 97 | + delay(10); |
| 98 | +} |
0 commit comments