Timer/Counter help for laser trip wire (speed trap)

So i'm very much a newb at coding and I have a very unique situation that I'm trying to figure out. Basically trying to create a speed trap by using 2 laser trip wires over a set distance. Trying to start a timer based on the first photoresistor going low, and then stopping when the second laser trip wire is triggered, then pass that time count into a formula to get a speed. Problem is that it may not always be that the second trip wire gets triggered, so I need to restart the counter/timer if the first trip wire is triggered again without hitting the second trip wire. Reasoning is that this will be on a foosball table and the second trigger will be in the goal, thus the reasoning that the second trigger may not always be triggered before the first one gets triggered again.

Just cant think of a good way to break a for or while loop based on the laser trip wire going low...starting is easy...restarting it is proving for me to be difficult.

Don't know your experience level, but the two avalible user interrupt pins ( pins 2 and 3) would make pretty simple solution. Attach first sensor to interrupt pin 2 and setup for falling trigger. The interrupt routine would just save the current millis() count into a variable called save_start_time.

The second sensor would wire to pin 3 also falling trigger and it would save the millis() count into a variable called save_stop_time and also a global variable called say speed_complete would be set to 1.

Then your main loop could test for speed_complete and get the elapsed time as ET = save_stop_time - save_start_time and then reset speed_complete to 0.

That's just one way to do it and probably needs more details but hopefully you get the basic concept. User interrupts can be quite useful.

By the way one can use the micros() function instead of millis() function if better timing resolution is required as long as the start and stop will not be longer the 70 mins or so.

Lefty

Thanks for the reply I get the big picture idea and the concept do you know of any links to using those interrupts?

BigCity: Thanks for the reply I get the big picture idea and the concept do you know of any links to using those interrupts?

Well the arduino reference has this:

http://arduino.cc/en/Reference/AttachInterrupt

Lefty

Heh, you beat me to a reply. I'll do some research and reply with what will hopefully be more specific questions. I appreciate the starting point!

Trying to start a timer based on the first photoresistor going low, and then stopping when the second laser trip wire is triggered, then pass that time count into a formula to get a speed.

You don't need a timer. What you need is to record WHEN the first sensor goes LOW (every time). Then record WHEN the second trigger goes LOW. The difference between the times is what defines the speed, although I don't see how speed matters in a foosball game.

EDIT - Nevermind helps if you have it on the correct pin to begin with...

Can attachInterupt work with a variable input like a photoresistor directly I notice the associated pins seem to be digital and I can't get the sample code to toggle the LED with the photoresistor, but it does work with a button.

Ok, I got it working to a certain extent, I can get it to track changes read the mills() into a variable for both a start and stop but there's a problem. It is working from the photoresistors, but the problem is that i have to cover the photo cell almost completely to get it to register for the interrupt. Is there anyway of setting/calibrating a specific value so that it will trigger based on a little less light instead of complete darkness?

Is there anyway of setting/calibrating a specific value so that it will trigger based on a little less light instead of complete darkness?

Post your code...

retrolefty: The second sensor would wire to pin 3 also falling trigger and it would save the millis() count into a variable called save_stop_time and also a global variable called say speed_complete would be set to 1.

Just FYI, speed_complete should be declared volatile. That prevents the compiler from optimizing access to the variable which may prevent the loop from observing a value change.

Example: volatile uint8_t speed_complete = 0 ;

How are the photoresistors wired up?

What is the resistance when you have the laser on the sensor and what is it when the beam has been broken?

The more you can shade the sensor from ambient light in the room, the more reliable results will be.

Thanks for the added replies, but I realized that I didn't really need the interrupt I could allow the first trigger to be triggered based on a simple IF statement. Since the first trigger I wanted it to read in the millis() time any time the laser is broken then it seems to work fine. Then do the same with the second laser trigger, but only with a delay to debounce the circuit so to speak.

New problem now is how can I use the millis value collected in formulas to produce decimals. Right now I just have them declared as int StartTime = 0;

This seems to work fine for simple subtraction of the second laser trigger - the first laser trigger, but if I try to divide it by 1000 then I just end up with zero.

TimeSeconds = ((StopTimeTm1 - StartTimeTm1)/1000); Speed = 1/TimeSeconds; //based on 1 foot set distance to give ft/sec

It's because you are using integers and expecting to get fractional amounts.

Use floats or scale the numbers before dividing and just keep track of where the decimal place is supposed to be.

int StartTime = 0;

The millis() function returns an unsigned long value. It's best to use an unsigned long to hold the output.

Let the Arduino run for more that 32.7 seconds to see why.