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;
}