Arduino as a timing device using light beams

has anyone used an arduino for measuring timing of say a runner or car over a certain distance

could be a neat little project

Yes, it could... have you done it?

I’m working on it (for snowmobiles).

Based on my experience, the only problem you really need to take into account is making sure your photodiode (don’t use a photoresistor) is calibrated properly for the beam that is being sent to it. In this respect you need to be able to turn your beam off so you can get a value measurement between the ambient light and when the beam is hitting it. It also verifies that your beams are targeted properly.

Might be better off having the beam go off a reflector and have the receiver mounted next to the transmitter. Less wiring that way. Mount one unit at the start point, have it send wireless (RF) signal to the end unit, have the end unit do the actual time keeping. Just a thought.

that is some smart thinking

maybe a laser could be used instead of light beam

or could bluetooth be used

would be interestingto see how the professional systems work (probably wired)

2 x £1.99 laser pointers off ebay. Disassemble them to remove the laser diodes and attach two wires then use these as the beams! You can still use an LDR as the sensor with the same circuit ie BC547 or 2N2222 transistor with Emitter to GND then 10K + 10K pot between Base and GND and the LDR between Base and VCC. Connect the collector to VCC via an LED and a resistor (220 for 5V). You can then adjust the sensitivity with the pot with the arduino connected to the collector on each circuit.

With the above you simply need some very simple code.

so connect the first circuit to pin 2 and the second to pin 3.

while (digitalRead(2) == LOW) { // do nothing } int first = millis(); while (digitalRead(3) == LOW) { // do nothing } int timetaken = millis()-first

speed is a product of distance and time so the above can also give you the speed if you accurately measure the distance.

millis() returns an unsigned long, an int would overflow within a minute....

unsigned long measureTime(uint8_t pinA, uint8_t pinB)
{
  while (digitalRead(pinA) == LOW) ;
  unsigned long first = millis();
  while (digitalRead(pinB) == LOW);
  unsigned long timetaken = millis()-first;
}

Another approach would be IRQ based, leaves room for the Arduino to do something while waiting....

volatile unsigned long first = 0;
volatile unsigend long second = 0;

void IRQA() 
{
  first = millis();
}

void IRQB()
{
  second = millis();
}

void resetTime()
{
  first = second = 0;
}

unsigned long lapTime()
{
  return millis() - first;
}

unsigned long finalTime()
{
  if (second > 0) return second - first;
  return 0;
}

or use - http://www.arduino.cc/playground/Code/StopWatchClass -

volatile unsigned StopWatch MySW;

void IRQA()
{
  MySW.start();
}

void IRQB()
{
  MySW.stop();
}

my 2 cents Rob

robtillaart: millis() returns an unsigned long, an int would overflow within a minute....

my 2 cents Rob

I actually wrote pseudo code then realised that it was nearly C so just re-wrote it slightly and hadn't even thought about the size of int. The same code with "long" in front would do it though :)

I am quite new to the Arduino and have not seen lots of things people keep coming up with like the stop watch class! All very useful stuff.

Spend one evening reading through the playground and you will get enough ideas for a year of happy tinkering, probably even more ;)