Wire.requestForm() Is Disabling my Motor Speed Control

Hello. I’m implementing the below MPU-6050 code within my ino file, which also includes my DC motor control logic.

I have pinpointed the specific instruction that is disabling my motor speed control. The motor speed control works fine until I try to use “Wire.requestForm(mpuAddress, 14, true)”

I tried implementing a timer where the Wire.requestForm is not executed until specific time period elapses but this is not helping. It only serves to disable speed control as well.

Any help with how to get around this will be very helpful. I have also provided the complete ino file as an attachment.

//Written by Ahmet Burkay KIRNIK
//TR_CapaFenLisesi
//Measure Angle with a MPU-6050(GY-521)

#include<Wire.h>

const int MPU_addr=0x68;
int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ;

int minVal=265;
int maxVal=402;

double x;
double y;
double z;

void setup(){
Wire.begin();
Wire.beginTransmission(MPU_addr);
Wire.write(0x6B);
Wire.write(0);
Wire.endTransmission(true);
Serial.begin(9600);
}
void loop(){
Wire.beginTransmission(MPU_addr);
Wire.write(0x3B);
Wire.endTransmission(false);
Wire.requestFrom(MPU_addr,14,true);
AcX=Wire.read()<<8|Wire.read();
AcY=Wire.read()<<8|Wire.read();
AcZ=Wire.read()<<8|Wire.read();
int xAng = map(AcX,minVal,maxVal,-90,90);
int yAng = map(AcY,minVal,maxVal,-90,90);
int zAng = map(AcZ,minVal,maxVal,-90,90);

x= RAD_TO_DEG * (atan2(-yAng, -zAng)+PI);
y= RAD_TO_DEG * (atan2(-xAng, -zAng)+PI);
z= RAD_TO_DEG * (atan2(-yAng, -xAng)+PI);

Serial.print("AngleX= ");
Serial.println(x);

Serial.print("AngleY= ");
Serial.println(y);

Serial.print(“AngleZ= “);
Serial.println(z);
Serial.println(”-----------------------------------------”);
delay(400);
}

sketch_DCStepperMotorControl_2Apr2017.ino (6.03 KB)

why request 14 bytes and read 6 ?

  AcX=Wire.read()<<8|Wire.read();

You cannot assume those two reads will happen left-to-right, order of evaluation is not defined
as such in expressions.

Wire.read() returns (signed) int unpacked from a byte array, so you cannot just or it in like that:

Use:

  AcX = Wire.read () << 8 ;
  AcX |= Wire.read () & 0xFF ; // strip off sign-extension bits