I have a problem with my code and I want help please, I'm using Vl53L0x

#include "Adafruit_VL53L0X.h"

Adafruit_VL53L0X lox = Adafruit_VL53L0X();
VL53L0X_RangingMeasurementData_t measure;


//define L298 enAble pins
const int ln1=11;                  // left wheel
const int ln2=10;
const int ln3=9;    //right wheel
const int ln4=8;    
const int enA=12;                 
const int enB=6;                  //!enAble pins have to connect with arduino's PWM pins!



#define carSpeed 100        //slower, include time for LiDAR sweep
#define carSpeedTurn 140    //turns can be faster

int time1;
int time2;

int pos=50;
const int stopDist = 30;

//Define movements
void forward() {
  analogWrite(enA, carSpeed);
  analogWrite(enB, carSpeed);
  digitalWrite(ln1, HIGH);
  digitalWrite(ln2, LOW);
  digitalWrite(ln3, HIGH);
  digitalWrite(ln4, LOW);
 
  Serial.println("Forward");
}

void back() {
  analogWrite(enA, carSpeedTurn);
  analogWrite(enB, carSpeedTurn);
  digitalWrite(ln1, LOW);
  digitalWrite(ln2, HIGH);
  digitalWrite(ln3, HIGH);
  digitalWrite(ln4, LOW);
  Serial.println("Back");
}

void left() {
  analogWrite(enA, carSpeedTurn);
  analogWrite(enB, carSpeedTurn);
  digitalWrite(ln1, LOW);
  digitalWrite(ln2, HIGH);
  digitalWrite(ln3, LOW);
  digitalWrite(ln4, HIGH);
  Serial.println("Left");
}

void right() {
  analogWrite(enA, carSpeedTurn);
  analogWrite(enB, carSpeedTurn);
  digitalWrite(ln1, HIGH);
  digitalWrite(ln2, LOW);
  digitalWrite(ln3, HIGH);
  digitalWrite(ln4, LOW);
  Serial.println("Right");
}

void stop() {
  digitalWrite(enA, LOW);
  digitalWrite(enB, LOW);
  Serial.println("Stop!");
}
//how the car decides in which direction to turn
int checkWay() {
  int rightDistance = distanceTest();
  Serial.println(rightDistance);

  delay(500);

  int leftDistance = distanceTest();
  Serial.println(leftDistance);

  delay(500);

  if (rightDistance > leftDistance) {
    right();
    Serial.println("Right");
    delay(600);
  }
  else if (rightDistance < leftDistance) {
    left();
    Serial.println("Left");
    delay(600);
  }
  else if ((rightDistance <= 150) || (leftDistance <= 150)) {
    back();
    Serial.println("Back");
    delay(180);
  }
  else if (rightDistance == leftDistance) {
    back();
    Serial.println("Back");
    delay(180);
  }
  else {
    forward();
  }
}
//define variable for LiDAR measurement
int distanceTest() {
  lox.rangingTest(&measure, false);
  delay(1);
  float Fdistance = measure.RangeMilliMeter;
  delay(1);
  return (int)Fdistance;
}
void setup() {
  pinMode(ln1,OUTPUT);            //all motor pins output
  pinMode(ln2,OUTPUT);
  pinMode(ln3,OUTPUT); 
  pinMode(ln4,OUTPUT);
  
  pinMode(enA,OUTPUT);
  pinMode(enB,OUTPUT);
Serial.begin(9600);

  while (! Serial) {
    delay(1);
  }

  Serial.println("VL53L0X");
  if (!lox.begin()) {
    Serial.println(F("Failed to boot VL53L0X"));
    while (1);
  }

  Serial.println(F("VL53L0X"));

  
}
  

void loop() 
{
//start LiDAR
  for (pos=50; pos;)  {
forward();
    lox.rangingTest(&measure, false); 

    if (measure.RangeStatus != 4) {  
      Serial.print("Position: ");
      Serial.print(pos);
      Serial.print("   ");
      Serial.print("Distance (mm): ");
      Serial.println(measure.RangeMilliMeter);
    }
    else {
           delay(500);

    }
    if (measure.RangeMilliMeter <= stopDist) {
      stop();

      delay(500);

      checkWay();
    }

    else {
      forward();
    }
  }
}

It’s a wall-following robot by using by VL53L0X sensor from behind

there’s a problem in the void loop, I don’t know how can I make it detect the wall (50mm) and drive the car forward

Hello.

Are you using our VL53L0X carrier? It seems like you are using an Adafruit library for the VL53L0X; just in case you are not aware, we have our own library for that sensor.

I am not familiar with the Adafruit library, so the amount of assistance we can offer will be limited. To troubleshoot I recommend trying much simpler programs to confirm that each of your components (the VL53L0X and the motor driver) each work by themselves as you expect. If you do that and have trouble figuring out what is going on, please post what parts you are using, the simplest program that demonstrates the problem, and tell us what you expect to happen versus what is actually happening. (If the parts you are having trouble with are from Adfruit and you are using their libraries, then I suggest contacting them for support.)

- Patrick