Read time differences in nanoseconds

I have a project in mind that I would like to use an arduino for.
I have a program on a linux machine that reads signals over the serial port and marks the time between the signals in nanoseconds.
I then need to compare the time between the signals (ie time1 - time2 = value)

Can the arduino record time in nanoseconds? Or would there be an alternative way for me to achieve the same goal?
In linux basically I echo to the serial port and every time the carrier detect pin is active I record the time down to the milliseconds, then I compare the times 1 by 1 to each other to see the timing in between the signals.
Hope that makes sense…

Thanks

millis() and micros() are time stamps of how many milliseconds and microseconds the processor has been running.

pseudo-code:

  long firstStamp = millis();
  // whatever happens
  long secondStamp = millis();
  long elapsedTime = SecondStamp - firstStamp;

Thanks for the response. :) I have read up on millis() but hadn't come across micros() before. I assume there is no nanos() function? I'm not 100% sure if microseconds will be accurate enough for me.

Thanks again for the response.

Microseconds is going to be as accurate as timers driven by a 16MHz clock can probably get.

I guess that makes sense :) Thanks for the responses. Gives me something to go with.

Thanks again

Yes it can record time in nS, with a resolution of about 1000nS :)

Truth is the basic instruction time of the processor is 65nS and the hardware timers run at that speed (or even slower I can't remember) so you haven't got much chance without external hardware.

I have a program on a linux machine that reads signals over the serial port and marks the time between the signals in nanoseconds.

Surely this resolution is nonsense under an OS?


Rob

Graynomad:

I have a program on a linux machine that reads signals over the serial port and marks the time between the signals in nanoseconds.

Surely this resolution is nonsense under an OS?

I kind of wondered that myself but I have this little thought about what I don't know about USB 3.0. Maybe it's not used for external sensor inputs but just thinking about what could be done is a bit exciting. Relative timing might be possible to determine by data position in the stream.

I assume there is no nanos() function? I'm not 100% sure if microseconds will be accurate enough for me.

Keep in mind that microseconds function returns values in 4 usec resolution 'steps'.

On 16 MHz Arduino boards (e.g. Duemilanove and Nano), this function has a resolution of four microseconds (i.e. the value returned is always a multiple of four). On 8 MHz Arduino boards (e.g. the LilyPad), this function has a resolution of eight microseconds.

Lefty

Graynomad:
Surely this resolution is nonsense under an OS?

Basically I am just reading the carrier detect pin as fast as possible in order to determine if it is “on” or not. everytime it is on I mark the time down to the nanosecond.

Nanoseconds may be overkill for what I need, and i think it is. It would just save me some hassle if I could basically just transfer the code I have now.
I don’t even own an arduino yet, but I think i’ll pick one up and just do some testing. I believe micros() will suffice, and probably even millis() will do as well.

Thanks for all the feedback

carlito: Nanoseconds may be overkill for what I need, and i think it is.

Accuracy and Precision are not the same thing. You can be precisely recording measurements down to the nanosecond, but it may not be very accurate.