beginner question: timestamping incoming midi data

Hi there,

I want to timestamp incoming midi data that I will read into Arduino using this midi interface and this midi library. My plan is to wait until there is data available at the serial connection, read it in and then immediately after generate a timestamp using micros(). Then I want to send the event and the timestamp over serial usb to a host. Does anything here raise a red flag with anyone? In particular, is there anything that might cause the timestamp to be inaccurate? Thanks!

My plan is to wait until there is data available at the serial connection, read it in and then immediately after generate a timestamp using micros().

What are you expecting to get from the serial port? How will you know when a packet is complete?

Then I want to send the event and the timestamp over serial usb to a host.

The same serial port that you just read from? What does this have to do with MIDI?

In particular, is there anything that might cause the timestamp to be inaccurate?

The fact that micros() only updates every 4 microseconds.

Thanks for the quick reply!

A microsecond granularity is fine with me, but that's good to know about. I want to send the data over usb to a host running Matlab where I collect the midi note data, along with some other sensors that are connected to the Arduino.

As for knowing when the packet is complete, I had assumed that the midi library would handle that.

If you look at the picture in your first link, the MIDI device is connected to the serial port. You can't also use the serial port to send data to the PC.

Using that library on a Mega with multiple hardware serial ports looks possible. Using it with SoftwareSerial and any other pins looks a bit more challenging.

This is valuable information. Just to clarify, even though I plan to use only the Rx port I can't use the Tx port to transmit to host?