correct sequence of buttons activate LED

Hi everyone! I would like to ask you for some help. I have written an Arduino code for button number sequence. What I want: If you press the correct sequence of buttons, the LED array gets activated.
What I get: no mather the sequence, LED is turned ON, when all buttons are pressed

int B_1 = 2; 
int B_2 = 3; 
int B_3 = 4;
int LED = A0;
int button1 = 0;      
int button2 = 0;         
int button3 = 0;   

void setup() {
pinMode(B_1,INPUT);
pinMode(B_2,INPUT);
pinMode(B_3,INPUT);
pinMode(LED,OUTPUT);
}

void loop() {
button1 = digitalRead(B_1);
button2 = digitalRead(B_2);
button3 = digitalRead(B_3);

    if (button1== HIGH){
      if (button2 == HIGH){
        if (button3 == HIGH){
            digitalWrite(LED, HIGH);
       }
      }
    }     
}

Please modify your post above and add the code tags. This is a forum rule!

Please explain in more detail what you want to achieve. Must all 3 buttons be pressed at the same time to activate the LEDs? Or should each button be pressed and released before the next button in the sequence is pressed?

Before posing questions here and indeed if you are in the process of experimenting, it would be a good idea to do a lot of reading through other posts regarding your topic on this forum as well as "Project Guidance" and "General Electronics".

So if you are using buttons, you need to learn how to wire buttons appropriately between a pin and ground, using INPUT_PULLUP, how to distinguish when a button is first pressed, how to de-bounce buttons and how to time actions,

While we can perhaps guide you through this process, it has all been explained before many times over and if you do the proper research you can get a much deeper understanding of what is involved. :grinning:

The code implies you have not wired the buttons correctly. See here for how to do it right, as well as the theory of what you are doing.
http://www.thebox.myzen.co.uk/Tutorial/Inputs.html

For how to deal with the buttons see:-

Note this tutorial wires up the buttons in a none optimal way ( wrong ). But a lot of Arduino tutorials on the whole of the web are wrong.

Grumpy_Mike:
But a lot of Arduino tutorials on the whole of the web are wrong.

Amen to that! :roll_eyes:

Thank you all for help.

@PaulRB all 3 buttons must be pressed and stayed pressed (I have self lock switch) in the correct sequence. example:

  1. button1 = pressed,
  2. button2 = pressed
  3. button3 = pressed
  4. Led = turned on

@Paul__B my buttons are wired exactly like in second link...

I imagine the code like this:
if I satisfy the first condition (button1 = high), the second condition (button2 = high) is read, and so on.

Reminder:

PaulRB:
Please modify your post above and add the code tags. This is a forum rule!

all 3 buttons must be pressed and stayed pressed (I have self lock switch) in the correct sequence

Then you need a state machine, with a state variable.

When you see no button pressed the state variable should be set to zero.

Then when the first button in your sequence is pressed, and no other buttons are pressed the state variable should be set to 1.

When the state variable is 1, and the second button is pressed and the first button continues to be pressed and the third button is unpressed then the state variable can be set to 2, otherwise the state variable is reset to zero.

Finally when the third button is pressed and the other two remain pressed the state variable can be set to 3.

When you see the state variable set to 3 you can then unlock your door.


You won't win many friends here if you ignore the advice to:-

Please modify your post above and add the code tags. This is a forum rule!

If you don't know how to do the please read this:-
How to use this forum
If you continue to ignore this request we will conclude that either :-

  1. you are too stupid to understand what this link says and therefore you will not understand any reply we give you.
    or
  2. you are too arrogant or lazy to comply with this and therefore you don't deserve any help.

The choice is yours.

Thank you for answer... you mean something like that ? LED is not turned ON...

int B_1 = 2; 
int B_2 = 3; 
int B_3 = 4;

int LED = A0;

int button1 = 0;      
int button2 = 0;         
int button3 = 0;    

int state = 0;
   
void setup() {
pinMode(B_1,INPUT);
pinMode(B_2,INPUT);
pinMode(B_3,INPUT);
pinMode(LED,OUTPUT);
}

void loop() {
button1 = digitalRead(B_1);
button2 = digitalRead(B_2);
button3 = digitalRead(B_3);

switch(state)
{
  case 0:
    if (button1 == LOW && button2 == LOW && button3 == LOW ){
      state = 0; }
  break;

  case 1:
    if (button1 == HIGH && button2 == LOW && button3 == LOW ){
      state = 1; }
    else {
      state = 0; }
   break;
      
  case 2:
    if (state == 1; button1 == HIGH && button2 == HIGH && button3 == LOW){
      state = 2; }
    else {
      state = 0; }
  break;

   case 3:
    if (state == 2; button1 == HIGH && button2 == HIGH && button3 == HIGH){
      state = 3; }     
   break;
}

if (state == 3) { digitalWrite(LED, HIGH); }
if (state == 0) { digitalWrite(LED, LOW); }
}

Sorry about posting a code wrong way.. I am new on this forum.. postis already fixed :slight_smile:

Thanks for fixing those code tags. +1 karma

The problem is your code is never moving the state upwards, to the next state. If the state is 0, it cannot become 1, if it is 1, it cannot become 2 and so on.

In each state, there are 3 possible outcomes, which depend on the readings from the buttons.

  1. The state remains unchanged
  2. The state progresses to the next state
  3. The state falls back to zero.

Yes can yo see that

case 2:
    if (state == 1; button1 == HIGH && button2 == HIGH && button3 == LOW){
      state = 2; }

Is stuck.

As the case is what current state value is, then only when it is equal to 2 does that if statement get executed. But the first thing it checks is if the state is equal to one. So that if will always fail. Incidentally there should not be a comma after this but a logic Boolean operator like AND ( && ) inclusive OR ( || ) or an exclusive OR ( ^^ )

Thank you all. I made it :slight_smile:

Show your appreciation by posting your fixed code, so that others with a similar problem in the future can find it.