need some help with my project

my project consists of two servos ,a distance sensor and a prossessing code the servos are ment to goto mouseX and mouseY which they do then the distance sensor is ment to send the distance to the prossessing and it creates a box at mouseX and mouseY and it coulor depends on how
far the distance is but it is geting weird distances which repeat so i am unsure of the problem

processing

import processing.serial.*;
int Y;
int X;
Serial myPort;  // Create object from Serial class
int D;
int C;
void setup() 
{
  size(660, 660);
  String portName = Serial.list()[8];
  myPort = new Serial(this, portName, 9600);
  background(255);    // Setting the background to white          // Setting the outline (stroke) to black     
}

void draw() {
  delay(100);
X=mouseX/8;
myPort.write(X);
delay(1);
Y=(mouseY/8)+91;
myPort.write(Y);
delay(1);
myPort.write(183);
delay(1);
  if ( myPort.available() > 0) {  // If data is available,
    D = myPort.read();         // read it and store it in val
  }
println(D);
C = D*2;
stroke(C);
fill(C);
rect(mouseX,mouseY,6,6);

}

ardiuno

#include <Servo.h> 
int pingPin = 7; 
Servo x;
Servo y;
int X = 0;
int Y = 0;
int a = 0;
void setup()
{ 
Serial.begin(9600);
 x.attach(10);
 y.attach(9);
}
void loop()  
{
  if ( Serial.available() > 0 )
  {
a = Serial.read();
if (a<=90){
X=a;
x.write(X*2);}
} 
if (a>=91 & a<182){
  Y=a-91;
  y.write(Y*2);
}




  // establish variables for duration of the ping, 
  // and the distance result in inches and centimeters:
  long duration, inches, cm;

  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
  Serial.print(cm);

  
  delay(500);



}

long microsecondsToInches(long microseconds)
{
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}

Cross posting makes people cross.

I tried this once, it puzzled me for a long time. The solution is simple, I think, you just have to power the servo motors from a seperate battery than the Arduino. This is becasue the pulseIn() command, for some weird reason, gets messed up when the Arduino is powering servo's.

And remember to connect the ground of the servo power source to the arduino ground!

Hope that helps!
~Bob