Problem logging values from 2 sensors to SD card

I'm having some issues writing sensor data to an SD card from 2 VEML6075 UV sensors. These are connected to a UNO R3 with data logging shield for arduino via an Adafruit TCA9548A MUX (I will be adding more UV sensors once I can get 2 to log). I have confirmed that the two sensors are working correctly through the serial monitor.

The sensors are recognised and correctly log sensor 1 values, but log the sensor 1 values for sensor 2. I can't figure out where I've programmed it incorrectly and was hoping somebody could identify the issue.

Here is my code:

#include <SparkFun_VEML6075_Arduino_Library.h>
#include "Wire.h" //include Wire library
#include <SD.h>
#include <RTClib.h>

#define TCAADDR 0x70 //define then select multiplexor port address

#define SYNC_INTERVAL 1000 // mills between calls to flush() - to write data to the card
#define LOG_INTERVAL  1000 // mills between entries (reduce to take more/faster data)
uint32_t syncTime = 0; // time of last sync()
#define ECHO_TO_SERIAL   1 // echo data to serial port
#define WAIT_TO_START    0 // Wait for serial input in setup()

/* Assign a unique ID to this sensor at the same time  - do this for MCP9808 later – may have to change orders?!*/
VEML6075 uv1 = VEML6075();
VEML6075 uv2 = VEML6075();

// chip select for SD card
const int SD_CS_PIN = 10;  

/* Set up the 8 ports on multiplexor */
void tcaselect(uint8_t i) {
  if (i > 7) return;
  Wire.beginTransmission(TCAADDR);
  Wire.write(1 << i);
  Wire.endTransmission();  
}

// variables
float UVa1 = 0;
float UVb1 = 0;
float UVa2 = 0;
float UVb2 = 0;

// the logging file
File logfile;


//start setup
void setup(void)  
 {
  Wire.begin();
  Serial.begin(9600);
    
    /* Initialise the 1st sensor */
    
  tcaselect(5);
if(! uv1.begin())
{
  Serial.println("Failed to communicate with VEML6075 sensor1, check wiring?");
    while(1);
  }
/* Initialise the 2nd sensor */
    tcaselect(6);
if(! uv2.begin())
{
    Serial.println("Failed to communicate with VEML6075 sensor1, check wiring?");
    while(1);
  }

 // initialize the SD card
  pinMode(10, OUTPUT);

   // create a new file
  char filename[] = "LOGGER00.CSV";
  for (uint8_t i = 0; i < 100; i++) {
    filename[6] = i/10 + '0';
    filename[7] = i%10 + '0';
    if (! SD.exists(filename)) {
      // only open a new file if it doesn't exist
      logfile = SD.open(filename, FILE_WRITE); 
      break;  // leave the loop!
    }
  }

  /**************** Initialise the sensors ******************/
  
logfile.println("millis,UVA1,UVB1,UVA2,UVB2");    
#if ECHO_TO_SERIAL
  Serial.println("millis,UVA1,UVB1,UVA2,UVB2");
#endif //ECHO_TO_SERIAL
  
}

//Now for the loop:
void loop() {
 
  // delay for the amount of time we want between readings
  delay((LOG_INTERVAL -1) - (millis() % LOG_INTERVAL));
  
    // log milliseconds since starting
  uint32_t m = millis();
  logfile.print(m);           // milliseconds since start
  logfile.print(", ");    
#if ECHO_TO_SERIAL
  Serial.print(m);         // milliseconds since start
  Serial.print(", ");  
#endif

// read the sensors values
  UVa1 = uv1.rawUva();
  UVb1 = uv1.rawUvb();
  UVa2 = uv2.rawUva();
  UVb2 = uv2.rawUvb();

logfile.print(", ");    
  logfile.print(String(UVa1));
    logfile.print(", ");
    logfile.print(String(UVb1));
    logfile.print(", ");    
  logfile.print(String(UVa2)); //problem - it's writing the same as UVa1
    logfile.print(", ");
    logfile.print(String(UVb2)); //problem - it's writing the same as UVb2
  #if ECHO_TO_SERIAL
  Serial.print(", ");   
  Serial.print(UVa1);
  Serial.print(", ");   
  Serial.print(UVb1);
  Serial.print(", ");   
  Serial.print(UVa2); //it's printing unique value to UVa1
  Serial.print(", ");   
  Serial.print(UVb2); //it's printing unique value to UVb1
  #endif //ECHO_TO_SERIAL 

logfile.println();
#if ECHO_TO_SERIAL
  Serial.println();
#endif // ECHO_TO_SERIAL
// Now we write data to disk! Don't sync too often - requires 2048 bytes of I/O to SD card
  // which uses a bunch of power and takes time
  if ((millis() - syncTime) < SYNC_INTERVAL) return;
  syncTime = millis();
    logfile.flush();

  /* Display the results */
tcaselect(5);
  Serial.print("Sensor #1 - ");
  Serial.print("Raw UVA reading:  "); Serial.println(uv1.rawUva());
  Serial.print("Sensor #1 - ");
  Serial.print("Raw UVB reading:  "); Serial.println(uv1.rawUvb());

  tcaselect(6);
  Serial.print("Sensor #2 - ");
  Serial.print("Raw UVA reading:  "); Serial.println(uv2.rawUva());
  Serial.print("Sensor #2 - ");
  Serial.print("Raw UVB reading:  "); Serial.println(uv2.rawUvb());

  delay(1000);
}

Here is the output from the serial monitor at a given time point:

12:18:52.176 -> millis,UVA1,UVB1,UVA2,UVB2
12:18:53.165 -> 999, , 88.00, 114.00, 88.00, 114.00
12:18:53.200 -> Sensor #1 - Raw UVA reading: 7
12:18:53.235 -> Sensor #1 - Raw UVB reading: 11
12:18:53.270 -> Sensor #2 - Raw UVA reading: 88
12:18:53.305 -> Sensor #2 - Raw UVB reading: 114

You can see that the sensor information is correct on lines 3-6, however the values being written to the SD card for the second sensor mirror that of the first sensor.

Thanks

Just as a sanity check could you also post some of the log file, please?

No problem:

millis UVA1 UVB1 UVA2 UVB2
999 92 119 92 119
2999 92 119 92 119
4999 92 118 92 118
6999 91 118 91 118
8998 92 119 92 119
10999 92 119 92 119

You can see that the sensor two readings (UVA2 and UVB2) are just the sensor 1 readings (UVA1 and UVB1). In the serial monitor output above you can see that these values are indeed different.

  // read the sensors values
  UVa1 = uv1.rawUva();
  UVb1 = uv1.rawUvb();
  UVa2 = uv2.rawUva();
  UVb2 = uv2.rawUvb();

  logfile.print(", ");
  logfile.print(String(UVa1));
  logfile.print(", ");
  logfile.print(String(UVb1));
  logfile.print(", ");
  logfile.print(String(UVa2)); //problem - it's writing the same as UVa1
  logfile.print(", ");
  logfile.print(String(UVb2)); //problem - it's writing the same as UVb2

Does not look like you are switching inputs on the mux between the uv1 and uv2 reads.

Also, it would be a good idea to get rid of String, print() will work directly with floats, and rawUVa/rawUVb return an integer anyway, so no use storing them as floats.

Thanks for the help David, I've removed the String and kept print().

Does not look like you are switching inputs on the mux between the uv1 and uv2 reads.

Pardon my ignorance, but do you have any idea how I might switch the input here? I have tried changing the order of that section of code you pasted to this:

// read the sensors values
  UVa1 = uv1.rawUva();
  UVb1 = uv1.rawUvb();

logfile.print(", ");    
  logfile.print(UVa1);
    logfile.print(", ");
    logfile.print(UVb1);

  #if ECHO_TO_SERIAL
  Serial.print(", ");   
  Serial.print(UVa1);
  Serial.print(", ");   
  Serial.print(UVb1);
  #endif //ECHO_TO_SERIAL 

// read the sensors values
  UVa2 = uv2.rawUva();
  UVb2 = uv2.rawUvb();
  
      logfile.print(", ");    
  logfile.print(UVa2); //problem - it's writing the same as UVa1
    logfile.print(", ");
    logfile.print(UVb2); //problem - it's writing the same as UVb2

  #if ECHO_TO_SERIAL
  Serial.print(", ");   
  Serial.print(UVa2); //it's printing unique value to UVa1
  Serial.print(", ");   
  Serial.print(UVb2); //it's printing unique value to UVb1
  #endif //ECHO_TO_SERIAL

However, this did not help. I'm new to programming and a bit lost.

Cheers

Nevermind, I realised how. Thanks so much for your help. Here is how I switched inputs on the mux:

  // read the sensors values
  UVa1 = uv1.rawUva();
  UVb1 = uv1.rawUvb();
  UVa2 = uv2.rawUva();
  UVb2 = uv2.rawUvb();

tcaselect(5)
  logfile.print(", ");
  logfile.print(String(UVa1));
  logfile.print(", ");
  logfile.print(String(UVb1));
  logfile.print(", ");
tcaselect(6) //switches inputs on the mux
  logfile.print(String(UVa2));
  logfile.print(", ");
  logfile.print(String(UVb2));