Button faster than other

Hey guys, i can't create a logic to do this: i want, if i click a button faster then the other button the first one will light the led. this example i just using 2 but i want use 4 buttons

int led = 8;
int led2 = 9;
const int botao = 2;
const int botao2 = 3;
int estadoBotao = 0;
int estadoBotao2 = 0;

void setup() {
pinMode(botao, INPUT);
pinMode(botao2, INPUT);
pinMode(led, OUTPUT);
pinMode(led2, OUTPUT);
}

void loop() {
estadoBotao = digitalRead(botao);
estadoBotao2 = digitalRead(botao2);
if(estadoBotao == LOW) {
digitalWrite(led, HIGH);
} else {
digitalWrite(led, LOW);
}

if(estadoBotao2 == LOW) {
digitalWrite(led2, HIGH);
} else {
digitalWrite(led2, LOW);
}

}

I don't think you mean "faster". I think you mean sooner.

You have no idea when either switch is pressed, so it seems pointless to talk about doing something if one is pressed before the other.

You need to do a better job of defining your requirements. Talk about what should happen if switch one is pressed first. What should happen if switch two is pressed first?

fast detection of first pin to go high

//List of all pins and there bit position within the 32bit union below
#define PinB0 1
#define PinB1 2
#define PinB2 4
#define PinB3 8
#define PinB4 16
#define PinB5 32
#define PinB6 64
#define PinB7 128
#define PinB8 256
#define PinB9 512
#define PinB10 1024
#define PinB11 2048
#define PinB12 4096
#define PinB13 8192
#define PinB14 65536
#define PinB15 131072
#define PinB16 262144
#define PinB17 524288
#define PinB18 1048576
#define PinB19 2097152
#define PinB20 4194304
#define PinB21 8388608

#define PinBA0 65536
#define PinBA1 131072
#define PinBA2 262144
#define PinBA3 524288
#define PinBA4 1048576
#define PinBA5 2097152
#define PinBA6 4194304
#define PinBA7 8388608


union PinsToBits {
  volatile uint32_t All;
  volatile uint8_t  Port[4];
} Pins;

unsigned long PinMask;
void setup() {
  Serial.begin(115200);
  
  PinMask = PinB2 | PinB3 | PinB4 | PinB5; // Set the pins we want to watch Add More by placing the | pipes between the defigned pins
  Serial.println(PinMask, BIN); // 111100

}

void loop() {
  static unsigned long UsedPins;

  // This is a Fast While loop it won't exit until it finds a high pin and won't re-enter until you reset Used Pins to zero 0
  while (!UsedPins) {
   // Loop as fast as possible until one of the pins we assigned in pin mask goes high
    // each bit represents a pin
    // Lets get all the pin values in 3 lines of code
    Pins.Port[0] = PIND; // Pins 0-7
    Pins.Port[1] = PINB; // Pins 8-13
    Pins.Port[2] = PINC; // Pins A0-A8
    UsedPins = Pins.All & PinMask;       // only keep the pins we are watching
  }
  if (UsedPins & PinB2) { // Pin2 is high first
    Serial.println("PinB2");
  }
  if (UsedPins & PinB3) { // Pin2 is high frist
    Serial.println("PinB3");
  }
  if (UsedPins & PinB4) { // Pin2 is high first
    Serial.println("PinB4");
  }
  if (UsedPins & PinB5) { // Pin2 is high first
    Serial.println("PinB5");
  }
  delay(5000);// Delay for 5 seconds
  Serial.println("reset");
  UsedPins = 0;
}

Here is another solution. It is not quite as precise or elegant as zhomeslice's but may be a bit easier for a beginner to follow. If two buttons are pressed at exactly the same time, the winner is the one with the lower button number. It resets itself after 5 seconds so the game repeats.

untested:

const byte numberOfButtons = 4 ;
byte led[numberOfButtons ] = {8,9,10,11} ;  // led pins
byte button[numberOfButtons ] = {2,3,4,5} ;  // buttons

boolean winnerFound = false ;
unsigned long winnerFoundAtMs = 0 ;

void setup() {   
   for ( int i = 1 ; i < numberOfButtons ; i ++ ) {
      pinMode( button[ i ] , INPUT ) ;
      pinMode( led[ i ] , OUTPUT ) ;
   }
}

void loop() {
    for ( int i = 1 ; i < numberOfButtons ; i ++ ) {
        if ( digitalRead( button[i ] ) == HIGH && winnerFound == false ) {
            winnerFound = true ;
            winnerFoundAtMs = millis() ;
            digitalWrite ( led[ i ] , HIGH ) ;
        }
    }

    // reset everything after 5 seconds so the game repeats.
    if ( winnerFound == true && millis() - winnerFoundAtMs > 5000 ) {
        winnerFound = false ;
        for ( int i = 1 ; i < numberOfButtons ; i ++ ) {
           digitalWrite ( led[ i ] , LOW ) ;
        }
    }
}

What about using pinChange interrupts and have the ISR disable the interrupts until the code has had time to figure which button was pressed?

...R