There's no bug in the code, but the sensor doesn't respond

I’m trying to use a PS2 mouse to measure distance of how far did the robot travel.
I have found this code, I tried it before and it worked, but now it doesn’t. I tried to find out what was wrong whit it myself, so I wrote Serial.print("") in a few places, you will see in the code. The code starts with no problems, but it gets past Serial.print(“setup1”), but doesn’t get to Serial.print(“setup2”) and I don’t know what may be wrong.

  #include <ps2.h>

 * this is a modification to the original ps2 mouse sketch, it requires ps2 library,
 * can be found on arduino website.
 * Modified by Jabbar Ali.
 * Feel free to use or modify.
 * an arduino sketch to interface with a   mouse ( optical, ball, ps2 and usb).
 * it measures the distance the mouse travels.
 * a button is used to zero the reading.
 * one side of the button is connected to pin 7 other to ground. 
 * it writes the results to the serial port.

 * Pin 5 is the mouse data pin, pin 6 is the clock pin
 * Feel free to use whatever pins are convenient.
PS2 ps2(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()
  ps2.write(0xff);  // reset
  Serial.println("2");;  // ack byte;  // blank */;  // blank */
  ps2.write(0xf0);  // remote mode
  Serial.println("3");;  // ack

void setup()
 // pinMode(zeroPin, INPUT_PULLUP);
* 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 */
  ps2.write(0xeb);  // give me data!;      // ignore ack
  mstat =;
  mx =;
  my =;
  Serial.print(mx/(18.500));// gives mm reading for my  mouse
  Serial.print(my/(18.500));//  gives mm reading for my  mouse


Please and thank you

it gets past Serial.print("setup1"), but doesn't get to Serial.print("setup2")

So, how far does it get?

(Despite the title of the thread, I suspect there is indeed at least one bug)

do you have a logic analyser to see what happens on pin 5 and 6?

Zarolis: it gets past Serial.print("setup1"), but doesn't get to Serial.print("setup2") and I don't know what may be wrong.

You have some Serial.print() statements in your mouse_init() function so you should have a more precise indication of where the problem lies.


Note that printing takes time and is done under interrupt. If the mouse.init blocks all interrupts somehow, printing stops too

does the problem/output looks the same when you use Serial.begin(115200); ?

It's not the most elegant code in the world. It does lots of this:-

while (digitalRead(_ps2clk) == LOW);

So if the mouse isn't clocking, everything stops.