Wire.h and analogWrite()

Hello, I’m controlling a three phasic motor using a DRV10983 controller (Just the chip, not the evaluation board). I want to control it by serial communication with Labview while I am reading to the gyro and others sensors.
My Problems is that I loose Serial communication when I call wire.h

If the next lines are comment there is no problem in the code:

//Get the gyro data
Wire.beginTransmission(MPU);
Wire.write(0x43);
Wire.endTransmission();
Wire.requestFrom(MPU,6,true); // read 6 continuos registers
GyX=Wire.read()<<8|Wire.read();
GyY=Wire.read()<<8|Wire.read();
GyZ=Wire.read()<<8|Wire.read();

(reading the gyro alone works perfectly is just a matter of doing it while I try to control the PWM of the motor controller)

The PWM of the DRV controllers works form 1kHz so I set my timer 2 to 3.9kHz by

TCCR2B = TCCR2B & B11111000 | B00000010;

But even changing to the other timers pins 9 and 6 I lost serial communication if want to read from the sensors.

Also, the code works if I don’t try to write to the PWM:

analogWrite(DRV,val);
What actually it does and spins the motor, just that I loose serial communication and cannot control it anymore

Anyone who had faces something similar…

You haven't given enough to go on. All I can say is that you did something wrong. We don't like trying to debug snippets. Post a complete program that demonstrates the problem.

My Problems is that I loose Serial communication when I call wire.h

You do NOT call header files. You call functions. Which function are you calling that you think is responsible for "loose Serial communication"?

sorry for my unclear post, this is my code:

#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_ADXL345_U.h>
//gyro

//Gyro Data
#define MPU 0x68
int16_t GyX, GyY, GyZ;
//Accelerometer
float Ax,Ay,Az;
float x, y, z;
int Motor = 3;     // Connect the Motor PWM output signal control line to 3
int val=0 // PWM value
//
int sampleTime=50;
double timestep=0;
char command=0;


void setup() {
TCCR2B = TCCR2B & B11111000 | B00000010;
Wire.begin();
Serial.begin(9600);          // start serial communication at 9600bps
}

void loop(void) 
{

analogWrite(motor,val);


if(millis() - timestep > sampleTime);


timestep = millis(); // update the time to get the next sample

/* Get a new sensor event */ 
sensors_event_t event; 
accel.getEvent(&event);

Get the gyro data
Wire.beginTransmission(MPU);
Wire.write(0x43);
Wire.endTransmission();
Wire.requestFrom(MPU,6,true); // read 6 continuous registers 
GyX=Wire.read()<<8|Wire.read();
GyY=Wire.read()<<8|Wire.read();
GyZ=Wire.read()<<8|Wire.read();
  //scale factor
Ax=event.acceleration.x;
Ay=event.acceleration.y;
Az=event.acceleration.z;



    

//To labview data
 command = Serial.read();
switch(command){
 case '1': Serial.println(GyX);
            delay(10);
   break;
 case '2': Serial.println(GyY);
            delay(10);

   break;
 case '3': Serial.println(GyZ);
           delay(10);
   break;
 case '4': Serial.println(Ax);  b //Print the Value of the sensors on labview
           delay(10);
    break;
 case '5': Serial.println(Ay);
           delay(10);
     break;
 case '6': Serial.println(Az);
           delay(10);
     break;
 case '7': 
            val=50;           //one push bottom control on/off of the motor at a fix PWM of 50
     break;
  case '8': 
            val=0;
      break;     
 default: break;

}



}

}

Please use code tags when posting code.

Please use the auto-format tool in Arduino before posting the code again. It makes it so much easier to read the code. It's very good. If it seems to have scrambled your code then the code was wrong to begin with.

Which function are you calling that you think is responsible for "loose Serial communication"?

My problem is when I have the wire. functions on the code and call analogwrite I lost the serial communications. If I remove the wire. functions out of the code there is no problem. If I Keep the wire functions and don't use the analogwrite, there is no problem. So the problem comes when both functions work together.

if(millis() - timestep > sampleTime);

Oops.

Oops.

the Sample time definition using the millis() and the if function works fine.

as I Said before the problem is with the wire functions and analogwrite

That if statement most definitely does NOT work right. The code that comes after it will be run unconditionally. The program would act the same if that line was completely removed.

That if statement most definitely does NOT work right. The code that comes after it will be run unconditionally. The program would act the same if that line was completely removed.

it would be nice if you could explain why.

int sampleTime=50;
double timestep=0;
void loop(void) 
{
  if(millis() - timestep > sampleTime);
{
 timestep = millis(); // update the time to get the next sample
 //Other Part of the code
} 
}

What I believe this if does is to run the get into the program once. Updated the timestep and compare in the next cycle if the sampleTime has been completed before getting once again in the program.

it would be nice if you could explain why.

You are stuck in trap #13

http://www.gammon.com.au/tips

This may or may not be the cause of your problem, but very few folks will think very hard about it, until obvious bugs are eliminated.