Program seems to stop looping?

I am using a library to interface with a PS/2 mouse, connected to an Arduino Micro (Leonardo compatible). The problem is that the program seems to stop entering loop(). What may be the reason for that?

Trimmed down program:

#include <PS2Mouse.h>
#define MOUSE_DATA 9
#define MOUSE_CLOCK 8

PS2Mouse mouse(MOUSE_CLOCK, MOUSE_DATA, PS2_STREAM);

void setup() {
  Serial.begin(9600);
  mouse.initialize();
}

void loop() {
  Serial.println("loop start");
  int data[2];
  mouse.report(data);
  Serial.println("loop end");
}

Example output, copied after a hang from the serial monitor of version 1.0.5:

loop end
loop start
loop end
loop start
loop end

Note the missing "loop start". So the program seems to hang after loop(), but where?

Try adding a delay in your loop; it's not really a good idea to print to the Serial monitor unimpeded like that when your loop() is likely running very quickly.

Arrch:
Try adding a delay in your loop; it's not really a good idea to print to the Serial monitor unimpeded like that when your loop() is likely running very quickly.

I was under the impression that the time it took to do a Serial.print was pretty much enough of a delay (sort of a self fulfilling delay). Maybe that was for a different application, but I would assume the same would apply here.

Ive never worked with that library but I have had times when similar things happen because I overflowed the serial buffer... Im curious to know if that delay suggestion works for you... I think it will....

fxmech:
overflowed the serial buffer...

I doubt that this happened: The output that I posted was all output, i.e. not just the last lines.

Im curious to know if that delay suggestion works for you... I think it will....

Indeed, after adding the delay, so far I wasn't able to reproduce the issue. Still that doesn't necessarily mean anything as behavior is totally random. I get plenty of hangs inside of the loop function, what also happened before from time to time, or no output at all.

What really bugs me is that is doesn't seem to be possible to have the serial monitor running right from the start.

It could be that the"loop start" print statement had been printed and buffered for transmission but not actually sent by the time the failure occurred. If that is the case then putting a delay after each print statement long enough for the transmission to complete would give you a more accurate trace output.

It may also be that the library is doing something clever with interrupts which is causing the Arduino to fail even though it is not in the middle of the call to mouse.report() at the time. A review of the source code for the library should tell you whether that's a possibility

Can't you connect the serial monitor and then reset the Arduino?

PeterH:
It could be that the"loop start" print statement had been printed and buffered for transmission but not actually sent by the time the failure occurred.

That sounds plausible. Already before posting my question, I have been thinking that the output may not get "flushed".

If that is the case then putting a delay after each print statement long enough for the transmission to complete would give you a more accurate trace output.

Interesting suggestion!

It may also be that the library is doing something clever with interrupts

No. The example code is using pins 5 (data) and 6 (clock), and I am using pins 9 and 8 respectively. The Micro's interrupt pins are 0 (RX), 1 (TX), 2, and 3.

Can't you connect the serial monitor and then reset the Arduino?

When I hit the reset button, then the USB connection to my laptop (WinXP) is lost, and I have to un-/plug the USB cable.