2 buttons for 1 input detection of the faster one.

Hello!
I have to make a final Arduino project for my high school diploma.
I want to make a retro 80's style game where you have 2 LED towers(7 LEDS high each). And each player has 1 button to press or smash :slight_smile: But I can't figure out how to detect which button was pressed faster. Goal is that when player1 presses faster his LED lights up and opponent's LED shuts down. And so on for example: When both have 5 LEDs; Player1 presses faster and he gets 6 LEDs but player2 gets 4 LEDs. And so on until one of them reaches 7 LEDs.
Thanks in advance.

What have you tried? Do you have any code? Do you have a schematic?

this comes up now and again.

if you put two if() statements, one after the other, there is a timing difference between them.
so small that humans cannot see the difference.

if you do it in hardware with some transistors, you can eliminate that time difference.
the first one with the correct circuit would take the power from the other one.

As a note there was a game show, JEAPORDY that was rigged. Every once in awhile the contestant would raise their hand above the kiosk and press the button showing they were pressing, and it would not register.

You can verify your set up by letting them swap buttons before they start.

    P1 = digitalRead( B1 );
    P2 = digitalRead( B2 );

    if( P1 )
        // player 1 wins (tie always goes to player 1)
    else if( P2 )    
        // player 2 win

The delay between the two reads is insignificant on the human time scale.
It would be impossible to dispute a tie. You could use a port read if you feel it's necessary. Still, what do you do for a tie?

This alternates between reading button 1 and 2, so equal chances.
A third button resets.
All three buttons between pin and ground, with pull up in pinMode.
Incomplete and untested.
Leo..

void loop() {
  if (!digitalRead(ButtonPin1)) {
    // player one LED 'on' code here
    while (resetButton); // wait here for reset
    // player one LED 'off' code here
  }
  if (!digitalRead(ButtonPin2)) {
    // player two LED 'on' code here
    while (resetButton); // wait here for reset
    // player two LED 'off' code here
  }
}

Thanks!

I get it now. I don't have any schematics or code because this is idea and I have 3 weeks more to decide my project but I wanted to check all the options.

Thanks again.

Maturalac1:
Thanks!

I get it now. I don't have any schematics or code because this is idea and I have 3 weeks more to decide my project but I wanted to check all the options.

Thanks again.

the goal is to have loop run as fast as possible.
if you make it run for, say, 10seconds
and 0.001 seconds after it read the inputs, #2 was pressed and 0.001 seconds before they are read again, #1 was pressed,
#1 wins. because #1 is read first.
the bottom line is that code has to be very simple

A simple 7400 wired as two bistables will tell you which of 2 buttons is pressed first. I did it 50 years ago.

Allan

allanhurst:
A simple 7400 wired as two bistables will tell you which of 2 buttons is pressed first. I did it 50 years ago.

Allan

This is one case where doing it in Hardware would make more sense than doing it in software

I think so . And resolution down to a few nS - you'd need a pretty nifty processor and sw to achieve that.

I'll leave the ( trivial) circuit as an exersize for the user. I was only a schoolkid of probably about your age , Maturalac1, so you should be able to handle it. And would be worth some credit with your tutors.

A

Why not utilize the two dedicated interrupts on pins 2 & 3 to minimize the software timing aspect? Int0 registers and locks out Int1 and vicey versi. A third switch or timer resets interrupt flags to restart.

YMMV