// Note: comment out all Serial lines if you want to run it just from the outlet (i.e. as a toy device that doesn't get powered from laptop) #include "Adafruit_VL53L0X.h" #define SPEAKER_PIN 2 #define RED_PIN 8 #define GREEN_PIN 9 #define BLUE_PIN 10 Adafruit_VL53L0X lox = Adafruit_VL53L0X(); void setup() { Serial.begin(115200); // wait until serial port opens for native USB devices while (! Serial) { delay(1); } if (!lox.begin()) { Serial.println(F("Failed to boot VL53L0X")); while(1); } pinMode(SPEAKER_PIN, OUTPUT); pinMode(RED_PIN, OUTPUT); pinMode(GREEN_PIN, OUTPUT); pinMode(BLUE_PIN, OUTPUT); analogWrite(RED_PIN, 0); analogWrite(GREEN_PIN, 0); analogWrite(BLUE_PIN, 0); } void loop() { VL53L0X_RangingMeasurementData_t measure; Serial.print("Reading a measurement... "); lox.rangingTest(&measure, false); if (measure.RangeStatus != 4) { // phase failures have incorrect data int inputValue = measure.RangeMilliMeter; //somewhere between 50 and 300 Serial.print("Distance (mm): "); Serial.println(measure.RangeMilliMeter); int outputValue = inputValue*2+300; // somewhere between 400 and 900 tone(SPEAKER_PIN, outputValue, 100); // 0-255 range analogWrite(RED_PIN, inputValue-50); analogWrite(GREEN_PIN, inputValue-50); analogWrite(BLUE_PIN, inputValue-50); } else { Serial.println(" out of range "); tone(SPEAKER_PIN, 0, 150); } delay(150); }