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

VL53L0X sensor;
const int motorPin = A4; // PWM output pin for the vibration motor

void setup() {
  Serial.begin(9600); // Start serial communication
  Wire.begin(); // Initialize I2C communication
  sensor.init(); // Initialize the sensor
  sensor.setTimeout(500);
  sensor.startContinuous(); // Start continuous measurement
  pinMode(motorPin, OUTPUT); // Configure motor pin as output
}

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

  // Map the read distance to PWM range
  int motorSpeed = map(distance, 20, 100, 255, 0);
  motorSpeed = constrain(motorSpeed, 0, 255); // PWM within range
  
  // Control the vibration motor speed using PWM
  analogWrite(motorPin, motorSpeed);

  // delay(500); // Wait for a brief period before the next reading
}