2 buttons, simultaneous press combination, 1 LED output ON/OFF

Hello everyone!
I'm experimenting with a new project which seemed easy at the beginning but it is expanding into something more complex. That's why I need help from this lovely community.

The aim is to have eight buttons connected to an Arduino Micro board, two of which when pressed simultaneously, turn the LED light ON.
When the two buttons are pressed again, the LED light goes OFF.

Now the complex bit: I don't how to latch the LED until the buttons are pressed again.
Could you please let me know how to sort this out?

Much appreciated

You are describing "state change detection."

  • know when two buttons are pressed
  • know which state the LED is in
  • change the state of the LED

If you want to do some reading, here is an Arduino page describing it, with code.

presumably the register controlling the LED holds it's state until changed.

recognizing that two buttons are pressed should only change the state of the LED when they both become pressed not while they are being pressed and a subsequent detection shouldn't occur until after recognizing that both buttons are no longer being pressed

and since mechanical button bounce, after a change in the state of either button, subsequent changes should be ignored momentarily ( ~20msec)

look this over

const byte PinBut1 = A1;
const byte PinBut2 = A2;
const byte PinLed  = LED_BUILTIN;

byte butState;

// -----------------------------------------------------------------------------
void
loop ()
{
    byte but = digitalRead (PinBut1) | digitalRead (PinBut2);

    if (butState != but)  {
        butState = but;
        delay (20);

        if (LOW == butState)
            digitalWrite (PinLed, ! digitalRead (PinLed));
    }
}

void
setup  ()
{
    Serial.begin (9600);

    pinMode (PinLed,  OUTPUT);
    pinMode (PinBut1, INPUT_PULLUP);
    pinMode (PinBut2, INPUT_PULLUP);

    butState = digitalRead (PinBut1) | digitalRead (PinBut2);
}

Hello emanuino

I`m curious.

What is the task of this programme in real life?

Hi guys, thanks a lot for your hints. I'll do some tests and let you know.
@paulpaulson your question brings my project to another question.
The device is a control surface for sound recording, but will Arduino respond to two different sketches? In the original sketch that I'm already using, the two encoders are already being used for a specific task (gain control). I wonder if the same encoder would trigger the LED if I integrate the original sketch with the LED control.
Thanks a lot!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.