PS/2 +Servo

Hello!
Im having troubles with controlling servos with ps/2 mouse. This is not rare question, but I havent found any isues with search on this forum or internet.
What do we have:
standart code example of ps/2 library + initialised 2 servos with standart Servo library

#include <ps2.h>
#include <Servo.h> 
/*
 * an arduino sketch to interface with a ps/2 mouse.
 * Also uses serial protocol to talk back to the host
 * and report what it finds.
 */

/*
 * Pin 5 is the mouse data pin, pin 6 is the clock pin
 * Feel free to use whatever pins are convenient.
 */
PS2 mouse(6, 5);

Servo Xservo;  // create servo object to control a servo 
Servo Yservo;
int Xval=0;
int Yval=0;

/*
 * initialize the mouse. Reset it, and place it into remote
 * mode, so we can get the encoder data on demand.
 */
void mouse_init()
{
  mouse.write(0xff);  // reset
  mouse.read();  // ack byte
  mouse.read();  // blank */
  mouse.read();  // blank */
  mouse.write(0xf0);  // remote mode
  mouse.read();  // ack
  delayMicroseconds(100);
}

void setup()
{
  Serial.begin(9600);
  mouse_init();
  Xservo.attach(10);
  Yservo.attach(9);
}

/*
 * get a reading from the mouse and report it back to the
 * host via the serial line.
 */
void loop()
{
  char mstat;
  char mx;
  char my;

  /* get a reading from the mouse */
  mouse.write(0xeb);  // give me data!
  mouse.read();      // ignore ack
  mstat = mouse.read();
  mx = mouse.read();
  my = mouse.read();

  /* send the data back up */
  Serial.print(mstat, BIN);
  Serial.print("\tX=");
  Serial.print(mx, DEC);
  Serial.print("\tY=");
  Serial.print(my, DEC);
  Serial.println();
  delay(20);  /* twiddle */
}

simple, isn`t it?
The problem is that conroller is hanging up after couple of cycles. Without servo - works fine.
what we have in com:

1000X=0Y=0
1000X=0Y=0
1000X=0Y=0
1000X=0Y=0
1000X=0Y=0
1000X=0Y=0
1000X=0Y=0
1000X=0Y=0
1000X=0Y=0
1000X=0Y=0
1000X=0Y=0
1000X=0Y=0
1000X=0Y=0
1000X=0Y=0
1000X=0Y=0
1000X=0Y=0
1000X=0Y=0
1000X=0Y=0
1000X=0Y=0
1000X=0Y=0
1000X=0Y=0
111000X=-1Y=-2
11000X=-4Y=2
11000X=-2Y=2
11000X=-4Y=5
1000X=0Y=4
1000X=2Y=2
1000X=4Y=2
1000X=3Y=0
101000X=3Y=-1
101000X=5Y=-3
101000X=4Y=-7
101000X=3Y=-8
101000X=0Y=-9
111000X=-4Y=-9
111000X=-3Y=-8
111000X=-5Y=-4
111000X=-4Y=-1
11000X=-8Y=3
11000X=-11Y=6
11000X=-11Y=8
11000X=-9Y=8
11000X=-1Y=6
1000X=2Y=5
1000X=4Y=4
1000X=6Y=3
1000X=7Y=2
1000X=6Y=1
1000X=3Y=0
101000X=5Y=-2
101000X=3Y=-4
101000X=3Y=-7
101000X=1Y=-7
111000X=-1Y=-5
111000X=-2Y=-1
11000X=-3Y=0
11000X=-3Y=2
11000X=-5Y=4
11000X=-4Y=7
11000X=-2Y=7
1000X=1Y=5
1000X=2Y=4
1000X=3Y=1
1000X=3Y=1
101000X=3Y=-1
101000X=4Y=-2
101000X=3Y=-3
101000X=2Y=-7
101000X=1Y=-7
111000X=-1Y=-5
111000X=-3Y=-3

(I moved mouse, so we have different values of X Y)
and thats all. controller goes in some “unlimited cycle” in ps/2 procedure.

Did anyone had such problems? Any ideas what to do?

The problem is that conroller is hanging up after couple of cycles.

How are the servos powered?

Servos arent even connected to arduino. I just made servo initialisation and attachment. Nothing else. Im using mega2560 board

So as of right now, without the servos connected, the code works or it doesn't work? It code be something in the libraries that are conflicting with each other. Post your .h and .cpp files for the PS2 mouse.

Code doesn`t work. As far as I understand, ps/2 Lib is somehow conflicting with servo lib. Without servo initialisation ps/2 works perfect. So as for servo.

ps2 lib is from here: http://playground.arduino.cc//ComponentLib/Ps2mouse
ps2.h

/*
 * ps2.h - a library to interface with ps2 devices. See comments in
 * ps2.cpp.
 * Written by Chris J. Kiick, January 2008.
 * Release into public domain.
 */

#ifndef ps2_h
#define ps2_h

#include "Arduino.h"

class PS2
{
	public:
		PS2(int clk, int data);
		void write(unsigned char data);
		unsigned char read(void);
	private:
		int _ps2clk;
		int _ps2data;
		void golo(int pin);
		void gohi(int pin);
};

#endif /* ps2_h */

ps/2.c:

/*
 * ps2.cpp - an interface library for ps2 devices.  Common devices are
 * mice, keyboard, barcode scanners etc.  See the examples for mouse and
 * keyboard interfacing.
 * limitations:
 *      we do not handle parity errors.
 *      The timing constants are hard coded from the spec. Data rate is
 *         not impressive.
 *      probably lots of room for optimization.
 */

#include "ps2.h"

/*
 * the clock and data pins can be wired directly to the clk and data pins
 * of the PS2 connector.  No external parts are needed.
 */
PS2::PS2(int clk, int data)
{
	_ps2clk = clk;
	_ps2data = data;
	gohi(_ps2clk);
	gohi(_ps2data);
}

/*
 * according to some code I saw, these functions will
 * correctly set the clock and data pins for
 * various conditions.  It's done this way so you don't need
 * pullup resistors.
 */
void
PS2::gohi(int pin)
{
	pinMode(pin, INPUT);
	digitalWrite(pin, HIGH);
}

void
PS2::golo(int pin)
{
	pinMode(pin, OUTPUT);
	digitalWrite(pin, LOW);
}

/* write a byte to the PS2 device */
void
PS2::write(unsigned char data)
{
	unsigned char i;
	unsigned char parity = 1;
	
	gohi(_ps2data);
	gohi(_ps2clk);
	delayMicroseconds(300);
	golo(_ps2clk);
	delayMicroseconds(300);
	golo(_ps2data);
	delayMicroseconds(10);
	gohi(_ps2clk);	// start bit
	/* wait for device to take control of clock */
	while (digitalRead(_ps2clk) == HIGH)
		;	// this loop intentionally left blank
	// clear to send data
	for (i=0; i < 8; i++)
	{
		if (data & 0x01)
		{
			gohi(_ps2data);
		} else {
			golo(_ps2data);
		}
		// wait for clock
		while (digitalRead(_ps2clk) == LOW)
			;
		while (digitalRead(_ps2clk) == HIGH)
			;
		parity = parity ^ (data & 0x01);
		data = data >> 1;
	}
	// parity bit
	if (parity)
	{
		gohi(_ps2data);
	} else {
		golo(_ps2data);
	}
	// clock cycle - like ack.
	while (digitalRead(_ps2clk) == LOW)
		;
	while (digitalRead(_ps2clk) == HIGH)
		;
	// stop bit
	gohi(_ps2data);
	delayMicroseconds(50);
	while (digitalRead(_ps2clk) == HIGH)
		;
	// mode switch
	while ((digitalRead(_ps2clk) == LOW) || (digitalRead(_ps2data) == LOW))
		;
	// hold up incoming data
	golo(_ps2clk);
}


/*
 * read a byte of data from the ps2 device.  Ignores parity.
 */
unsigned char
PS2::read(void)
{
	unsigned char data = 0x00;
	unsigned char i;
	unsigned char bit = 0x01;

	// start clock
	gohi(_ps2clk);
	gohi(_ps2data);
	delayMicroseconds(50);
	while (digitalRead(_ps2clk) == HIGH)
		;
	delayMicroseconds(5);	// not sure why.
	while (digitalRead(_ps2clk) == LOW)
		;	// eat start bit
	for (i=0; i < 8; i++)
	{
		while (digitalRead(_ps2clk) == HIGH)
			;
		if (digitalRead(_ps2data) == HIGH)
		{
			data = data | bit;
		}
		while (digitalRead(_ps2clk) == LOW)
			;
		bit = bit << 1;
	}
	// eat parity bit, ignore it.
	while (digitalRead(_ps2clk) == HIGH)
		;
	while (digitalRead(_ps2clk) == LOW)
		;
	// eat stop bit
	while (digitalRead(_ps2clk) == HIGH)
		;
	while (digitalRead(_ps2clk) == LOW)
		;
	golo(_ps2clk);	// hold incoming data

	return data;
}

Ive seen some topics in forum with this libs working in same project. Also contoling servos with ps/2 mouse. But in my case it doesnt. Dont know what to do next. Im thinking of debuging lib with Serial interface masseges. Maybe I`ll find where it goes in unlimeted cycle or what… (there are lots of while loops in ps/2 lib)

The servo library uses timers, which ones... i'm not sure of, but your using pin 6 which does run on a timer. It is possible that pin 6 is causing the problems. Try moving the wire on pin 6 to another pin that it NOT pwm. See if that works, if it does not then the problem is somewhere deeper.

alredy did this. moved them to 31,32 pins. no effect. well... Ill try with another mouse later.. but dont think it is hardware problem.