Adjusting library to suit my need

Hello !

I'm using a MPU9250 as an accelerometer, gyroscope and magnetometer. But now, I would like to have 2 of them working at the same time.

So I know it is possible because the I2C adress is changing wheither I'm putting the pin AD0 on GND or VCC. (0x69 if HIGH, 0x68 if LOW).

But, I'm using this library, and in my code, to acquire data, I call a function named readSensor (or something like this).
If I put two of them, even if they have different adress, the result is that I won't know which sensor will be called by the function.
I'm not a big fan of modifying library (never done that before), but I don't see any other way to do it.
To be honest, I don't know what I should be changing and how I should do it.

If you could give me some guidance, it would be super cool !

Thanks for reading.

PS: Code currently used :

#include "MPU9250.h"



// an MPU9250 object with the MPU-9250 sensor on I2C bus 0 with address 0x68
MPU9250 IMU(Wire,0x68);
MPU9250 IMU(Wire,0x69);
int status;
float timer;
float a=0;

void setup() {
  // serial to display data
  Serial.begin(9600);

  
  while(!Serial) {}

  // start communication with IMU 
  status = IMU.begin();
  if (status < 0) {
    Serial.println("IMU initialization unsuccessful");
    Serial.println("Check IMU wiring or try cycling power");
    Serial.print("Status: ");
    Serial.println(status);
    while(1) {}
  }
}


void position()
{
  IMU.readSensor();
float accelx = 0; float accely =0;              // C.I
float speedx = 0; float speedy=0;
float displacementx=0; float displacementy =0;
float deltaTime = 1;

float avgAccelx = (accelx + IMU.getAccelX_mss() ) / 2;
float avgAccely = (accely + IMU.getAccelY_mss() ) / 2;

float newSpeedx = speedx + (avgAccelx  * deltaTime);
float newSpeedy = speedy + (avgAccely  * deltaTime);
float avgSpeedx = (speedx + newSpeedx) / 2;           //Moyennage pour les beaux yeux.
float avgSpeedy = (speedy + newSpeedy) / 2;

float newDisplacementx = displacementx + (avgSpeedx * deltaTime);
float newDisplacementy = displacementy + (avgSpeedy * deltaTime);

speedx = newSpeedx ;
speedy = newSpeedy ;
displacementx = newDisplacementx;
displacementy = newDisplacementy;


float positionx = newDisplacementx+displacementx;
float positiony = newDisplacementy+displacementy;
int intensity = analogRead(1);

 
  Serial.print(positionx);
  Serial.print("  ");
  Serial.print(positiony);
  Serial.print("  ");
  Serial.println(intensity);
}


void loop() {
  
  // read the senso
  // display the data
  
  position();
  delay(1000);
 
}

Start by giving the objects different names

MPU9250 IMU0(Wire,0x68);
MPU9250 IMU1(Wire,0x69);

Ohmyduddy:
Hello !

I'm using a MPU9250 as an accelerometer, gyroscope and magnetometer. But now, I would like to have 2 of them working at the same time.

So I know it is possible because the I2C adress is changing wheither I'm putting the pin AD0 on GND or VCC. (0x69 if HIGH, 0x68 if LOW).

But, I'm using this library, and in my code, to acquire data, I call a function named readSensor (or something like this).
If I put two of them, even if they have different adress, the result is that I won't know which sensor will be called by the function.
I'm not a big fan of modifying library (never done that before), but I don't see any other way to do it.
To be honest, I don't know what I should be changing and how I should do it.

If you could give me some guidance, it would be super cool !

Thanks for reading.

PS: Code currently used :

#include "MPU9250.h"

// an MPU9250 object with the MPU-9250 sensor on I2C bus 0 with address 0x68
MPU9250 IMU(Wire,0x68);
MPU9250 IMU(Wire,0x69);
int status;
float timer;
float a=0;

void setup() {
 // serial to display data
 Serial.begin(9600);

while(!Serial) {}

// start communication with IMU
 status = IMU.begin();
 if (status < 0) {
   Serial.println("IMU initialization unsuccessful");
   Serial.println("Check IMU wiring or try cycling power");
   Serial.print("Status: ");
   Serial.println(status);
   while(1) {}
 }
}

void position()
{
 IMU.readSensor();
float accelx = 0; float accely =0;              // C.I
float speedx = 0; float speedy=0;
float displacementx=0; float displacementy =0;
float deltaTime = 1;

float avgAccelx = (accelx + IMU.getAccelX_mss() ) / 2;
float avgAccely = (accely + IMU.getAccelY_mss() ) / 2;

float newSpeedx = speedx + (avgAccelx  * deltaTime);
float newSpeedy = speedy + (avgAccely  * deltaTime);
float avgSpeedx = (speedx + newSpeedx) / 2;           //Moyennage pour les beaux yeux.
float avgSpeedy = (speedy + newSpeedy) / 2;

float newDisplacementx = displacementx + (avgSpeedx * deltaTime);
float newDisplacementy = displacementy + (avgSpeedy * deltaTime);

speedx = newSpeedx ;
speedy = newSpeedy ;
displacementx = newDisplacementx;
displacementy = newDisplacementy;

float positionx = newDisplacementx+displacementx;
float positiony = newDisplacementy+displacementy;
int intensity = analogRead(1);

Serial.print(positionx);
 Serial.print("  ");
 Serial.print(positiony);
 Serial.print("  ");
 Serial.println(intensity);
}

void loop() {
 
 // read the senso
 // display the data
 
 position();
 delay(1000);

}

as stated already just give them different names

MPU9250 IMUA(Wire,0x68);
MPU9250 IMUB(Wire,0x69);
int statusa;
int statusb;
float timer;
float a=0;

void setup() {
  // serial to display data
  Serial.begin(9600);

  
  while(!Serial) {}

  // start communication with IMU 
  statusa = IMUA.begin();
  statusb = IMUB.begin();
  if (statusa < 0) {
    Serial.println("IMU A initialization unsuccessful");
    Serial.println("Check IMU A wiring or try cycling power");
    Serial.print("Status A: ");
    Serial.println(statusa);
    while(1) {}
  }
  if (statusb < 0) {
    Serial.println("IMU B initialization unsuccessful");
    Serial.println("Check IMU B wiring or try cycling power");
    Serial.print("Status B: ");
    Serial.println(statusb);
    while(1) {}
  }
  
}

I'm not going to re-write it all but the above should get you going down the right path.