             g1 = g1 + GYRO_X_OFFSET;
             g2 = g2 + GYRO_Y_OFFSET;
             g3 = g3 + GYRO_Z_OFFSET;
-            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;
+            gx_degps = g1*GYRO_RANGE_DEGPS*pow(2, n_gyro_range)/pow(2, 15);
+            gy_degps = g2*GYRO_RANGE_DEGPS*pow(2, n_gyro_range)/pow(2, 15);
+            gz_degps = g3*GYRO_RANGE_DEGPS*pow(2, n_gyro_range)/pow(2, 15);
 //  The gyros and accelerometers can in principle be calibrated in addition to any factory calibration but they are generally
 //  pretty accurate. You can check the accelerometer by making sure the reading is +1 g in the positive direction for each axis.
 //  The gyro should read zero for each axis when the sensor is at rest. Small or zero adjustment should be needed for these sensors.

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

      len = mavlink_msg_to_send_buffer(buf, &msg);
      Serial.write(buf, len);
      
      /*
      //static inline uint16_t mavlink_msg_highres_imu_pack(uint8_t system_id, uint8_t component_id, mavlink_message_t* msg,
      //						       uint64_t time_usec, float xacc, float yacc, float zacc, float xgyro, float ygyro, float zgyro, float xmag, float ymag, float zmag, float abs_pressure, float diff_pressure, float pressure_alt, float temperature, uint16_t fields_updated)
      mavlink_msg_highres_imu_pack(system_id, component_id, &msg, (uint64_t)time_boot_us, ax_g*9.81, ay_g*9.81, az_g*9.81, gx_degps*PI/180.0f, gy_degps*PI/180.0f, gz_degps*PI/180.0f, my, mx, mz, press, press, altitude, temperature, a1);
      len = mavlink_msg_to_send_buffer(buf, &msg);
      Serial.write(buf, len);
      
      //mavlink_msg_scaled_imu_pack(uint8_t system_id, uint8_t component_id, mavlink_message_t* msg,
      // uint32_t time_boot_ms, int16_t xacc, int16_t yacc, int16_t zacc, int16_t xgyro, int16_t ygyro, int16_t zgyro, int16_t xmag, int16_t ymag, int16_t zmag)
      mavlink_msg_raw_imu_pack(system_id, component_id, &msg, time_boot_ms, a1, a2, a3, g1, g2, g3, m1, m2, m3);
