Mouse sensor

Hi,

i'm reading a mouse sensor on the serial monitor using this code , but i have to disconnect/connect the 5v pin of the sensor many times before i get the data from it going on the serial monitor, can someone please see if there is any error in the code

#include <ps2.h>

PS2 mouse(6, 5);

char mstat,mx,my;

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 mouse_pos(char &mstat, char &mx, char &my){
  mouse.write(0xEB);
  mouse.read();
  mstat=mouse.read();
  mx=mouse.read();
  my=mouse.read();
}

void setup()
{
  Serial.begin(115200);
  mouse_init();
  
}

void loop()
{
  mouse_pos(mstat,mx,my);

  Serial.println(my,DEC);

}

Perhaps that if the routines are expecting X # of characters before returning and didn't get them, the routine will sit there until it does. By cycling power, you send junk characters at the interface... just a thought. Check the routines to see how many characters they expect before returning.

thank you for your reply ! , i added "while(!Serial);" in setup function and it works .