View Full Version : How to measure time with the Basic Stamp 2

10-01-2008, 07:02 AM
Basically I am using two light sensors that are separated by 12 inches to measure how fast a toy car is going.

When the car passes the first light sensor, I want the clock to start keeping track of time and when the car passes the 2nd sensor, I want it to calculate the the time that has passed.

I was wondering how you use the resonator in the BASIC Stamp 2 or if I have to buy other parts to keep time. It doesn't have to be very accurate since the time between sensors is very small.

Post Edited (chechio) : 10/1/2008 12:11:27 AM GMT

Mike Green
10-01-2008, 07:13 AM
Tracy Allen's website (www.emesystems.com (http://www.emesystems.com)) has some information on statement timing in Parallax Stamp Basic. On the bottom of the main page, choose the "app-notes" link.

You might also look at the PULSIN statement which can give you very accurate pulse width times. You'll need to use the two light sensors to make a pulse, on when the car passes the 1st sensor and off when it passes the 2nd sensor. It's pretty easy to do using a simple set-reset flip-flop. The output of the 1st sensor "sets" the flipflop and the output of the 2nd sensor "resets" the flipflop. The output of the flipflop goes to the Stamp I/O pin that does the PULSIN timing. Google "wiki flipflop" or "wiki set reset flipflop" for a discussion. You can use a quad NAND gate to implement this (74HC00).

10-01-2008, 01:54 PM
I guessing that this is to measure the speed of a pine car derby type event. If so, there was a thread about that around here somewhere...

PJ Allen
10-01-2008, 07:14 PM

Jesse Hasty
10-01-2008, 09:24 PM
Would it be possible to use a polled interrupt scheme, so the first sensor puts the stamp into a routine that counts time and the second input takes it out of the routine? You would need a safety timer so that the processor didn't wait forever for the second input. Is there someplace on this website which provides timing information for the various program steps?

10-02-2008, 03:03 AM
First off, great avatar PJ and thanks for everyone's inputs.

I'm actually making a 5x5 grid of lights, 5 going across and 5 vertical and another grid of just 5 horizontal lights about a foot behind the first grid. I'm trying to make a device that detects if a pitch in baseball (with a larger ball) is either a strike or a ball and how fast it is going. The first grid is to detect if the pitch is a ball or a strike. And I want to record the time it takes from getting from the first grid to the second grid to get the speed. Does that make sense? I used the toy car example cuz that is what I am basically doing, the time between two sensors.

After I looked at the circuit diagram of the quad NOR IC of an RS flipflop in the other forum, I have many sensors that can start the detector and many that can stop detector. Is there a way I can still use just one RS flipflop? Or what the best way to find the time between grids?

I'm not a programmer so I'm not quite clear on using the pulse width? I looked at the sample of PWM on emesystems.com but i didn't really understand it. Can you give me a sample code.


Mike Green
10-02-2008, 03:58 AM
What you're proposing is much much more difficult than timing toy cars. The speed is much higher and the pulse width between events is much less. In addition, you have much more of a problem getting a reliable signal from the sensors because of the size of the ball and its movement through the grid.

If you can get a reliable signal that's true when the ball is crossing the first grid and another reliable signal that's true when the ball is crossing the second grid, you can use those signals for the set/reset of a single RS flipflop and use the output of that flipflop for a Stamp pulse input which can be measured to an accuracy of a few microseconds with the PULSIN statement. The leading edge of the 1st grid's pulse will set the flipflop and be used to start the timing of the PULSIN statement. The leading edge of the 2nd grid's pulse will reset the flipflop and be used to stop the timing and report the difference between the two leading edges.

The PWM statement is something completely different and is normally used to produce a slowly changing or specific program defined analog voltage. Look at the description of PULSIN.

Please read my previous message.