I basically have two switches Both switches will be pressed simultaneously in different order I want to know which switch was pressed first

  • Please include in your question a description of what exactly is your problem with doing that. You can edit your question (this information belongs into the question, not in the comments)
    – chrisl
    Feb 19, 2021 at 9:53
  • 1
    simultaneously and different order are mutually exclusive ... you either press them at the same time, or you press them sequentially ... you can't do both
    – jsotola
    Feb 19, 2021 at 17:41

2 Answers 2


Use External Interrupts is the short answer.

Longer version:

You probably want to make a buzzer system like what in family feud game show. I assume you are using Arduino UNO/MEGA and or having experience with programming.

1/ Use INT0 and INT1 for button 1 and button 2.

When an interrupt is raised, the mcu jumps to a specific address to run the interrupt service routine. INT0 and INT1 vectors are pointing to two distinctive addresses to run 2 different functions. For this, mcu takes the least amount of CPU clock to capture which button press first.

2/ Use PCINT vectors which is PinChange Interrupt. But personally I prefer to call it PortChange Interrupt.

There are 3 PinChange interrupt vectors PCINT0, PCINT1, PCINT2 to run 3 distinctive service routines. But this kind of interrupt doesn't know which pin is changed.

So inside the interrupt service routine, you have to check for that specific pin. You can do this the arduino way digitalRead(button1) or capture the whole port register, ie. PORTB, PORTC or PORTD.

In a nutshell, PORTB holds values of digital pin 8 to 13 while port D hold values of digital pin 0 to 7 and PORC for A0 to A7.

You can shave off some CPU clocks (3-10 clocks probably) by put 2 buttons different PCINT vectors. So that each button still triggers distinctive function.

In 3 cpu clocks (or around 180 ns) a lot of things may change and you may not get the correct result.

3/ Polling in main loop() This is the worst way to do it. Nevertheless, it still gives good result.

if (digitalRead(button1)==LOW) {
// do something

if (digitalRead(button2)==LOW) {
// do something else
  1. The simple version: check continuously for the state of the digital inputs the switches are connected to. When one is HIGH that is the switched pressed first.
  2. The intermediate version: check the 'blink without delay' example to make sure you don't get too many false positives.
  3. The advanced version: use interrupts when one of the two switches gets 'HIGH', using the 'blink without delay' to prevent false positives.

If you want to know if both are pressed at the same time, but one is pressed first, use variables to keep track on it.

like two variables:

bool button1Pressed = false;
bool button2Pressed = false;

Set them accordingly (depending on which of the 3 possibilities you use). Then when you know a button is changed, you can easily check the booleans to know which was already pressed.

Or you can use an enum that combines the state of both:


EState buttonState;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.