Question about a timing system.

Hello everyone,

For a project for school im creating a drag racing timing system for R/C cars. I'm using two infra red beams (one at the start line one at the finish) which the r/c car will break through to trigger the starting and stopping of the timer. I am using the Arduino uno board in the system. How do I go about getting the Arduino board to work in terms of doing the actual timing for the run the car is making. Does the board have a clock that can be used? also what would I need to do as far as writing the programming for this project. I have an LCD screen that would read out the times of each lane. Also can the board be used to time two seperate lanes at the same time. Thanks in advance for your help.

I'm using two infra red beams (one at the start line one at the finish) which the r/c car will break through to trigger the starting and stopping of the timer.

How are you detecting the beam break? Are you polling or using interrupts?

Does the board have a clock that can be used?

Yes. Both millis() and micros() return the current time (one in milliseconds, the other in microseconds). Record the time the first beam breaks, and the time the second beam breaks. The difference is the time down the track.

Also can the board be used to time two seperate lanes at the same time.

Depends on whether you are polling or using interrupts. With polling, yes, but there are only two external interrupts available, on pins 2 and 3.

Just a quick question. Your way couldn't the car with the fastest elapsed time still be the loser because it got a slow start off the line? I would think the starting trigger should be independent of either cars start?

Lefty

You have a beam for each car, yes, and 2 cars? Interrupt0 at the start of the race for car1, use the same interrupt at the end of the race. Interrupt1 for car2.

I'm looking to do this with interrupts. No matter the Elapse time difference, the winner will be declared by who breaks the finish line beam first with a seperate circuit that will feature a "win light" LED that will light up on the finish line sensor of the winning lane to depict the winner. All I need this code to do is output two seperate elapsed times. Ive never written code with the arduino before, would anyone have this type of code or simliar code that would point me in the right direction? Also keep in mind the elapsed times would be outputted to an LCD display screen.

to use the lcd there is a great library look into that

Yep I did found a very good library for the LCD thank you

interrupts are easy too.
Here's some of the basics of what you'll need.
You'll have to clean it up some to get it to compile, is mostly written as pseudo-code to get the ideas across.

How are you detecting the beams being broken?
You could start with a simple mechanical switch, the car hits it & shorts an input pin to gnd to cause the end of race interrupt.

// anything after double slash is a comment

#include <avr/interrupt.h> // interrupts library

// declare your interrupt pins. 2 & 3 are the external interrupts
int pin2 = 2; // int means data type integer
int pin3 = 3;

int car1done = 0;
int car2done = 0;

// for testing to start with, just turn a couple of LEDs on to check the interrupts are working
int LED1 = 4; // pin 4 to 330 ohm resistor to Anode of LED to gnd - write high to turn on
int LED2=5;

// define what you need for the LCD control

// Write a couple of ISRs to do this kind of functionality

//***************************************************
// * Name: pin2Interrupt, "ISR" to run when car 1 finishes
void pin2Interrupt()
{
// disable car 2 interrupts
// if (car 2 not done (== 0) ) {turn on car 1 done light and set a flag that car 1 is done}
// enable car 2 interrupts
// reset the done flags
}

//***************************************************
// * Name: pin3Interrupt, "ISR" to run when car 2 finishes
void pin3Interrupt()
{
// disable car 1 interrupts
// if (car 1 not done) {turn on car 2 done light and set a flag that car 2 is done}
// delay a second or two, then turn off done light
// enable car 1 interrupts
// reset the done flags
}

void setup()
// declare the pins as inputs with internal pullup resistors
pinMode (pin2, INPUT)
digitalWrite (pin2, HIGH)
pinMode (pin3, INPUT)
digitalWrite (pin3, HIGH)

// declare
pinMode (LED4, OUTPUT)
digitalWrite (LED4,LOW)
pinMode (LED5, OUTPUT)
digitalWrite (LED5, LOW)

void loop()
{
// now wait for the cars to start
// will need some attachInterrupt statements

/* Setup pin2 as an interrupt and attach handler. */
attachInterrupt(0, pin2Interrupt, LOW);

/* Setup pin3 as an interrupt and attach handler. */
attachInterrupt(1, pin3Interrupt, LOW);

}