interfacing ps2 mouse with arduino

hello everyone…
i am doing a project using a ps2 mouse.
i want to get the X and Y coordinates as we move the mouse…
can anyone please help me with codes and libraries of arduino …
i have these codes but it is not working perfectly

problems with codes are…

  1. Once we start moving mouse X and Y readings increase but max reading we got was 101 in +ve X direction.
  2. Once mouse is halted its origin gets shifted and new position is treated as new origin(0,0).
  3. Also from little search I came to know that optical mouse provides 400 CPI(Counts per Inch) but I think it is pre-scaled in code somewhere.

mouse1.ino (1.77 KB)

PS2Mouse.cpp (5.26 KB)

PS2Mouse.h (826 Bytes)

I belive that the mouse is designed to give you the delta x and delta y for a fixed period of time, not the absolute position. If you think how a mouse actually works, this makes sense. It will also be consistent with your findings. So rather than setting X and Y to the delta x and delta y reading from the mouse ieach time, you need to increment them.

takes the datasheet of the onBoard camera(the only pic in the mouse) i don’t remember but you can use only ps2.h library and lot of write to set the maximum resolution.

i post my last prooject (the setup set the mouse from 2D to 3D mode that enable the z , that is the wheel )

#include <ps2.h>

/*
 * 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);
  char mstat;
  char mx;
  char my;
  char mz;
  byte button;
  byte resolution;
  byte samplingrate;
  boolean bLeft;
  boolean bZ;
  boolean bRight;
void mouse_init()
{
  mouse.write(0xff);  // reset so for default it's setted on 2D mode
  mouse.read();  // ack byte
  mouse.read();  // blank */
  mouse.read();  // blank */
  mouse.write(0xf0);  // remote mode
  mouse.read();  // ack
  delayMicroseconds(100);
  mouse.write(0xe8);//set resolution e8 up to 2^3(second number) count per mm
  mouse.write(0x03);
  mouse.read();// ack
  mouse.write(0xf3);
  mouse.write(0xc8);
  mouse.read();
  mouse.write(0xf3);
  mouse.write(0x64);
  mouse.read();
  mouse.write(0xf3);
  mouse.write(0x50);
  mouse.read();
  mouse.write(0xf2);//whole code for set 3d mouse
  mouse.read();
  byte in = mouse.read();
  /*if(in==0x03)
   Serial.println("Setting up at 3D");
  else
    Serial.println("Setting up at 2D, it hasn't z axis");*/
}

void setup()
{
  Serial.begin(9600);

  mouse_init();
}

void loop()
{

  /* 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();
  mz = mouse.read();

  mouse.write(0xe9);  // give me data!
  mouse.read();      // ignore ack
  button = mouse.read();//0 rightbutton 1 middel button 2 left button 3 always a 0 
  resolution = mouse.read();
  samplingrate = mouse.read();
  /* send the data back up */
  bLeft=((B0000100 & button)>>2);
  bZ=((B0000010 & button)>>1);
  bRight=(B0000001 & button);
  Serial.print(mx, DEC);
  Serial.print('/');
  Serial.print(my, DEC);
  Serial.print('/');
  Serial.print(mz, DEC);
  Serial.print('/');
  Serial.print(bLeft, BIN);
  Serial.print('/');
  Serial.print(bZ, BIN);
  Serial.print('/');
  Serial.print(bRight, BIN);
  Serial.print('/');
  delay(20);  /* twiddle */
}