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 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.
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 :-
you are too stupid to understand what this link says and therefore you will not understand any reply we give you.
or
you are too arrogant or lazy to comply with this and therefore you don't deserve any help.
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.
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 ( ^^ )