Hot to get in use of the data from: Serial.print();?

Hi all.
The sketch print out the MPU9250 data well used:

 Serial.print(IMU.getAccelX_mss(), 6);
  Serial.print("\t");

got:

-6.785513
-5.162162
-3.663315
-2.356014

when I changed it as: float AcX = (IMU.getAccelX_mss(), 6); just got:
6.00
6.00
6.00
6.00
why and how can get the printed data in use?

Thanks
Adam

#include "MPU9250.h"

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

void setup() {
  // serial to display data
  Serial.begin(115200);
  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 loop() {
  // read the sensor
  IMU.readSensor();
  // display the data
  Serial.print(IMU.getAccelX_mss(), 6);
  Serial.print("\t");
  Serial.print(IMU.getAccelY_mss(), 6);
  Serial.print("\t");
  Serial.print(IMU.getAccelZ_mss(), 6);
  Serial.print("\t");
  Serial.print(IMU.getGyroX_rads(), 6);
  Serial.print("\t");
  Serial.print(IMU.getGyroY_rads(), 6);
  Serial.print("\t");
  Serial.print(IMU.getGyroZ_rads(), 6);
  Serial.print("\t");
  Serial.print(IMU.getMagX_uT(), 6);
  Serial.print("\t");
  Serial.print(IMU.getMagY_uT(), 6);
  Serial.print("\t");
  Serial.print(IMU.getMagZ_uT(), 6);
  Serial.print("\t");
  Serial.println(IMU.getTemperature_C(), 6);
  delay(100);
}

The second parameter when printing float values gives the number of digits after the decimal dot.

Although I'm not exactly sure what the comma separator does (lack of knowledge), based on your results AcX is assigned the value 6 and not the value of the getAccelX_mss().

Try

float AcX = IMU.getAccelX_mss();
Serial.print(AcX, 6);
1 Like

your unusual use of English makes it difficult to understand what you are asking

It looks to me like you are asking "I have measured this data, now what do I do with it?'

Great!
it works.

Do you understand what you did wrong?

2 Likes

Thanks.

I thought "(IMU.getAccelZ_mss(), 6)" works together, really the '6' is for printing.

1 Like

You got it :+1:

1 Like

Thanks.

and how to get: AcX = AcX1 + AcX2 with 6 decimal precision?

That's not how a computer works. You can truncate it to 6 decimal places when you display it. But internally the number is stored as floating point with whatever precision that implies.

1 Like

Thanks.
How can get 6 decimal precision result that needed in later calculation?

Just store it as a float. There is no this many or that many decimal places. Floating point on most 8 bit Arduino's has about 6-7 digits of precision.

If you're using a board that supports double then you can use double and have more precision.

You're thinking of math on paper where you only write so many places. That's not how a computer works.

yes, I guess some thing may wrong with my store? below are the strange results, the
AcXA[i] = IMU.getAccelY_mss(); stored data shown by Serial.print(); followed, but the Serial.print(); soon after shown different results, how can?

CASE A:

void loop() {
  IMU.readSensor();
  delay(10);



  

  //......................................
  for (int i = 0; i < 10; i++) {
    AcXA[i] = IMU.getAccelY_mss();
    Serial.print("L61 AcXA["); Serial.print(i); Serial.print("] =");
    Serial.println(AcXA[i]);

    AcYA[i] = IMU.getAccelY_mss();
    Serial.print("L65 AcYA["); Serial.print(i); Serial.print("] =");
    Serial.println(AcYA[i]);

    AcZA[i] = IMU.getAccelZ_mss();

    Serial.print("L70 AcZA["); Serial.print(i); Serial.print("] =");
    Serial.println(AcZA[i]);

 

    // print again:

    

    Serial.print("L82 AcXA["); Serial.print(i); Serial.print("] =");
    Serial.println(AcXA[i]);

    Serial.print("L85 AcYA["); Serial.print(i); Serial.print("] =");
    Serial.println(AcYA[i]);

    Serial.print("L88 AcZA["); Serial.print(i); Serial.print("] =");
    Serial.println(AcZA[i]);

    //.................................

  }
}

RESULTS A:

L61 AcXA[0] =-9.06
L65 AcYA[0] =-9.06
L70 AcZA[0] =-4.62
L82 AcXA[0] =-4.62
L85 AcYA[0] =-4.62
L88 AcZA[0] =-4.62
L61 AcXA[1] =-9.06
L65 AcYA[1] =-9.06
L70 AcZA[1] =-4.62
L82 AcXA[1] =-4.62
L85 AcYA[1] =-4.62
L88 AcZA[1] =-4.62
L61 AcXA[2] =-9.06
L65 AcYA[2] =-9.06
L70 AcZA[2] =-4.62
L82 AcXA[2] =-4.62
L85 AcYA[2] =-4.62
L88 AcZA[2] =-4.62
L61 AcXA[3] =-9.06
L65 AcYA[3] =-9.06
L70 AcZA[3] =-4.62
L82 AcXA[3] =-4.62
L85 AcYA[3] =-4.62
L88 AcZA[3] =-4.62
L61 AcXA[4] =-9.06
L65 AcYA[4] =-9.06
L70 AcZA[4] =-4.62
L82 AcXA[4] =-4.62
L85 AcYA[4] =-4.62
L88 AcZA[4] =-4.62
L61 AcXA[5] =-9.06
L65 AcYA[5] =-9.06
L70 AcZA[5] =-4.62
L82 AcXA[5] =-4.62
L85 AcYA[5] =-4.62
L88 AcZA[5] =-4.62
L61 AcXA[6] =-9.06
L65 AcYA[6] =-9.06
L70 AcZA[6] =-4.62
L82 AcXA[6] =-4.62
L85 AcYA[6] =-4.62
L88 AcZA[6] =-4.62
L61 AcXA[7] =-9.06
L65 AcYA[7] =-9.06
L70 AcZA[7] =-4.62
L82 AcXA[7] =-4.62
L85 AcYA[7] =-4.62
L88 AcZA[7] =-4.62
L61 AcXA[8] =-9.06
L65 AcYA[8] =-9.06
L70 AcZA[8] =-4.62
L82 AcXA[8] =-4.62
L85 AcYA[8] =-4.62
L88 AcZA[8] =-4.62
L61 AcXA[9] =-9.06
L65 AcYA[9] =-9.06
L70 AcZA[9] =-4.62
L82 AcXA[9] =-4.62
L85 AcYA[9] =-4.62
L88 AcZA[9] =-4.62

CASE B:

void loop() {
  IMU.readSensor();
  delay(10);



  

  //......................................
  for (int i = 0; i < 10; i++) {
    AcXA[i] = IMU.getAccelY_mss();
    Serial.print("L61 AcXA["); Serial.print(i); Serial.print("] =");
    Serial.println(AcXA[i]);

    AcYA[i] = IMU.getAccelY_mss();
    Serial.print("L65 AcYA["); Serial.print(i); Serial.print("] =");
    Serial.println(AcYA[i]);

    AcZA[i] = IMU.getAccelZ_mss();

    Serial.print("L70 AcZA["); Serial.print(i); Serial.print("] =");
    Serial.println(AcZA[i]);

    TmpA[i] = IMU.getTemperature_C();
    GyXA[i] = IMU.getGyroX_rads();
    GyYA[i] = IMU.getGyroX_rads();
    GyZA[i] = IMU.getGyroZ_rads();

    // print again:

    

    Serial.print("L82 AcXA["); Serial.print(i); Serial.print("] =");
    Serial.println(AcXA[i]);

    Serial.print("L85 AcYA["); Serial.print(i); Serial.print("] =");
    Serial.println(AcYA[i]);

    Serial.print("L88 AcZA["); Serial.print(i); Serial.print("] =");
    Serial.println(AcZA[i]);

    //.................................

  }
}

RESULTS B:

L61 AcXA[0] =-7.33
L65 AcYA[0] =-7.33
L70 AcZA[0] =-4.45
L82 AcXA[0] =0.00
L85 AcYA[0] =0.00
L88 AcZA[0] =0.00
L61 AcXA[1] =-7.33
L65 AcYA[1] =-7.33
L70 AcZA[1] =-4.45
L82 AcXA[1] =0.00
L85 AcYA[1] =0.00
L88 AcZA[1] =0.00
L61 AcXA[2] =-7.33
L65 AcYA[2] =-7.33
L70 AcZA[2] =-4.45
L82 AcXA[2] =0.00
L85 AcYA[2] =0.00
L88 AcZA[2] =0.00
L61 AcXA[3] =-7.33
L65 AcYA[3] =-7.33
L70 AcZA[3] =-4.45
L82 AcXA[3] =0.00
L85 AcYA[3] =0.00
L88 AcZA[3] =0.00
L61 AcXA[4] =-7.33
L65 AcYA[4] =-7.33
L70 AcZA[4] =-4.45
L82 AcXA[4] =0.00
L85 AcYA[4] =0.00
L88 AcZA[4] =0.00
L61 AcXA[5] =-7.33
L65 AcYA[5] =-7.33
L70 AcZA[5] =-4.45
L82 AcXA[5] =0.00
L85 AcYA[5] =0.00
L88 AcZA[5] =0.00
L61 AcXA[6] =-7.33
L65 AcYA[6] =-7.33
L70 AcZA[6] =-4.45
L82 AcXA[6] =0.00
L85 AcYA[6] =0.00
L88 AcZA[6] =0.00
L61 AcXA[7] =-7.33
L65 AcYA[7] =-7.33
L70 AcZA[7] =-4.45
L82 AcXA[7] =0.00
L85 AcYA[7] =0.00
L88 AcZA[7] =0.00
L61 AcXA[8] =-7.33
L65 AcYA[8] =-7.33
L70 AcZA[8] =-4.45
L82 AcXA[8] =0.00
L85 AcYA[8] =0.00
L88 AcZA[8] =0.00
L61 AcXA[9] =-7.33
L65 AcYA[9] =-7.33
L70 AcZA[9] =-4.45
L82 AcXA[9] =0.00
L85 AcYA[9] =0.00
L88 AcZA[9] =0.00

Post a complete code. I can't work from the snippets since I can't test. I'm not going to fill in the missing stuff myself. I want the same as you have.

1 Like

Thanks.
I'll find it.

You don't have the code? What???? Then what are you talking about that doesn't work? What did you come here for help with?

Actually, it looks like you had some code that demonstrated the problem and then edited it out. Why can't we look at that code?

1 Like

the all sketch can't find after many edits.
I attached another one and realized it's not same question and deleted.
Thanks.

It looked like the same question.

In that code you were getting one of the numbers printed. So your method of saving a number into a floating point variable is good. I think perhaps you were reading 0 from the sensor in that case.

1 Like

The new attached half same as the code before, the reading good, my asking in #12 is how to store the readings and reprint out without value lost.

do you have idea how to drive a line direction deviation swing like a digital compass, actually I'm trying to make digital compass by these readings of paw.

assign it to a float variable. If that's not working then there's an issue with the reading.

You can see in the code you deleted that this works.

1 Like

well.
let me continue on #12 that is about MPU9250 readings.

the same MPU9250 got different readings now.
tested by library: https://github.com/hideakitai/MPU9250/blob/master/MPU9250.h

#include "MPU9250.h"  /// MPU9250_A

MPU9250 mpu;

void setup() {
    Serial.begin(115200);
    Wire.begin();
    delay(2000);

    if (!mpu.setup(0x68)) {  // change to your own address
        while (1) {
            Serial.println("MPU connection failed. Please check your connection with `connection_check` example.");
            delay(5000);
        }
    }
}

void loop() {
    if (mpu.update()) {
        static uint32_t prev_ms = millis();
        if (millis() > prev_ms + 25) {
          //  print_roll_pitch_yaw();
          test_print();
            prev_ms = millis();
        }
    }
}

/* 
void print_roll_pitch_yaw() {
    Serial.print("Yaw, Pitch, Roll: ");
    Serial.print(mpu.getYaw(), 2);
    Serial.print(", ");
    Serial.print(mpu.getPitch(), 2);
    Serial.print(", ");
    Serial.println(mpu.getRoll(), 2);
}  */

void test_print() {

  float acX = mpu.getAccX();
  Serial.print("acX = ");
  Serial.println(acX);

  float acY = mpu.getAccX();
  Serial.print("acY = ");
  Serial.println(acY);

   float acZ = mpu.getAccX();
  Serial.print("acZ = ");
  Serial.println(acZ);
  

  float YawR = mpu.getYaw();
  Serial.print("YawR =");
  Serial.println(YawR);

//    float getAccX() const { return a[0]; }  // FROM .h
 // float AcX = IMU.getAccelX_mss(); // from: B6
} 

got:

YawR =-15.15
acX = -0.08
acY = -0.08
acZ = -0.08
YawR =-15.20
acX = -0.23
acY = -0.23
acZ = -0.23

used library: https://github.com/psychogenic/MPU9250/tree/master/src

#include "MPU9250.h"  /// MPU9250_B 
//MPU9250 IMU(Wire, 0x68);
MPU9250 mpu(Wire, 0x68);
//MPU9250 mpu;

int status;

void setup() {
  Serial.begin(115200);
  Wire.begin();
  delay(2000);

/*   if (!mpu.setup(0x68)) {  // change to your own address
    while (1) {
      Serial.println("MPU connection failed. Please check your connection with `connection_check` example.");
      delay(5000);
    }
  }  */

  status = mpu.begin();
}

void loop() {

    mpu.readSensor();
  delay(10);
  
//*  if (mpu.update()) {
    static uint32_t prev_ms = millis();
    if (millis() > prev_ms + 25) {
      //  print_roll_pitch_yaw();
      test_print();
      prev_ms = millis();
    }
//  }   */
}

/*
void print_roll_pitch_yaw() {
  Serial.print("Yaw, Pitch, Roll: ");
  Serial.print(mpu.getYaw(), 2);
  Serial.print(", ");
  Serial.print(mpu.getPitch(), 2);
  Serial.print(", ");
  Serial.println(mpu.getRoll(), 2);
}   */

void test_print() {

  float acX = mpu.getAccelX_mss();
  Serial.print("acX = ");
  Serial.println(acX);

  float acY = mpu.getAccelY_mss();
  Serial.print("acY = ");
  Serial.println(acY);

   float acZ = mpu.getAccelZ_mss();
  Serial.print("acZ = ");
  Serial.println(acZ);
  

//  float YawR = mpu.getYaw();
//  Serial.print("YawR =");
//  Serial.println(YawR);

  //    float getAccX() const { return a[0]; }  // FROM .h
  // float AcX = IMU.getAccelX_mss(); // from: B6
}

got:

MagX =-12.11
AcX = -6.18
AcY =-6.16
AcZ =-4.94
offset = 17
vectorM [4] =50
MagX =-12.11
AcX =-6.18
AcY =-6.16
AcZ =-4.94

is the library reason? or else?