PS2 Mouse Library

What am I missing? Looking at the PS2_mouse example of the library. mx and my are both of type char and they're using Serial.print(mx, DEC) and (my, DEC) to send to the serial port as human-readable ASCII text. Why do it this way, couldn't they have used mx and my of type int? And what if you need to access those values in your prrogram?

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

You could probably use an int to store the value from mouse.read() but it would just take up 16 bits to store the 8-bit value so it is wasteful of memory. The data type 'char' is an integer data type so you can do the same calculations with it as you could with an int.

long int positionX = 0;
long int positionY = 0;

...

mx = mouse.read();
my = mouse.read();

positionX += mx;  // Update current cursor position
positionY += my;  // Update current cursor position

Thanks John, It never even crossed my mind about how much memory it would take up. I tried out your code last night and I have a question. Why use compound addition (+=) when adding your code to the existing code returns the same values for positionX/mx and positionY/my?

The mouse data returned is the relative motion of the mouse. The values in positionX and positionY are absolute positions - the cumulative effect of all the mouse movement.

I forgot to mention that positionX and positionY should be globals, not local to loop() and initialized to 0 each time.