#include <Wire.h>
#include <VL53L0X.h>

VL53L0X sensor;
const int ledPin = D7; // pwm output for LED

void setup() {
  Serial.begin(9600); // Start serial communication
  Wire.begin(); // Start I2C communication
  sensor.init();
  sensor.setTimeout(500);
  sensor.startContinuous(); // continous meassurement of distance
  pinMode(ledPin, OUTPUT);
}

void loop() {
  uint16_t distance = sensor.readRangeContinuousMillimeters(); // reed distance
  Serial.print("Distancia: ");
  Serial.print(distance);
  Serial.println(" mm");

  // map the distance read to the pwm
  int brightness = map(distance, 20, 1000, 0, 255);
  brightness = constrain(brightness, 0, 255); // pwm in range  
  // assign the pwm to the led
  analogWrite(ledPin, brightness);

  delay(500); // Wait 500 ms for next read
}