 //  compared to the gyro and accelerometer!
             if (mcount > 1000/MagRate) {  // this is a poor man's way of setting the magnetometer read rate (see below) 
             mpu.getMag  ( &m1, &m2, &m3 );
-            mx = m1*10.0f*1229.0f/4096.0f + MAG_X_OFFSET; // milliGauss (1229 microTesla per 2^12 bits, 10 mG per microTesla)
-            my = m2*10.0f*1229.0f/4096.0f + MAG_Y_OFFSET; // apply calibration offsets in mG that correspond to your environment and magnetometer
-            mz = m3*10.0f*1229.0f/4096.0f + MAG_Z_OFFSET;
+            m2 = -m2;
+            // Apply calibration offsets on raw measurements that correspond to your environment and magnetometer
+            m1 = m1 + MAG_X_OFFSET;
+            m2 = m2 + MAG_Y_OFFSET;
+            m3 = m3 + MAG_Z_OFFSET;
+            mx = m1*10.0f*1229.0f/4096.0f; // milliGauss (1229 microTesla per 2^12 bits, 10 mG per microTesla)
+            my = m2*10.0f*1229.0f/4096.0f; 
+            mz = m3*10.0f*1229.0f/4096.0f;
             mcount = 0;
             }           
          }

            mcount++;
           // read the raw sensor data
            mpu.getAcceleration  ( &a1, &a2, &a3  );
            ax_g = a1*2.0f/32768.0f; // 2 g full range for accelerometer
            ay_g = a2*2.0f/32768.0f;
            az_g = a3*2.0f/32768.0f;

            mpu.getRotation  ( &g1, &g2, &g3  );
            gx_degps = g1*250.0f/32768.0f; // 250 deg/s full range for gyroscope
            gy_degps = g2*250.0f/32768.0f;
            gz_degps = g3*250.0f/32768.0f;


// Define corrections/offsets for magnetometer
#define LOCAL_DECLINATION_DEG -3 //at Nantes, France is around 3 degrees in 2014
#define MAG_X_OFFSET 0.0f
#define MAG_Y_OFFSET 0.0f
#define MAG_Z_OFFSET 0.0f
#define ACC_X_OFFSET 0.0f
#define ACC_Y_OFFSET 0.0f
#define ACC_Z_OFFSET 0.0f
#define GYRO_X_OFFSET 0.0f
#define GYRO_Y_OFFSET 0.0f
#define GYRO_Z_OFFSET 0.0f

// Declare device MPU6050 class
MPU6050 mpu(0x69);
