Hello everyone,
I created 6 push button combination lock. Everything is working but now i want to change the pass-code to for example 4 digit . But when i change the pass-code to 4 digit then first i have to press all the six button after that output is generated.
Even if there are six buttons but i want the pass-code to be minimum 1 digit and maximum 6 digits.
I tried to change the pass-code for the button which are not required to be press to '0' but still i have to press all six buttons.
How can i set the 'zero' value in pass-code to be considered as LOW input.
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const int button1 = 6; //first button is on pin 6
const int button2 = 7; //second is on pin 7
const int button3 = 8; //third is pin 8
const int button4 = 9; //fourth is pin 9
const int button5 = 10; //third is pin 10
const int button6 = 13; //fourth is pin 13
void checkEntered1(int button);
int code[] = {1,1,2,2,2,2}; //the desired code is entered in this array,
//I WANT ABILITY TO CHANGE ABOVE PASS-CODE to any number of digits.. MAXIMUM 6.
int entered[7]; //create a new empty array for the code entered by
//the user
void setup(){ //run once at sketch startup
pinMode(button1, INPUT_PULLUP); //button 1 is an input
pinMode(button2, INPUT_PULLUP); //button 2 is an input
pinMode(button3, INPUT_PULLUP); //button 3 is an input
pinMode(button4, INPUT_PULLUP); //button 4 is an input
pinMode(button5, INPUT_PULLUP); //button 5 is an input
pinMode(button6, INPUT_PULLUP); //button 6 is an input
lcd.begin(16, 2);
//lcd.print("Enter Pass");
}
void loop(){ //run repeatedly
if (digitalRead(button1) == HIGH){ //if button1 is pressed
checkEntered1(1); //call checkEntered and pass it a 1
delay(250);//wait, needed for correct functioning, otherwise
//buttons are deemed to be pressed more than once
}
else if (digitalRead(button2) == HIGH){ //if button2 is pressed
checkEntered1(2); //call checkEntered1 and pass it a 2
delay(250); //wait
}
else if (digitalRead(button3) == HIGH){ //if button3 is pressed
checkEntered1(3); //call checkEntered1 and pass it a 3
delay(250); //wait
}
else if (digitalRead(button4) == HIGH){ //if button4 is pressed
checkEntered1(4); //call checkEntered1 and pass it a 4
delay(250); //wait
}
else if (digitalRead(button5) == HIGH){ //if button4 is pressed
checkEntered1(5); //call checkEntered1 and pass it a 4
delay(250); //wait
}
else if (digitalRead(button6) == HIGH){ //if button4 is pressed
checkEntered1(6); //call checkEntered1 and pass it a 4
delay(250); //wait
}
}
void checkEntered1(int button){ //check the first element of the entered[] array
if (entered[0] != 0){ //if it is not a zero, i.e. it has already been inputted
checkEntered2(button); //move on to checkEntered2, passing it "button"
}
else if(entered[0] == 0){ //if it is zero, i.e. if it hasn't been defined with a button yet
entered[0] = button; //set the first element as the button that has been pressed
}
}
void checkEntered2(int button){ //check the second element of the entered[] array
if (entered[1] != 0){ //if it is not a zero, i.e. it has already been inputted
checkEntered3(button); //move on to checkEntered3, passing it "button"
}
else if(entered[1] == 0){ //if it is zero, i.e. if it hasn't been defined with a button yet
entered[1] = button; //set the second element as the button that has been pressed
}
}
void checkEntered3(int button){ //check the third element of the entered[] array
if (entered[2] != 0){ //if it is not a zero, i.e. it has already been inputted
checkEntered4(button); //move on to checkEntered4, passing it "button"
}
else if (entered[2] == 0){ //if it is zero, i.e. if it hasn't been defined with a button yet
entered[2] = button; //set the third element as the button that has been pressed
}
}
void checkEntered4(int button){ //check the third element of the entered[] array
if (entered[3] != 0){ //if it is not a zero, i.e. it has already been inputted
checkEntered5(button); //move on to checkEntered4, passing it "button"
}
else if (entered[3] == 0){ //if it is zero, i.e. if it hasn't been defined with a button yet
entered[3] = button; //set the third element as the button that has been pressed
Serial.print("4: ");Serial.println(entered[3]); //for debugging
}
}
void checkEntered5(int button){ //check the third element of the entered[] array
if (entered[4] != 0){ //if it is not a zero, i.e. it has already been inputted
checkEntered6(button); //move on to checkEntered4, passing it "button"
}
else if (entered[4] == 0){ //if it is zero, i.e. if it hasn't been defined with a button yet
entered[4] = button; //set the third element as the button that has been pressed
Serial.print("5: ");Serial.println(entered[4]); //for debugging
}
}
void checkEntered6(int button){ //check the fourth element of the entered[] array
if (entered[5] == 0){ //if it is zero, i.e. if it hasn't been defined with a button yet
entered[5] = button; //set the final element as the button that has been pressed
Serial.print("6: ");Serial.println(entered[5]); //for debugging
delay(100); //allow time for processing
compareCode(); //call the compareCode function
}
}
void compareCode(){ //checks if the code entered is correct by comparing the code[] array with the entered[] array
for (int i = 0; i<6;i++){ //these three lines are for debugging
Serial.println(entered[i]);
}
if ((entered[0]==code[0])&& (entered[2]==code[2]) && (entered[3]==code[3]) && (entered[4]==code[4])&& (entered[5]==code[5])){ //if all the elements of each array are equal
lcd.print("correct");
delay(1000); //wait for a bit
for (int i = 0; i < 7; i++){ //array to zero
entered[i] = 0;
}
lcd.clear();
//loop(); //return to loop() (not really necessary)
}
else { //if you get the code wrong
lcd.print("wrong");
delay(1000);
for (int i = 0; i < 7; i++){ //this next loop is for debugging
entered[i] = 0;
}
lcd.clear();
}
//close_all();
}