Using IMU Readings to Control Servo

I have a Sparkfun 9 DOF Razor IMU that I am using as a compass. I can read the sensor on an Arduino Uno without any issues. But I want to be able to use the reading to control a Servo motor. But whenever I try to write to the motor while reading from the sensor, the sensor stops reading properly.

The IMU is attached to the RX and TX pins. The servo is attached to the Digital 9 pin.

At the moment, the sensor is just writing the yaw to serial. So I get a simple output in the Serial Monitor (values from -180 to 180). The code for what I have is below:

#include <Servo.h>

char data;

Servo motor;

void setup()

{ 
  Serial.begin(57600);
  motor.attach(7);
}

void loop()
{
  while (Serial.available() > 0) 
  {
    data = Serial.read();
    Serial.write(data);

   // Want to implement motor code here
   // motor.write(data);
   // delay(15);
   // But this doesn't work and messes up my sensor readings
  }
}

Once I try to implement the motor.write, the sensor readings begin to have random characters in them. This then doesn't write anything to the motor.

Is this a power issue or am I doing something wrong with my code? Let me know if you need any more information. Thank you!

Probably a power issue.
The Arduino can not supply the (start) current for the servo.
Is is powered with the USB bus ?
You need a seperate power supply for the servo.

Yes, the Arduino is powered with the USB bus. But it should be noted that the servo has a separate 12 V supply. I'm guessing that the Arduino can't handle the 3.3 V IMU and the 5 V servo signal.

Alright, a little update. I still haven't gotten this to work. Right now I have the sensor writing to serial and then I am reading from that to get the yaw values. It is reading properly because when I print to serial, it prints correctly.

So my code for the Servo is set to simply go one direction if the value is negative and the other if the value is positive. But for some reason, this is not working. I am getting no movement from the Servo (only at the start does it adjust somewhere).

Why isn't this code working? I'm pretty sure it makes sense. Any help would be greatly appreciated. Thanks!

#include <Servo.h>

Servo motor;

char data;
int i = 0;

void setup()

{ 
  Serial.begin(9600);
  motor.attach(9);
}

void loop()
{
  while (Serial.available() > 0) 
  {
    data = Serial.read();
    Serial.print(data);
    
  if(data < 0)
  {
    i--;
    if(i<0)
    {
      i = 0;
    }
    motor.write(i);
    //delay(10);
  }
  
  if(data >= 0)
  {
    i++;
    if(i>180)
    {
      i = 180;
    }
    motor.write(i);
    //delay(10);
  }
  }
}

What are you sending ? Do you type something in the serial monitor ?

Your sketch used the binary 8-bit signed character that is received. If you would type "3" and transmit that with carriage return and line feed, the sketch receives 51, 13, 10

What do you want to transfer via the serial port, and how do you want to use that ?
Perhaps you want to use this: Serial.parseInt() - Arduino Reference