Go Down

Topic: timimg 2 almost simultaneously pressed buttons (Read 1 time) previous topic - next topic

tomansbro

I am building a drag race game.  There are 2 sets of light, one for each lane.  When the green light comes on both players press a button as quickly as possible.  I want to measure the time between the Green light turning on until each button is pressed ( reaction time).
Sounds simple, I can get one players time no problem, but i miss the second players button push a lot. I tried using interrupts, but the timing got messed up...any ideas? thanks

johnwasser

I would use interrupts and record the micros() time on the first interrupt:
Code: [Select]

TimeA = 0;
TimeB = 0;
// run down the lights
StartTime = micros();
while (TimeA == 0 && TimeB == 0) ;  // waiting for both buttons
if (TimeA < StartTime). // disqualified. Jumped the lights
Same for B
Compare A to B to see which won

ISR_A() {
    if (TimeA == 0) // protect against button bounce
        TimeA = micros();
}
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp

Grumpy_Mike

Quote
but i miss the second players button push a lot.

Is that because you are using the push button levels and not the change in levels?

Who knows without looking at your code? Which you have not posted.

Paul__B

And I on the other hand would not use interrupts, because microsecond accuracy is clearly not required.

The problem is (of course) dead easy.  At the start point (when you change the lights), you save the reading of the millis() count, then in your loop, you look at each switch alternately.  If one switch is pressed, you record the millis() count for that switch and set a "flag" (variable) to note that the switch was indeed pressed so that on successive traversals of the loop, you do not examine that switch again.  When at the end of that loop function, you find that both switches have been pressed, you progress to the display function.

The salient points in this are that you understand the concept of a main code loop.  You cannot use "busy waiting" or "while" (or "delay()") functions.  The loop repeats indefinitely performing one task after another, never stopping.  Tasks are controlled by "state switches" (variables) which tell them whether to perform an action or fall through to the following task - in other words, each is controlled by an "if" switch.  Tasks here include the event starting task, the switch reading task and the display task; you can even control them with one task switch variable; zero to set up the start, one to read the switches and two to display the result, using a "case" statement.

In this particular instance, you do not need to "debounce" the pushbuttons except in the "start" task.  It should be obvious, but the start cannot occur until both buttons have been released without exception for an interval of some seconds.  After the start however, the contact has only to be made for a moment to be registered.

I am somewhat curious as to why you particularly mention " 2 sets of light, one for each lane"?

dc42

I would do as Paul says, but use micros() instead of millis() just in case both buttons are pressed within 1ms of each other.
Formal verification of safety-critical software, software development, and electronic design and prototyping. See http://www.eschertech.com. Please do not ask for unpaid help via PM, use the forum.

lar3ry

Quote
I am somewhat curious as to why you particularly mention " 2 sets of light, one for each lane"?


Well, seeing that it's a drag racing game, he is probably making a "Christmas Tree", which is the tower with a number of lights, indicating pre-stage, stage, warning ambers, green, and red.  Have a look at http://www.youtube.com/watch?v=-s5lz3gaHEw to see what it looks like. Search youtube for "drag racing 101" and see the one subtitled "The tree and staging" for the meanings of the different lights.

tomansbro

Thank you All for your quick replies..  Yes the answer was Dead Simple......as soon as removed the Serial.print statement from the ISR!!!
  Thanks again for your input and I will try to rewrite the code using mills() and see how that works out!

Paul__B


Well, seeing that it's a drag racing game, he is probably making a "Christmas Tree", which is the tower with a number of lights, indicating pre-stage, stage, warning ambers, green, and red.


No doubt, but he specified two sets of lights.  I was curious as to whether this meant some variation on the theme.

lar3ry



Well, seeing that it's a drag racing game, he is probably making a "Christmas Tree", which is the tower with a number of lights, indicating pre-stage, stage, warning ambers, green, and red.

No doubt, but he specified two sets of lights.  I was curious as to whether this meant some variation on the theme.

Well yes, but they ARE two sets of lights: one for each lane. They operate independently until both cars have fully staged, at which time they operate simultaneously for the amber and green, then back to independently for the red, if needed.

Go Up