Can you please help me correctly use I2C protocol with 3 sensors??

Hi all,

I'm not sure if anyone will be able to help me but I am having quite a bit of trouble with my code when I try to verify. I am quite new to programming so I have based this code of examples from the relevant libraries and then added my own code to try and get 3 sensors communicating via I2C and outputting information to the Serial Monitor.

I start getting issues when the code gets to the "float read_data(int address)" section, does anyone have any thoughts on how I could fix this?

Thanks alot!

#include <Wire.h>
#include <SparkFun_VL53L1X.h>
#include <SparkFun_Ublox_Arduino_Library.h>
#include <SparkFunMPU9250-DMP.h>

#ifdef defined(SAMD)  //conversion from SAMD to ESP32
 #define SerialPort SerialUSB
#else
 #define SerialPort Serial
#endif

SFEVL53L1X distanceSensor;
MPU9250_DMP imu;
SFE_UBLOX_GPS myGPS;

float lastTime = 0;
int i = 0;
float dataAngle();
float dataDistance();
float dataLatitude();
float dataLongitude();

void setup () {
  
  Wire.begin();
  Serial.begin(115200);
  SerialPort.begin(115200);
  
  if (imu.begin() == false)
  {
      SerialPort.println("Unable to communicate with MPU-9250");
  }
  if (distanceSensor.begin() == false)
  {
    Serial.println("Unable to communicate with VL53L1X");
  }
  if (myGPS.begin() == false)
  {
    Serial.println(F("Unable to communicate with UBLOX GPS"));
    while (1);
  }
  
  imu.dmpBegin(DMP_FEATURE_6X_LP_QUAT | DMP_FEATURE_GYRO_CAL, 10); 
  // Use gyro calibration // Enable 6-axis quat // Set DMP FIFO rate to 10 Hz
  myGPS.setI2COutput(COM_TYPE_UBX); //Set the I2C port to output UBX only (turn off NMEA noise)
  myGPS.saveConfiguration(); //Save the current settings to flash and BBR
  distanceSensor.setDistanceModeShort();
  //distanceSensor.setDistanceModeLong();
  
}


void loop () {

  float angle = read_data(0x68);
  float distance = read_data(0x29);
  float longitude = read_data(0x42);
  float latitude = read_data(0x42);
  Serial.println("Angle: ");
  Serial.print(angle);
  Serial.print(", Distance: ");
  Serial.print(distance);
  Serial.print(", Longitude: ");
  Serial.print(longitude);
  Serial.print(", Latitude: ");
  Serial.print(latitude);
  
}

float read_data(int address) {
  //start the communication with IC with the address xx
  Wire.beginTransmission(address); 
  switch (i) {
  case 0: Wire.write(dataAngle);
    break;
  case 1: Wire.write(dataDistance);
    break;
  case 2: Wire.write(dataLongitude);
    break;
  case 3: Wire.write(dataLatitude);
    break;
  }
  //end transmission
  Wire.endTransmission();
  //request float from address xx
  Wire.requestFrom(address, 4);
  //wait for response
  while(Wire.available() == 0);
  //put the temperature in variable data
  float data = Wire.read();  
  i++;
  if (i>3) {
    i = 0;
  } 
  return data;
}

float dataAngle () {  
  if ( imu.fifoAvailable() ) {
    if ( imu.dmpUpdateFifo() == INV_SUCCESS) {
      imu.computeEulerAngles();
      float angle = imu.roll;
      return angle;
    }
  }
}

float dataDistance () {
  distanceSensor.startRanging();
  float distance = distanceSensor.getDistance();
  distanceSensor.stopRanging();
  return distance;
}

float dataLongitude () {
  float longitude = myGPS.getLongitude();
  return longitude;
}

float dataLatitude () {
  float latitude = myGPS.getLatitude();
  return latitude;
}

Don't you think it would be helpful if you shared the exact and complete (not you're paraphrased version) error messages?

What kind of trouble? If there is an error, post the entire error message. Do not paraphrase. That leaves out important information. If no errors, describe what the code actually does and how that differs from what you want.

Karma for code tags on your first post. It is rare that new members read the forum guidelines before posting.

Apologies, the full error message was "In function 'float read_data(int)':

FeedReaderProto_v1:77:31: error: no matching function for call to 'write(float (&)())'

case 0: Wire.write(dataAngle);"

When using the Wire library, there is no overload of the write() function that take a float-type argument. Only integer types are allowed. In fact, any integer type other than uint_8 is first cast to a uint8_t anyway.