PS2 trackpad that output the brightness of 2 LEDS

Hello,
I have a trackpad here wired up as show on the arduino site.
And i have uploaded the code for the PS2 and that works perfect.

Now i want to take this a step further and i want the movement to change the brightness of to LEDS, 2 for X and one for Y.

I used this code and that works but not so great:

#include <ps2.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);

/*
 * 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);
}

int ledPinX = 9;
int ledPinY = 10;

void setup()
{
  Serial.begin(9600);
  pinMode(ledPinX, OUTPUT);
  pinMode(ledPinY, OUTPUT);
  mouse_init();
}

/*
 * 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();
  
  analogWrite(ledPinX, mx);
  analogWrite(ledPinY, my);

  /* 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(2000);  /* twiddle */
}

Does anybody now how to do this write?

best regards
Ronald

When you do the analogWrite, you must write a value in the range 0..255
You should start with variables for the brightness of each LED, and set them to 128. When you read an increasing X or Y value, you can increase/decrease the variable accordingly, and write the new values for the LEDs out, not the actual values of mx and my.

Does that help?

I think your main problem is that the mx and my values fetched by the mouse code are DELTAS from the last read position, rather than absolute values. Further, they seem to be 9-bit values with the sign the mstat value (though I suppose that usually you'll have movement less than 256 steps per update.)

Check here: Keyboard scancodes: The PS/2 Mouse