Simple Arduino Tachometer isn't working

Hi!

I'm working with a laser tachometer using an Arduino Uno. When the laser is blocked, digital input 0 reads low, and an interrupt records the time. Like this:

#include <Time.h>

void setup()
{
  Serial.begin(9600);
  attachInterrupt(0,timestamp, FALLING);
}

void loop()
{  
}

void timestamp()
{
Serial.println(millis());
}

The serial port is monitored by a Matlab script which records, processes, and graphs the tachometer data.

This all works fine under normal operation. HOWEVER: when the laser is blocked very gradually and slowly, the arduino seems to get only half of an interrupt and then fails. The result looks like a string of millisecond values with one value at the end that only contains a few of the digits:

13489
13511
13525
135

And then no more values are sent to the serial port until the board is reset.

I read about some software debouncing, but that had no effect on the problem.

Has anyone experienced this or know where the problem might originate?

Thanks for the help

Don't do serial output in interrupt handlers. Serial output is handled by interrupts, which are disabled while an interrupt handler is being executed. If your serial output exceeds the amount that can be buffered inside the serial port (which it easily could if the interrupt is being triggered repeatedly by input values staying around the trigger threshold), the Arduino will deadlock.

If it's necessary to print anything, save the data in a volatile global variable and have the main loop() code detect and print it. In this case, it seems to me that the handler should be doing no more than incrementing a global counter to indicate that it has been called; the main loop() would then read that counter at regular intervals to calculate the frequency and display that.

Additionally, you should try a higher baud rate for the serial communication (you can go up to 115200 on the Arduino side).