help with timers and limit switches

Hi

I have an Arduino mega 2560. I'm trying to measure the time it takes for a linear stroke to go from the retracted position till the fully extended. I thought the simplest way of doing this would be use two limit switches and internal timer. one limit switch would start the timer/counter and the other would stop it, and then the timer would output the time on to a spreadsheet of some sort. However, I am very new to micro-controllers and programming and i'm having trouble starting this project. Are there any libraries that I can use? is there a better way of keeping time? Is this even possible? Any help will be greatly appreciated!

Thanks in advance!

How fast are you trying to measure? If you are trying to measure microseconds that may be difficult or impossible, at least with standard libraries and functions. If you are measuring hundreds of milliseconds or more it should be pretty easy.

The simplest method is to read the function millis() when the first switch is triggered and then read it again when the second switch is triggered. The difference will be the number of milliseconds between the two events.

Another option is using photo-interrupters. The time delay of the switches will be more consistent.