# Feasibility: high-speed sensing, calculation, and output

Hi everyone,

I wanted to check if a project I have in mind can be done with Arduino. To be clear, I've done some Googling on the subject and think what I have in mind might work, but I wanted to be sure. I also have no hands-on experience with Arduino yet, and have little experience with electronics in general, so please forgive my ignorance.

Here what I'm hoping to do: I have two lasers pointed at two photodiodes, and an object is moving through the path of each laser over the course of ~1 millisecond, blocking each laser from hitting its respective photodiode. The photodiodes are producing TTL signal. What I'd like to do is determine the exact length of time, T, between the blockage of each laser (ideally to within a few microseconds), calculate a number, N, by doing multiplication and addition on T, and then output a TTL signal after waiting N microseconds. Ideally, this whole process will happen within 1-2 milliseconds from the time the second laser is blocked, and faster than 1 millisecond is ideal.

Can I do this with Arduino? Thanks, and I'll be happy to clarify if something is confusing.

Might be tricky.
You could use an interrupt to capture micros() when each is interrupted, take the difference between the two. The unsigned long math should be quick.

Maybe even use pulseIn() to capture the times of the two falling edges.

Thank you for your input! Would recording the times and doing the math be the rate limiting step, or would outputting the signal be the rate limiting step? I talked briefly with a colleague who said that his Arduino (I didn't catch which version he has, but I think it's an older one) could only output information at around 500 Hz, which would be way too slow for what I need. My colleague said that newer versions can output with a much higher frequency, though, and that's what I'm gathering from the information I'm reading online as well. I assume I should go with the newest version of Arduino if I want this project to work?

Thanks again.

Also, would this project work better with something like a Teensy 3.1? I'm reading that they can perform certain functions faster than Arduino.

You don't need a faster processor, but a sufficiently fast output connection. What output device do you have in mind?

An 'Arduino' is an overlay of software on top of the micro-controller chip. you address the 'arduino' the arduino reads your code, then has to address the port, then read the port. all this overhead incurs some speed penalty. the price you pay for letting the 'arduino' be your software interface.

if the process to read the pin can be done in 1 clock cycle on the chip, but it takes 4 clock cycles, then the 'arduino' is causing the delay.

if that is the case, then you could speed up the chip by a factor of four to get the same rate as a raw chip.

also, if another chip has it's software overlay that takes 16 clock cycles to perform the same operation, then the Arduino is 4 times faster.

we typically use a variety of chips, each chip has different port assignments, the arduino handles the port assignments for you at a speed pentaly. but the process is so fast that often the arduino is still orders of magnitude faster than inputs.

Download the datasheet for the 328 and search on TCNT1 which is the counter for system timer 1 which has a maximum rate of 16 MHz. I have done timing like you describe by using the TTL signal from the photodiodes to trigger an interrupt, on the first interrupt set the counter to zero with the following statement:
TCNT1 = 0;

When the second interrupt is triggered the elapsed time is just the value of the counter (TCNT1) times the period of the clock, the period of the clock is 1/(clock frequency) so the elapsed time is now:
elapsedTime = TCNT1 / 1.6e7; // Elapsed time in seconds

You did not say what calculations you wanted to make to get N but I think you can meet your timing requirements after the second laser is tripped.

A few Pointers:

• keep the routines that are called to service the interrupt are as small as possible, this is a universal rule for interrupt service routines
• search the keyword volatile for variable declarations
• if either laser can be triggered first you will need to set a global variable that tells the system the timer is running so you know when you have to set TCNT1 = 0 and when to calculate the elapsed time
• there are probably others but this is all I can think of.

Hope this helps,