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