Hi, I am trying to receive data from an IMU sensor and I want it in a form of an array
void MPU(int i,float* data[7] )
{
tcaselect(i);
Serial.print("MPU0: ");
Wire.beginTransmission(MPU_addr[i]);
Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H)
Wire.endTransmission(false);
Wire.requestFrom(MPU_addr[i],14,true);
AcX=Wire.read()<<8|Wire.read(); // Example value 100
AcY=Wire.read()<<8|Wire.read(); // Example value 200
AcZ=Wire.read()<<8|Wire.read(); // Example value 300
Tmp=Wire.read()<<8|Wire.read(); // Example value 400
GyX=Wire.read()<<8|Wire.read(); // Example value 500
GyY=Wire.read()<<8|Wire.read(); // Example value 600
GyZ=Wire.read()<<8|Wire.read(); // Example value 700
data[7]= {AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ};
}
void loop(){
float data[7]
for (int i = 0; i <= 7; i++) {
test= MPU(i,data);
Serial.println("MPU " i": data 2: " data[2] "data 3: "data[3]);
}
}
a conventional approach for returning a large or variable amount of data from a sub-function is to pass a pointer to an array and its size to the sub-function which fills it in and usually returns the amount of data captured. the calling function then processes array it passed to the sub-function
Thank you everyone for getting back to me, I managed to get my code to work, if you have any suggestions do let me know.
p.s serial print is just there for a short while I will remove it as I won't read serial data anymore, atm I working on Arduino Uno and will eventually move to ESP32.
Here's the rough code for anyone who may want it
int Data[7]={1,2,3,4,5,6,7};
void MPU( int i, int Data[7] )
{
tcaselect(i);
Wire.beginTransmission(MPU_addr[i]);
Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H)
Wire.endTransmission(false);
Wire.requestFrom(MPU_addr[i],14,true); // request a total of 14 registers
Data[1]=Wire.read()<<8|Wire.read(); // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)
Data[2]=Wire.read()<<8|Wire.read(); // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
Data[3]=Wire.read()<<8|Wire.read(); // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
Data[4]=Wire.read()<<8|Wire.read(); // 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L)
Data[5]=Wire.read()<<8|Wire.read(); // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
Data[6]=Wire.read()<<8|Wire.read(); // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
Data[7]=Wire.read()<<8|Wire.read(); // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)
}
void loop(){
for (int i = 0; i <= 7; i++) {
MPU(i,Data);
Serial.print("MPU: ");
Serial.print(i);
// Serial.print(" | AcX = "); Serial.print(Data[1]);
// Serial.print(" | AcY = "); Serial.print(Data[2]);
// Serial.print(" | AcZ = "); Serial.print(Data[3]);
Serial.print(" | Tmp = "); Serial.print(Data[4]/340.00+36.53); //equation for temperature in degrees C from datasheet
// Serial.print(" | GyX = "); Serial.print(Data[5]);
// Serial.print(" | GyY = "); Serial.print(Data[6]);
// Serial.print(" | GyZ = "); Serial.print(Data[7]);
Serial.println();
}