3 push button for one action

Hi Guys, I try to find something for my idea but not lucky yet.
I want 3 push button, for one LED. So you have to hit every push button before LED goes on.
One button more for LED goes off.
is it simple? i have no idea how to approach it.

thanks for your help! :slight_smile:

Look into the example codes in the IDE. Start playing with button code, learning how to manage them. Then look for LED exercising code...

Does the order of button presses matter?

no it has to be randomly

Even before you write any code you need to think through what might happen.

Say someone pushes button 2.
What do you do?
Let's set a flag saying that 2 was pressed.

What can happen next?

  1. They push button 2 again
  2. They push button 1
  3. They push button 3

So what do you do for the three different cases?

  1. Do nothing
  2. Set a flag for button 1
  3. Set a flag for button 3

.......

Now we got to the point where all 3 flags are set. Now what?

Think of the logic behind what you want to do. You have three things that can only be used once, and when they are all gone, something happens.

Like you had 3 coins in a bag and you take out one at a time. When the bag is empty, you do something. Now when you attempt to remove a coin from the empty bag, then you do a different thing.

So, in programming terms. We have 3 switch identifiers in a container (let's say it's an array). When a switch is pressed, we set its identifier in the array to -1. When the array has only -1's in it, then you turn on the LED. Now, when a switch is pressed, you see that the container is full of -1's, so you turn off the LED.

Turning that logic into code is left as an exercise for the student.

want to every button remeber his status. So if button 2 is hit and it gets hit again i want to do nothing. When every button is pressed then the LED turn on. And if i hit 4 times button 2, dont care.

You have the idea
You should look at the code examples in the IDE, specifically the Button and Blink examples.
Here is a code snippit to get you started

button1 = digitalRead(button1_Pin);

if (button1 == LOW) // It is pressed
{
  button1_pressed = true;
}


if (button1_pressed == true) && (button2_pressed == true) && (button3_pressed == true)
{
  // Light the LED
}

OP has disappeared and may never be seen again, but just in case.
Try this. I think it would work.

const int BUTTON1_PIN = 2;
const int BUTTON2_PIN = 3;
const int BUTTON3_PIN = 4;

const int buttons[] = {BUTTON1_PIN, BUTTON2_PIN, BUTTON3_PIN};
int buttonState[3];

void setup() 
{
    for (int i = 0; i < 3; i++)
    {
        pinMode(buttons[i], INPUT_PULLUP);
        buttonState[i] = buttons[i];
    }
    pinMode(LED_BUILTIN, OUTPUT);
    digitalWrite(LED_BUILTIN, LOW);
}

void loop() 
{
    bool ledState = false;
    delay(100);

    int buttonCount = 0;
    for (int i = 0; i < 3; i++)
    {
        // Check if button pressed
        if((digitalRead(buttons[i]) == LOW) && (buttonState[i] != -1))
        {
            buttonState[i] = -1;
            buttonCount++;
        }
    }

    if ((ledState == false) && (buttonCount == 3))
    {
        // All buttons were pressed
        digitalWrite(LED_BUILTIN, HIGH);
    }

    if ((ledState == true) &&(buttonCount > 0))
    {       
       // Button pressed after LED on
       digitalWrite(LED_BUILTIN, LOW);
       while(1);
    }
}

I never noticed that "Copy for forum" menu selection before this!

i am here haha. Sounds well thank you! just for my amateur knowledge, how would you wire this idea up?

Pushbuttons between pins 2,3,4 & ground.

but where is the LED?

This LED is on your Arduino board.

can you put a real LED in? itΒ΄s not working for me in the simulation program

const int BUTTON1_PIN = 2;
const int BUTTON2_PIN = 3;
const int BUTTON3_PIN = 4;
const int LED_PIN = 5;

const int buttons[] = {BUTTON1_PIN, BUTTON2_PIN, BUTTON3_PIN};
int buttonState[3];

void setup() 
{
    for (int i = 0; i < 3; i++)
    {
        pinMode(buttons[i], INPUT_PULLUP);
        buttonState[i] = buttons[i];
    }
    pinMode(LED_PIN, OUTPUT);
    digitalWrite(LED_PIN LOW);
}

void loop() 
{
    bool ledState = false;
    delay(100);

    int buttonCount = 0;
    for (int i = 0; i < 3; i++)
    {
        // Check if button pressed
        if((digitalRead(buttons[i]) == LOW) && (buttonState[i] != -1))
        {
            buttonState[i] = -1;
            buttonCount++;
        }
    }

    if ((ledState == false) && (buttonCount == 3))
    {
        // All buttons were pressed
        digitalWrite(LED_PIN, HIGH);
    }

    if ((ledState == true) &&(buttonCount > 0))
    {       
       // Button pressed after LED on
       digitalWrite(LED_PIN, LOW);
       while(1);
    }
}

thank you so much. But error with this code

Build failed!
In file included from sketch.ino.cpp:1:0:
sketch.ino: In function 'void setup()':
/arduino/hardware/avr/1.8.6/cores/arduino/Arduino.h:41:14: error: expected ')' before numeric constant
#define LOW 0x0
^
sketch.ino:17:26: note: in expansion of macro 'LOW'
digitalWrite(LED_PIN LOW);
^~~
sketch.ino:17:29: error: too few arguments to function 'void digitalWrite(uint8_t, uint8_t)'
digitalWrite(LED_PIN LOW);
^
In file included from sketch.ino.cpp:1:0:
/arduino/hardware/avr/1.8.6/cores/arduino/Arduino.h:135:6: note: declared here
void digitalWrite(uint8_t pin, uint8_t val);
^~~~~~~~~~~~

Error during build: exit status 1

const int BUTTON1_PIN = 2;
const int BUTTON2_PIN = 3;
const int BUTTON3_PIN = 4;
const int LED_PIN = 5;

const int buttons[] = {BUTTON1_PIN, BUTTON2_PIN, BUTTON3_PIN};
int buttonState[3];

void setup() 
{
    for (int i = 0; i < 3; i++)
    {
        pinMode(buttons[i], INPUT_PULLUP);
        buttonState[i] = buttons[i];
    }
    pinMode(LED_PIN, OUTPUT);
    digitalWrite(LED_PIN, LOW);
}

void loop() 
{
    bool ledState = false;
    delay(100);

    int buttonCount = 0;
    for (int i = 0; i < 3; i++)
    {
        // Check if button pressed
        if((digitalRead(buttons[i]) == LOW) && (buttonState[i] != -1))
        {
            buttonState[i] = -1;
            buttonCount++;
        }
    }

    if ((ledState == false) && (buttonCount == 3))
    {
        // All buttons were pressed
        digitalWrite(LED_PIN, HIGH);
    }

    if ((ledState == true) &&(buttonCount > 0))
    {       
       // Button pressed after LED on
       digitalWrite(LED_PIN, LOW);
       while(1);
    }
}

dont work..is it my wiring?

You have the buttons wired backward from the way @ cedarlakeinstruments imagined, replace the INPUT_PULLUPs with INPUT.