problem reading two PS/2 mice

Hello to all. I use succesfully this library GitHub - kristopher/PS2-Mouse-Arduino: Arduino/Wiring Library for interfacing with a PS2 mouse. for reading coordinates from a PS/2 mouse using arduino.Though, what I really want to do is read data from multiple mice. In this I find some problems and I would really appreciate if you could give me some help!

I try to modify the example code to get input from two mice. So I create an other object, dublicate the code and connect a second mouse to two different pins of arduino (like the first). To my surprise, what happens is that I get x, y from the first mouse like before but from the second I can only get x! Instead of y I receive to my serial monitor a constantly icreasing value. I am not profficient in programming and C++, so I cannot understand why this is happening.

Do i have to connect the second mouse to the same arduino pins (maybe like an I2C communication)? Is this supported by this library? Is it possible in general to recieve data from multiple
mice this way? If yes, from how many? Do I have to add some pull-up resistors?

Its important to me to manage to read 2 mice (for localization reasons), as it's part of my thesis in robotics. If you find some time, please give me some advice. I will really appreciate it.
Thank you very much in advance!

From a short overview of the library I would say that you can use multiple mice. Show us your code and how you wired the mice to your Arduino.

This is my code. It is the example code of this library a little bit modified.

/**
 * Reads X/Y values from a PS/2 mouse connected to an Arduino
 * using the PS2Mouse library available from
 *   http://github.com/kristopher/PS2-Mouse-Arduino/
 * Original by Kristopher Chambers <kristopher.chambers@gmail.com>
 * Updated by Jonathan Oxer <jon@oxer.com.au>
 */

#include <PS2Mouse.h>
#define MOUSE_DATA 5
#define MOUSE_CLOCK 7

PS2Mouse mouse(MOUSE_CLOCK, MOUSE_DATA, STREAM);
PS2Mouse mouse2(3, 8, STREAM);

int xx = 0;
int yy = 0;

int xx2 = 0;
int yy2 = 0;

/**
 * Setup
 */
void setup()
{
  Serial.begin(9600);
  mouse.initialize();
  mouse2.initialize();
  //38400
}

/**
 * Main program loop
 */
void loop()
{
  int data[2];
  mouse.report(data);
  
  int data2[2];
  mouse2.report(data2);
  
   xx = xx + data[1];
   yy = yy + data[2];
   
   xx2 = xx2 + data2[1];
   yy2 = yy2 + data2[2];
  
  Serial.print(data[0]); // Status Byte
  Serial.print(":   ");
  Serial.print(xx); // X Movement Data
  Serial.print("   ");
  Serial.print(yy);
  Serial.print("  ||  ");
  Serial.print(xx2); // X Movement Data
  Serial.print("   ");
  Serial.print(yy2);
  Serial.println();
  
  //delay(10);
}

And this is how I wired the mice to the arduino. According to this SohilPatel: Arduino + PS/2 Mouse
I connect the data and clock pins to four of the arduino digital pins. I used Digital pin 5 as DATA and 7 as CLOCK for the first mouse and pin 8 as DATA and 3 as CLOCK for the second mouse (its a random combination-I have tried others too but the problem remains). Are these connections correct? Should it work this way? Again, I can get x,y from one mouse, but only x ftom the second!

You should change these lines

xx = xx + data[1];
   yy = yy + data[2];
   
   xx2 = xx2 + data2[1];
   yy2 = yy2 + data2[2];

to

xx = xx + data[0];
   yy = yy + data[1];
   
   xx2 = xx2 + data2[0];
   yy2 = yy2 + data2[1];

In C/C++ array indexes start at 0 not 1. Your array has 2 elements and you were accessing the non-existing 3rd element, which is just the memory after the array, so your values are non-deterministic.

I think this is correct. take a look at the subroutine of the library

int * PS2Mouse::report(int data[]) {
  write(0xeb); // Send Read Data
  read_byte(); // Read Ack Byte
  data[0] = read(); // Status bit
  data[1] = read_movement_x(data[0]); // X Movement Packet
  data[2] = read_movement_y(data[0]); // Y Movement Packet
  return data;
}

the data matrix has 3 elements and x-->data[1], y-->data[2], don't you think?

with some more experiments I found out that the data[2] value is replaced by the data2[0] value (the status bit of the second mouse object)
I found out that : data2[0]=data[2] , data2[1]=data[3] , data2[2]=data[4]. strange!

any ideas how to make it data[2]-->y (like it is when I use only one mouse) ?

please find attached a picture of my serial monitor. both mice are not moving, so it should be 0 0 || 0 0 . Instead, the second value is something else

In this case your definition is wrong:

int data[2];

must be

int data[3];

to hold 3 values.

you are so right!!! now it makes sense. thank you very much. now I will add one more mouse and the localization system for my robot will be in a good way!