if inside if statements

I've already read several questions but I can't find the answer to mine:

I wanna do a "passcode" that when you press B, C or D button nothing happens, except for "A" button. If you press A, the buzzer makes a sound to notifies that you are correct.

AFTER pressing A, if you press C or D button the "passcode" would be incorrect.

My problem is that when I press B without pressing A before, either it does nothing or notifies with the correct sound.

*Note: Im using Arduino Leonardo

void setup() { Serial.begin(9600); pinMode(2,INPUT_PULLUP); pinMode(3,INPUT_PULLUP); pinMode(4,INPUT_PULLUP); pinMode(5,INPUT_PULLUP); pinMode(6,INPUT_PULLUP); pinMode(7,INPUT_PULLUP); pinMode(A1,OUTPUT); pinMode(A2,OUTPUT); pinMode(A3,OUTPUT); pinMode(8,OUTPUT); pinMode(9,OUTPUT); pinMode(10,OUTPUT); pinMode(11,OUTPUT); pinMode(12,OUTPUT); pinMode(13,OUTPUT); pinMode(A0,OUTPUT); }

void loop() { int up = digitalRead(2); int down = digitalRead(3); int left = digitalRead(4); int right = digitalRead(5); int aa = digitalRead(6); int bb = digitalRead(7); int sensorPot = analogRead(A4);

if (up == LOW){ tone(0,alarm); }else{ noTone(0); } if (down == LOW){ tone(0,alarm); }else{ noTone(0); } if (left == LOW){ tone(0,alarm); }else{ noTone(0); } if (right == LOW){ tone(0,alarm); }else{ noTone(0); } if (aa == LOW){ tone(0,alarm); }else{ noTone(0); } if (bb == LOW){ tone(0,correct); delay(200); tone(0,tail); delay(300); if (up == LOW){ tone(0,correct); delay(200); tone(0,tail); delay(300);

} } }

I don't see any reference to buttons "A" or "B" or "C" or "D" in your program. How can you ever hope to have success without naming the variables you are referring to?

Paul

Please provide detailed wiring, explain what is connected for sound and how (resistor in line with the speaker?), and use code tags to post your code, after indenting it correctly (use ctrl-T in the IDE before copying)

I would not connect the buzzer on pin 0 and use Serial.begin(9600); which sets pin 0 as Rx (even if the tone call will probably set it as an output)

I bought an already-built arduino computer, the buttons, buzzer and all of that is already connected that way. I want the buzzer to make the correct sound when the "up" button is pressed and then, make the sound again if "bb" button is pressed AFTER pressing first up.

Paul_KD7HB: I don't see any reference to buttons "A" or "B" or "C" or "D" in your program. How can you ever hope to have success without naming the variables you are referring to?

Paul

You don't need those names bro, if you read the code you could see half of it is waking up pins, naming variables and the other half is the actual running. If I'm talking about ifs, you read the if part, piece of cake.

Then you realize 5 of 7 ifs are exactly the same so ignore them.

Without even attaching my code I was hoping for help, I think its enough with the initial explanation.

You obviously have the solutions well in hand!

Paul

Hey I’m yet a noob in here so I don’t know what to do in this case.

What I wanna do is kind of complex:

I wanna do a “passcode” whose order is “bb-up-down-left-right-aa” buttons.

I want the buzzer to make a “correct” sound when bb is pressed while making an “alarm” sound when any other button is pressed.

Then, if up button is pressed AFTER pressing bb, I want the buzzer to make the “correct” sound and no longer the “alarm” one.

I tried to make this code by using int a = 0 as a variable in void loop (dont know if it has to do with global variables)

And if bb is pressed then int a = a+1
Then if up is pressed int a = a+1
But if you press any other button in the wrong pattern/order as “bb-up-down-left-right-aa”, then a returns to 0 value.

I attach my code cause I can’t find the error.

*Note: Im using Arduino Leonardo, all the buttons, leds and buzzer are already connected as in the image (pre-built arduino computer)
**Note: Ignore the thing from serial.begin

void setup() {
Serial.begin(9600);
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
pinMode(5, INPUT_PULLUP);
pinMode(6, INPUT_PULLUP);
pinMode(7, INPUT_PULLUP);
pinMode(A1, OUTPUT);
pinMode(A2, OUTPUT);
pinMode(A3, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
pinMode(A0, OUTPUT);
}

void loop() {
int up = digitalRead(2);
int down = digitalRead(3);
int left = digitalRead(4);
int right = digitalRead(5);
int aa = digitalRead(6);
int bb = digitalRead(7);
int sensorPot = analogRead(A4);
static int a = 0;

if (a == 0 && up == LOW) {
tone(0, alarm);
} else {
noTone(0);
}
if (a == 0 && down == LOW) {
tone(0, alarm);
} else {
noTone(0);
}
if (a == 0 && left == LOW) {
tone(0, alarm);
} else {
noTone(0);
}
if (a == 0 && right == LOW) {
tone(0, alarm);
} else {
noTone(0);
}
if (a == 0 && aa == LOW) {
tone(0, alarm);
} else {
noTone(0);
}
if (a == 0 && bb == LOW) {//first passcode bb
tone(0, correct);
delay(200);
tone(0, tail);
delay(300);
int a = a + 1;
}
if (a == 1 && up == LOW) {//second passcode up
tone(0, correct);
delay(200);
tone(0, tail);
delay(300);
int a = a + 1;
} else {
noTone(0);
int a = 0;
}
if (a == 2 && down == LOW) {//third passcode down
tone(0, correct);
delay(200);
tone(0, tail);
delay(300);
int a = a + 1;
} else {
noTone(0);
int a = 0;
}
if (a == 3 && left == LOW) {//fourth passcode left
tone(0, correct);
delay(200);
tone(0, tail);
delay(300);
int a = a + 1;
} else {
noTone(0);
int a = 0;
}
if (a == 4 && right == LOW) {//fifth passcode right
tone(0, correct);
delay(200);
tone(0, tail);
delay(300);
int a = a + 1;
} else {
noTone(0);
int a = 0;
}
if (a == 5 && aa == LOW) {//sixth passcode aa
tone(0, correct);
delay(200);
tone(0, tail);
delay(300);
int a = 0;
}
}

For your consideration: search ‘combination lock’ on this site. This problem has appeared many times.

Please use code tags </> when posting code and format with t to show indents.

dougp:
For your consideration: search ‘combination lock’ on this site. This problem has appeared many times.

Please use code tags </> when posting code and format with t to show indents.

I actually used ctr+t
What are code tags??

One problem you are going to have is that holding a button down for more than a tiny fraction of a second will cause the combination to fail because you have no code to distinguish between a button that has just been pushed and a button that is still down from the last push.

Look at the example 02.Digital->StateChangeDetection. In loop() you should check for new key presses. You should also assign a unique value to each button (like a key code) so you can use a list of the key codes as the combination (see below).

Rather than writing a bunch of nearly identical 'if' statements for each character of the combination you can compare the current button press to the key code for the current combination step. It either matches or it doesn't.

In the upper left corner of the edit box (above the smilies) is the symbol I used, just click on it and paste your code inside. The reason is that certain character sequences are interpreted by the forum software as smilies. Code tags prevent that.

johnwasser: One problem you are going to have is that holding a button down for more than a tiny fraction of a second will cause the combination to fail because you have no code to distinguish between a button that has just been pushed and a button that is still down from the last push.

Look at the example 02.Digital->StateChangeDetection. In loop() you should check for new key presses. You should also assign a unique value to each button (like a key code) so you can use a list of the key codes as the combination (see below).

Rather than writing a bunch of nearly identical 'if' statements for each character of the combination you can compare the current button press to the key code for the current combination step. It either matches or it doesn't.

I think you're right when you say that pressing a button for more than a fraction of a second will cause the passcode to fail BUT I think i got saved by the delays of the correct sound, they last enough time for you to unpress the button :)

@Artpunk00, do not cross-post. Threads merged.

Paul_KD7HB: You obviously have the solutions well in hand!

Paul

+1

...R

Artpunk00: I actually used ctr+t What are code tags??

1 good point, 1 bad question

There are a few posts pinned at the top of the forum, including two titled How to use this forum - please read. Read this before posting a programming question ...

I'm always puzzled to see posters would not have the decency to spend 5 minutes there... please do your homework before next post and fix all your existing posts accordingly

J-M-L:
1 good point, 1 bad question

There are a few posts pinned at the top of the forum, including two titled
How to use this forum - please read.
Read this before posting a programming question …

I’m always puzzled to see posters would not have the decency to spend 5 minutes there… please do your homework before next post and fix all your existing posts accordingly

Don’t worry, I won’t :wink:

:o