Two time of flight sensors

I want to read two time of flight sensors one after the another. With this code i only read one value. If I try to read both values it will only print 1.

Sounds like a programming error. Please show your program.


Adafruit_VL53L0X lox = Adafruit_VL53L0X();

int XSHUT = 2;
int XSHUT1 = 3;

void setup() {
  Serial.begin(9600);
  pinMode(XSHUT, OUTPUT);
  pinMode(XSHUT1, OUTPUT);
  
  // wait until serial port opens for native USB devices
  while (! Serial) {
    delay(1);
  }

}


void loop() 
{
  digitalWrite(XSHUT1, HIGH);
  digitalWrite(XSHUT, LOW);
  delay(10);
    
  Serial.println("Adafruit VL53L0X test");
  if (!lox.begin()) {
    Serial.println(F("Failed to boot VL53L0X"));
    while(1);
  }
  // power 

  VL53L0X_RangingMeasurementData_t measure;
    
  lox.rangingTest(&measure, false); // pass in 'true' to get debug data printout!

  if (measure.RangeStatus != 4) {  // phase failures have incorrect data
    Serial.print("Distance1 (mm): "); Serial.println(measure.RangeMilliMeter);
  } else {
    Serial.println(" out of range ");
  }

  if(measure.RangeMilliMeter <= 100)
  {
    //Serial.println("Stop");
  }
  else{}

  digitalWrite(XSHUT, HIGH);
  digitalWrite(XSHUT1, LOW);
  delay(10);
    
  Serial.println("Adafruit VL53L0X test");
  if (!lox.begin()) {
    Serial.println(F("Failed to boot VL53L0X"));
    while(1);
  }
  // power 
    

  if (measure.RangeStatus != 4) {  // phase failures have incorrect data
    Serial.print("Distance (mm): "); Serial.println(measure.RangeMilliMeter);
  } else {
    Serial.println(" out of range ");
  }

  if(measure.RangeMilliMeter <= 100)
  {
    //Serial.println("Stop");
  }
  else{}

  

   
  delay(100);
}```

Would the OP want 2 Adafruit_VL53L0X objects; one for each device?

Such as

Adafruit_VL53L0X lox0 = Adafruit_VL53L0X();
Adafruit_VL53L0X lox1 = Adafruit_VL53L0X();

?

And. Please put your code in code tags.

No, I would like to have one object and just save the value to the same variable each time by writing over the last value

What does Serial Monitor show when you run your code?

To get more debug details on Serial Monitor, change:
if (!lox.begin()) {
to
if (!lox.begin(VL53L0X_I2C_ADDR, true)) {

The Serial Monitir shows this:
Adafruit VL53L0X test

Distance (mm): 302

Adafruit VL53L0X test

Distance1 (mm): 346

Adafruit VL53L0X test

Distance (mm): 346

Adafruit VL53L0X test

Distance1 (mm): 356

Adafruit VL53L0X test

Distance (mm): 356

Adafruit VL53L0X test

Distance1 (mm): 327

But that just seems to be one value and not two different values.

What different distances do you expect? What happens when you move one of the sensors closer to or further from the target?

It may not be a matter of preference. The library may not have been designed to allow one object to be used with more than one sensor, so it might be necessary to use one object for each sensor. Often, this detail is not described in the library documentation. If so, you could ask the question on Adafruit's forum.

If I move my hand above one of the two its printing the right value and if i do that with the other sensor it doesn't print the right value. It seems that with the second sensor the value of the firts gets printed in the Serial Monitor.

Got be, I'm not sure. I will check and otherwise ask on Adafruit's forum.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.