Mkr1500 & tca9548a & vl53l0x

Accepted wisdom says if you understand code then use it, If you cant understand code don’t use it. I have found code which I need on Github and acknowledge the original author. It is at the limit of my understanding so I have commented the code more fully to show my understanding of each line. If I have misunderstood the code can you please point out my errors and suggest where I might find information to better understand.

/**
 * CODE by Johnathan Martin a.k.a the_jmart
 * By using the TCA9548A I2C Multiplexer Breakout you can read
 * four VL53L0X Time-of-Flight range sensors at the same time over 
 * one I2C address. This code is written specific for the devices 
 * used. 
 * 
 * TCA9548 I2CScanner.pde -- I2C bus scanner for Arduino
 * Based on code c. 2009, Tod E. Kurt, http://todbot.com/blog/
 *
 */

#include "Wire.h"
/*extern "C" {                                                                                ***MKR1500 doesn't need/want this
#include "utility/twi.h"  // from Wire library, so we can do bus scanning
}  */
#include "Adafruit_VL53L0X.h"

Adafruit_VL53L0X lox = Adafruit_VL53L0X();


#define TCAADDR 0x70

/*
 * Adafruit "suggest using this little helper to help you select the port" the next 7 lines
 * 
 */

void tcaselect(uint8_t i) {                     //An 8 bit unsigned integer, i is passed to a void function
  if (i > 7) return;                            //if (i>7) terminate the (void)function without returning anything
 
  Wire.beginTransmission(TCAADDR);
  Wire.write(1 << i);                           //left shift 1 by i places ==i.e. put i zeros to the right of it, then write that to the TCA9548A on I2C 
  Wire.endTransmission();  
}
// standard Arduino setup()
void setup()
{
    while (!Serial);
    delay(1000);
    Wire.begin();
    Serial.begin(9600);

    Serial.print("Start.");
    
    tcaselect(0);                                   //call the function tcaselect and pass the function 0 so i = 0,         so 1 is left shifted 0 places so 0b00000001 gets sent to the TCA9548A, opening channel 0
    if (!lox.begin()) {                             //if sensor doesn't respond
    Serial.println(F("Failed to boot VL53L0X"));    //serial report the failure
    while(1);                                       //enter infinite loop and do nothing
    }
    tcaselect(1);                             //call the function tcaselect and pass the function 1 so i = 1,         so 1 is left shifted 1 places so 0b00000010 gets sent to the TCA9548A, opening channel 1
    if (!lox.begin()) {
    Serial.println(F("Failed to boot VL53L0X"));
    while(1);
    }
    tcaselect(2);                             //call the function tcaselect and pass the function 2 so i = 2,         so 1 is left shifted 2 places so 0b00000100 gets sent to the TCA9548A, opening channel 2
    if (!lox.begin()) {
    Serial.println(F("Failed to boot VL53L0X"));
    while(1);
    }
    tcaselect(3);                             //call the function tcaselect and pass the function 3 so i = 3,         so 1 is left shifted 3 places so 0b00001000 gets sent to the TCA9548A, opening channel 3                           
    if (!lox.begin()) {
    Serial.println(F("Failed to boot VL53L0X"));
    while(1);
    }
    /*                                          only using using channels 0-3
    tcaselect(7);
    if (!lox.begin()) {
    Serial.println(F("Failed to boot VL53L0X"));
    while(1);
    }     */
    
    Serial.println(" End setup");
    delay(2000);
    
}

void loop() 
{
  
  VL53L0X_RangingMeasurementData_t measure;

   tcaselect(0);                                    //call the function tcaselect and pass the function 0 so i = 0,         so 1 is left shifted 0 places so 0b00000001 gets sent to the TCA9548A, opening channel 0
    lox.rangingTest(&measure, false);               // pass in 'true' to get debug data printout!

    if (measure.RangeStatus != 4) {                 // phase failures have incorrect data
      Serial.print("L0: "); Serial.print(measure.RangeMilliMeter);Serial.print(" ");        //print that sensor 0 responds xxxmm  , 
    } else {
      Serial.print("L0: "); Serial.print("OOR"); Serial.print(" ");                         // //print that sensor 0 is Out Of Range  , 
    }
    
    tcaselect(1);
    lox.rangingTest(&measure, false); // pass in 'true' to get debug data printout!

    if (measure.RangeStatus != 4) {  // phase failures have incorrect data
      Serial.print("L1: "); Serial.print(measure.RangeMilliMeter);Serial.print(" ");
    } else {
      Serial.print("L1: "); Serial.print("OOR");Serial.print(" ");
    }
    
    tcaselect(2);
    lox.rangingTest(&measure, false); // pass in 'true' to get debug data printout!

    if (measure.RangeStatus != 4) {  // phase failures have incorrect data
      Serial.print("L2: "); Serial.print(measure.RangeMilliMeter);Serial.print(" ");
    } else {
      Serial.print("L2: "); Serial.print("OOR");Serial.print(" ");
    }
    
    tcaselect(7);
    lox.rangingTest(&measure, false); // pass in 'true' to get debug data printout!

    if (measure.RangeStatus != 4) {  // phase failures have incorrect data
      Serial.print("L3: "); Serial.print(measure.RangeMilliMeter);Serial.print(" ");
    } else {
      Serial.print("L3: "); Serial.print("OOR");Serial.print(" ");
    }
    Serial.println("");
    
    delay(20);
 
}

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