Enough of the background, My problem is that it seems like I am getting the absolute value not the real value. Am I doing something wrong when I am combining the bytes into an int or is it something else. Below is my pyhton code and below that is the code for the arduino.
Code: Select all
def get_data(self):
xmsb = self._read_data(self.REG_OUT_X_MSB)
xlsb = self._read_data(self.REG_OUT_X_LSB)
ymsb = self._read_data(self.REG_OUT_Y_MSB)
ylsb = self._read_data(self.REG_OUT_Y_LSB)
zmsb = self._read_data(self.REG_OUT_Z_MSB)
zlsb = self._read_data(self.REG_OUT_Z_LSB)
x= int_from_bytes(xmsb, xlsb)
y= int_from_bytes(ymsb, ylsb)
z= int_from_bytes(zmsb, zlsb)
return [x,y,z]
Code: Select all
void Adafruit_MMA8451::read(void) {
// read x y z at once
Wire.beginTransmission(_i2caddr);
i2cwrite(MMA8451_REG_OUT_X_MSB);
Wire.endTransmission(false); // MMA8451 + friends uses repeated start!!
Wire.requestFrom(_i2caddr, 6);
x = Wire.read(); x <<= 8; x |= Wire.read(); x >>= 2;
y = Wire.read(); y <<= 8; y |= Wire.read(); y >>= 2;
z = Wire.read(); z <<= 8; z |= Wire.read(); z >>= 2;
uint8_t range = getRange();
uint16_t divider = 1;
if (range == MMA8451_RANGE_8_G) divider = 1024;
if (range == MMA8451_RANGE_4_G) divider = 2048;
if (range == MMA8451_RANGE_2_G) divider = 4096;
x_g = (float)x / divider;
y_g = (float)y / divider;
z_g = (float)z / divider;
}