locking mechanism not working as intended

Hi everyone. I've been working on a 4 button code lock mechanism for awhile now, and I've almost gotten things bug free(a big thanks to PaulS for that). I'm running into an issue with an array though. Here is my code I am working with:

#include <Button.h>

int release = 10; //call to release solenoid(green LED)
int lockout = 9; //incorrect code LED flasher (red LED)
int ledpin = 8; //LED that lights when any button is pressed
Button button1 = Button(1, PULLUP); //button for code
Button button2 = Button(2, PULLUP); //button for code
Button button3 = Button(3, PULLUP); //button for code
Button button4 = Button(4, PULLUP); //button for code
int c;

int code[] = {1, 2, 3, 4}; //This is the correct code

void setup()
{
pinMode(release, OUTPUT); //declaring the inputs and outputs
pinMode(lockout, OUTPUT);
pinMode(ledpin, OUTPUT);
}

void loop()
{
int i = 0; //create i to keep track of the array position
int query[] = {0, 0, 0, 0}; //initialize the button presses each time loop is ran
while (i <= 3) //while the number of button presses is less than 4
{
if(button1.uniquePress()) //if button one is pressed, light a signal led and put a 1 into array
{
digitalWrite(ledpin, HIGH);
delay(500);
query = 1;

  • i++; //advance the array to the next position*

  • digitalWrite(ledpin, LOW); *

  • }*

  • if(button2.uniquePress()) //if button two is pressed, light led and put a 2 into array*

  • {*

  • digitalWrite(ledpin, HIGH);*

  • delay(500);*
    _ query = 2;_
    * i++; //advance the array to the next position*
    * digitalWrite(ledpin, LOW);*
    * }*

* if(button3.uniquePress())*
* {*
* digitalWrite(ledpin, HIGH);*
* delay(500);*
_ query = 3;
* i++;
digitalWrite(ledpin, LOW);
}*_

* if(button4.uniquePress())*
* {*
* digitalWrite(ledpin, HIGH);*
* delay(500);*
_ query = 4;
* i++;
digitalWrite(ledpin, LOW);
}
}*_

* if (code[0] == query[0]) //test to see if one array matches the other*
* {*
* if (code[1] == query[1])*
* {*
* if (code[2] == query[2])*
* {*
* if (code[3] == query[3])*
* { //if it matches, release the solenoid for 5 seconds*
* digitalWrite(release, HIGH);*
* delay(5000);*
* digitalWrite(release, LOW);*
* delay(50);*
* }*
* }*
* }*
* }*
* else*
* { //otherwise flash a LED for an incorrect entry*
* digitalWrite(lockout, HIGH);*
* delay(500);*
* digitalWrite(lockout, LOW);*
* delay(500);*
* digitalWrite(lockout, HIGH);*
* delay(500);*
* digitalWrite(lockout, LOW);*
* delay(500);*

* }*
}
If I put in the correct code(1234), the green LED will light. if I put in an incorrect code of 4444, the red LED will blink. it is when I get into something like code(1232) or(1111) that I run into an error. Nothing will happen, and I can continue to press buttons for another 4 button presses(assuming i don't press 1232 or 1111 again). What is happening in my code that is making this happen?? Any help would be appreciated

Two things...

  1. Please put "code tags" around your code.

  2. Try this for the comparison...

 if ( (code[0] == query[0]) && (code[1] == query[1]) && (code[2] == query[2]) && (code[3] == query[3]) )
 {                     //if it matches, release the solenoid for 5 seconds
  digitalWrite(release, HIGH);
  delay(5000);
  digitalWrite(release, LOW);
  delay(50);
 }
 else
 {                           //otherwise flash a LED for an incorrect entry
   digitalWrite(lockout, HIGH);
   delay(500);
   digitalWrite(lockout, LOW);
   delay(500);
   digitalWrite(lockout, HIGH);
   delay(500);
   digitalWrite(lockout, LOW);
   delay(500);
 }

Thanks a lot Coding Badly, that did fix my problem. I just found out how to add in the Insert code, so all my future posts should have those. I don't quite understand why that snippit of code wasn't working correctly, but it's nice to have some resolution.

I don't quite understand why that snippit of code wasn't working correctly, but it's nice to have some resolution.

The code you posted checked the first value entered against the first value stored. If they did not match, the LED got flashed.

If they did match, you checked the second value entered against the second value stored. If they did not match, you did nothing.

If they did match, you checked the third value entered against the third value stored. If they did not match, you did nothing.

If they did match, you checked the fourth value entered against the fourth value stored. If they did not match, you did nothing.

If they did match, you unlocked the door.

Coding Badly changed the comparison so that any failure to match resulted in the else block being executed (to flash the LED).