analogWrite() problem

I use an ITG3200 gyroscope sensor , Turnigy 9X transmitter,4 brushless motors and arduino Mega 2560. I found a code used to get readings from the gyro and it works fine. I used the analogwrite function to write the value coming from receivers to motors and it works fine too. but when these two functions are put together in the same code (i.e reading from gyro and analogwrite() function) everything stop working, the serial monitor stops monitoring the gyro readings and the motors are no longer receiving values from TX and they are stuck to the last speed they received till I disconnect. If I remove any of those functions from void loop() the other one works fine with no problems. I thought the problem might be with the itgaddress which is 0x69 (may be conflict occurs or something) cause I’ve been trying everything else for 3 days but in vain. Does anyone know what the problem could be or if there is a way to change the address of the gyroscope?

Here is the code I use for testing where both functions are put toghether:

#include <Wire.h>
#include <Servo.h> 

//Tx definations
int throttle = 11;
int val  = 0;
unsigned long duration;


//defining the Offsets of x,y,z
#define OFFSET_X 18;
#define OFFSET_Y 242;
#define OFFSET_Z 8;
//

//This is a list of registers in the ITG-3200. Registers are parameters that determine how the sensor will behave, or they can hold data that represent the
//sensors current status.
//To learn more about the registers on the ITG-3200, download and read the datasheet.
char WHO_AM_I = 0x00;
char SMPLRT_DIV= 0x15;
char DLPF_FS = 0x16;
char GYRO_XOUT_H = 0x1D;
char GYRO_XOUT_L = 0x1E;
char GYRO_YOUT_H = 0x1F;
char GYRO_YOUT_L = 0x20;
char GYRO_ZOUT_H = 0x21;
char GYRO_ZOUT_L = 0x22;

//definitions of the equation that converts to deg/s
float Vref = 3.3;
float sensgyro = .0007238;


//This is a list of settings that can be loaded into the registers.
//DLPF, Full Scale Register Bits
//FS_SEL must be set to 3 for proper operation
//Set DLPF_CFG to 3 for 1kHz Fint and 42 Hz Low Pass Filter
char DLPF_CFG_0 = (1<<0);
char DLPF_CFG_1 = (1<<1);
char DLPF_CFG_2 = (1<<2);
char DLPF_FS_SEL_0 = (1<<3);
char DLPF_FS_SEL_1 = (1<<4);

//I2C devices each have an address. The address is defined in the datasheet for the device. The ITG-3200 breakout board can have different address depending on how
//the jumper on top of the board is configured. By default, the jumper is connected to the VDD pin. When the jumper is connected to the VDD pin the I2C address
//is 0x69.
char itgAddress = 0x69;

//In the setup section of the sketch the serial port will be configured, the i2c communication will be initialized, and the itg-3200 will be configured.
void setup()
{
  //Create a serial connection using a 9600bps baud rate.
  Serial.begin(115200);
  
  //Initialize the I2C communication. This will set the Arduino up as the 'Master' device.
  Wire.begin();
  
  //Read the WHO_AM_I register and print the result
  char id=0; 
  id = itgRead(itgAddress, 0x00);  
  Serial.print("ID: ");
  Serial.println(id, HEX);
  
  //Configure the gyroscope
  //Set the gyroscope scale for the outputs to +/-2000 degrees per second
  itgWrite(itgAddress, DLPF_FS, (DLPF_FS_SEL_0|DLPF_FS_SEL_1|DLPF_CFG_0));
  //Set the sample rate to 100 hz
  itgWrite(itgAddress, SMPLRT_DIV, 9);
  pinMode(throttle, INPUT); 
}

//The loop section of the sketch will read the X,Y and Z output rates from the gyroscope and output them in the Serial Terminal
void loop()
{
  //Create variables to hold the output rates.
  int xRate, yRate, zRate;

  //Read the x,y and z output rates from the gyroscope.
  xRate = readX();
  yRate = readY();
  zRate = readZ();
  
  duration = pulseIn(throttle, HIGH); //Get the value of RX received from TX
  duration = map(duration,1000,2000,100,200); 
  analogWrite(9,duration); //write value from receiver on motor1 connected to pin9
  analogWrite(10,duration); 
  
  //calculate the total time of the process

  //Print the output rates to the terminal, seperated by a TAB character.
  Serial.print(xRate);
  Serial.print('\t');
  Serial.print(yRate);
  Serial.print('\t');
  Serial.println(zRate);

  //Wait 10ms before reading the values again. (Remember, the output rate was set to 100hz and 1reading per 10ms = 100hz.)
  delay(0);
}

//This function will write a value to a register on the itg-3200.
//Parameters:
//  char address: The I2C address of the sensor. For the ITG-3200 breakout the address is 0x69.
//  char registerAddress: The address of the register on the sensor that should be written to.
//  char data: The value to be written to the specified register.
void itgWrite(char address, char registerAddress, char data)
{
  //Initiate a communication sequence with the desired i2c device
  Wire.beginTransmission(address);
  //Tell the I2C address which register we are writing to
  Wire.write(registerAddress);
  //Send the value to write to the specified register
  Wire.write(data);
  //End the communication sequence
  Wire.endTransmission();
}

//This function will read the data from a specified register on the ITG-3200 and return the value.
//Parameters:
//  char address: The I2C address of the sensor. For the ITG-3200 breakout the address is 0x69.
//  char registerAddress: The address of the register on the sensor that should be read
//Return:
//  unsigned char: The value currently residing in the specified register
unsigned char itgRead(char address, char registerAddress)
{
  //This variable will hold the contents read from the i2c device.
  unsigned char data=0;
  
  //Send the register address to be read.
  Wire.beginTransmission(address);
  //Send the Register Address
  Wire.write(registerAddress);
  //End the communication sequence.
  Wire.endTransmission();
  
  //Ask the I2C device for data
  Wire.beginTransmission(address);
  Wire.requestFrom(address, 1);
  
  //Wait for a response from the I2C device
  if(Wire.available()){
    //Save the data sent from the I2C device
    data = Wire.read();
  }
  
  //End the communication sequence.
  Wire.endTransmission();
  
  //Return the data read during the operation
  return data;
}

//This function is used to read the X-Axis rate of the gyroscope. The function returns the ADC value from the Gyroscope
//NOTE: This value is NOT in degrees per second. 
//Usage: int xRate = readX();
int readX(void)
{
  int data=0;
  data = itgRead(itgAddress, GYRO_XOUT_H)<<8;
  data |= itgRead(itgAddress, GYRO_XOUT_L);
  data -= OFFSET_X;   
  float Ratex = (((data * Vref / 65535) - 0) / sensgyro);
  data = Ratex;
  return data;
}

//This function is used to read the Y-Axis rate of the gyroscope. The function returns the ADC value from the Gyroscope
//NOTE: This value is NOT in degrees per second. 
//Usage: int yRate = readY();
int readY(void)
{
  int data=0;
  data = itgRead(itgAddress, GYRO_YOUT_H)<<8;
  data |= itgRead(itgAddress, GYRO_YOUT_L);   
  data -= OFFSET_Y;   
  float Ratey = (((data * Vref / 65535) - 0) / sensgyro);
  data = Ratey;
  return data;
}

//This function is used to read the Z-Axis rate of the gyroscope. The function returns the ADC value from the Gyroscope
//NOTE: This value is NOT in degrees per second. 
//Usage: int zRate = readZ();
int readZ(void)
{
  int data=0;
  data = itgRead(itgAddress, GYRO_ZOUT_H)<<8;
  data |= itgRead(itgAddress, GYRO_ZOUT_L);  
  data -= OFFSET_Z;   
  float Ratez = (((data * Vref / 65535) - 0) / sensgyro);
  data = Ratez;
  return data;
}

Check what timer the servo library uses, check what timer is used to drive the PWM for pins 9 & 10 and I think you will find an overlap. As a cure use pins for PWM that do not use the servo's timer.

  duration = map(duration,1000,2000,100,200);

As opposed to just dividing by 10?

How do you KNOW that duration is in the range 1000 to 2000?

  //Wait 10ms before reading the values again. (Remember, the output rate was set to 100hz and 1reading per 10ms = 100hz.)
  delay(0);

If the comment doesn't match the code, does it serve any useful purpose? Calling delay() with a value of 0 certainly does not.

What do you Serial.print() statements tell you is happening?

Check what timer the servo library uses, check what timer is used to drive the PWM for pins 9 & 10 and I think you will find an overlap. As a cure use pins for PWM that do not use the servo's timer.

I'm a noob with that timers thing but for simplicity I connected only 2 motors to pins 7,8 and only one channel from receiver is connected to pin 5. So, I think I'm not using servo's timers

So, I think I'm not using servo's timers

If you don't have any servos, why are you including the Servo header file?

As opposed to just dividing by 10?

I just did that for simplicity but even changing that range problem still goes on

How do you KNOW that duration is in the range 1000 to 2000?

I got this from forums of people using the same type of this transmitter, with a value lower than 1000, receiver won’t get any signals.

If the comment doesn’t match the code, does it serve any useful purpose? Calling delay() with a value of 0 certainly does not.

No it didn’t solve anything, I just forgot to modfy it back.

What do you Serial.print() statements tell you is happening?

They print out angular velocities around X,Y and Z. They work very fine without the analogWrite() function. I use an accelerometer to print ou angles but it doesn’t cause the same problem with the analogWrite function

If you don't have any servos, why are you including the Servo header file?

Because I was using servo functions before modifying them to analogWrite, can this make a problem?

Well as you don't appear to be actually using the servos at all why is it included at the start of the sketch?

There is no software reason why the two sketches should interfere with each other. Have you tried replacing the motors with an LED and resistor? It could be the interference from the motor is messing up the I2C bus.

Well as you don’t appear to be actually using the servos at all why is it included at the start of the sketch?

Yes you are right, I removed it.

There is no software reason why the two sketches should interfere with each other. Have you tried replacing the motors with an LED and resistor? It could be the interference from the motor is messing up the I2C bus.

Well, I don’t have leds right now, I’ll buy some 2day and try that but there is a thing that may mean something to you. When any statement that uses this itgaddress is put in the void loop, this problem occurs (for example, data = itgRead(itgAddress, GYRO_XOUT_H)<<8; //in the readX() function). I changed the itgaddress from 0x69 to 0x68 to see if the problem stills on but it went away but ofcourse all the gyro readings printed out are zeros cause it’s not the right address to write on. As I mentioned I’m using an accelerometer (the address is 0x40) too with the motors but it doesn’t cause this problem so, I think it’s not with the I2C bus. It’s something with that address, may be it gets something from both the gyro and receiver at the same time that causes this conflict, I’m just guessing.

But does this cause problems if the motor is disconnected?

But does this cause problems if the motor is disconnected?

Well, Motors are powered by an external battery and connected to arduino through electronic speed controllers. The problem only occurs when those ESCs are connected, it vanishes when I disconnect them but it doesn't make a difference if motors are powered or not.

Not sure but are you saying that with the same code you only get a problem if the ESC is connected to the arduino but no motor is connected to the ESC?

Not sure but are you saying that with the same code you only get a problem if the ESC is connected to the arduino but no motor is connected to the ESC?

First, Thanks for being so patient.I really appreciate it.

Second, I thought of making a videos showing the problem to make it more clear

watch?v=Im_qju60XWc&feature=youtu.be (on youtube)

I think the problem has been solved. the problem was something wrong with the calibration I made to my ESCs. I recalibrated them and the problem was gone. Thanks a lot Mike for your help.

Well you solved it. I only prodded you, but that is what I consider the best result from a forum like this. :)