Hi im fairly new to Arduino and am using the MPU-6050 for the "time machine glove" project. I need to be in full understanding of the code but im stuck at the math part of the MPU where the angles and acceleration is calculated. I also do not understand why the data is being shifted 8 bits to the left? Could someone please enlighten me on this as i am very interested in understanding that particular part?
The first code is the bit shifting and the second is the math. Any sources on this topic are also appreciated.
Thanks
void measure() {
uint8_t* data = i2cRead(0x3B, 14);
accX = ((data[0] << 8) | data[1]);
accY = ((data[2] << 8) | data[3]);
accZ = ((data[4] << 8) | data[5]);
tempRaw = ((data[6] << 8) | data[7]);
gyroX = ((data[8] << 8) | data[9]);
gyroY = ((data[10] << 8) | data[11]);
gyroZ = ((data[12] << 8) | data[13]);
accZangle = (atan2(accX, accY) + PI) * RAD_TO_DEG;
accXangle = (atan2(accY, accX) + PI) * RAD_TO_DEG;
double gyroXrate = (double)gyroX / 131.0;
double gyroZrate = -((double)gyroZ / 131.0);
gyroXangle += kalmanX.getRate() * ((double)(micros() - timer) / 1000000);
gyroZangle += kalmanZ.getRate() * ((double)(micros() - timer) / 1000000);
kalAngleX = kalmanX.getAngle(accXangle, gyroXrate, (double)(micros() - timer) / 1000000);
kalAngleZ = kalmanZ.getAngle(accZangle, gyroZrate, (double)(micros() - timer) / 1000000);
timer = micros();
}
data is coming in as a stream of bytes. accX = ((data[0]<<8) | data[1])
combines those two bytes into one 16bit number
etc.
@OP
1. The execution of the following instruction has saved 14-byte data into locations data[0], data[1], ..., data[13] of the array pointed by the pointer variable data. (data type uint8_t refers to 8-bit data which also byte.)
uint8_t *data = i2cRead(0x3B, 14);
2. Now, data[0] location contains 8-bit data; it is the lower 8-bit part of your accX variable which is a 16-bit variable. data[1] location also contains 8-bit data; it is the upper 8-bit part of your accX variable. The following depicts the mapping of the data bytes in the memory (Fig-1).

Fig-1:
3. Now, you need to combine data[0] and data[1] in such a way so that the resultant data item becomes a 16-bit value as shown below (Fig-2). The data item can now be saved in the variable accX.

Fig-2:
4. How to bring data[1] to the left of data[0] as is shown in Fig-2 of Step-3?
(1) Let us first shift data[1] to the left by 8-bit positions; as a result, data[1] will appear as follows (Fig-3): (Shifting to the left by 8-bit positions is done by this process: data[1]<<8;.)

Fig-3:
(2) Now, you may say that the data[1] is 8-bit; when it will be shifted to the left by 8-bit position, it will be lost. In order to prevent this event from being happened, let us tell the compiler to extend the storage (processing buffer) area for data[1] to 16-bit. This is known as casting, and it is done by the following code.
int x1 = (int)data[1]<<8; //extend storage area, then shift and then save in x1
(3) Now, make an OR operation between data item of Fig-3 and data[0] of Fig-1 to get 16-bit data item.
int data16 = (int)data[1]<<8 | (int)data[0]; //data[0]'s storage area is also expanded to 16-bit.
accX = data16;


