Hi one and all this is my first post and fairly new to the world of arduino and look forward to any guidance and help offered in aquiring knolledge on this exiting platform , and any help is welcome. ill start with a brief outline of what im trying to achieve and highlight the problem im encountering
I have managed to interface an 3x4 keypad to arduino , by taking inspiration in some respects and lots of reading and youtubing so far so apoligies if comments and code is not layed out in an easiy digestableway.
My problem is trying to encorporate a password into this program and , i have tried numerous things , and the only sucess that i have has is using 2 arrays one for userinput and one for password along with a keystate variable 'keypress' and a index counting variable for both . i have tried comparing these and have managed to get the unlock stage done but the lock stage is not. i also want the hash to be used to verify the correct code and star to reset the lock. I understand my code may be inefficeint and welcome any constructive critsism in aid to bring this project closer to its finish ,i also need the welcome message "enter password " to reconvine after it has been locked , reseting to orginal state ready for input.i think that is about it for the moment , thanks in advance
////////////////////////////////////////////////////
///////////Global variable declaration/////////////
//////////////////////////////////////////////////
int rw1 = 7; //row1
int rw2 = 2; //row2
int rw3 = 3; //row3
int rw4 = 5; //row4
int cl1 = 6; //collumn1
int cl2 = 8; //collumn2
int cl3 = 4; //colluumn3
int col_var_1;
int col_var_2; //column variables
int col_var_3;
int global_db_delay=200; ///global delay variable
int keyid=0; //key identifier
int keypress=0; //key state
int userinput[4]; //user keystrokes
int password[4]= {1,2,3,4}; //password array
int usr_in_count=0; //index variable for password array
//////////////////////////////////////////////////
///////////Setup and initialization//////////////
////////////////////////////////////////////////
///////////////////////////////////////////////
void setup(){
String wm="Enter password";
Serial.begin(9600); //baud rate for serial monitor
pinMode(rw1, OUTPUT);
pinMode(rw2, OUTPUT);
pinMode(rw3, OUTPUT);
pinMode(rw4, OUTPUT); ///////set pinmode parameters for rows and cols
pinMode(cl1, INPUT);
pinMode(cl2, INPUT);
pinMode(cl3, INPUT);
digitalWrite(cl1, HIGH);
digitalWrite(cl2, HIGH); //input pins pulled high -will read high unless buttons are pressed
digitalWrite(cl3, HIGH);
Serial.println(wm);
}
//////////////////////////////////////////////
//////////////////Main Program///////////////
////////////////////////////////////////////
void loop(){
//////////////////////////////////
///////////ROW1 SCAN//////////////
//////////////////////////////////
digitalWrite(rw1, LOW);
digitalWrite(rw2, HIGH); //row is selected by making the output of that row low - with the input pulled high -when the button is pressed it will pull that input low
digitalWrite(rw3, HIGH); //all other rows pulled high are ignored
digitalWrite(rw4, HIGH);
col_var_1 = digitalRead(cl1);
col_var_2 = digitalRead(cl2);
col_var_3= digitalRead(cl3);
if (col_var_1 == LOW){
keypress=1;
keyid=1;
Serial.println("1");
delay(global_db_delay);
}
else{
if (col_var_2 == LOW){
keypress=1;
keyid=2;
Serial.println("2");
delay(200);
}
else{
if (col_var_3== LOW){
keypress=1;
keyid=3;
Serial.println("3");
delay(global_db_delay);
}
}
}
//////////////////////////////////
///////////ROW2 SCAN//////////////
//////////////////////////////////
digitalWrite(rw1, HIGH);
digitalWrite(rw2, LOW);
digitalWrite(rw3, HIGH);
digitalWrite(rw4, HIGH);
col_var_1 = digitalRead(cl1);
col_var_2 = digitalRead(cl2);
col_var_3= digitalRead(cl3);
if (col_var_1== LOW){
keypress=1;
keyid=4;
Serial.println("4");
delay(global_db_delay);
}
else{
if (col_var_2== LOW){
keypress=1;
keyid=5;
Serial.println("5");
delay(global_db_delay);
}
else{
if (col_var_3== LOW){
keypress=1;
keyid=6;
Serial.println("6");
delay(global_db_delay);
}
}
}
//////////////////////////////////
///////////ROW3 SCAN//////////////
//////////////////////////////////
digitalWrite(rw1, HIGH);
digitalWrite(rw2, HIGH);
digitalWrite(rw3, LOW);
digitalWrite(rw4, HIGH);
col_var_1 = digitalRead(cl1);
col_var_2= digitalRead(cl2);
col_var_3= digitalRead(cl3);
if (col_var_1== LOW){
keypress=1;
keyid=7;
Serial.println("7");
delay(global_db_delay);
}
else{
if (col_var_2 == LOW){
keypress=1;
keyid=8;
Serial.println("8");
delay(global_db_delay);
}
else{
if (col_var_3== LOW){
keypress=1;
keyid=9;
Serial.println("9");
delay(global_db_delay);
}
}
}
///////////////////////////////////
//////////////////////////////////
digitalWrite(rw1, HIGH);
digitalWrite(rw2, HIGH);
digitalWrite(rw3, HIGH);
digitalWrite(rw4, LOW);
col_var_1 = digitalRead(cl1);
col_var_2= digitalRead(cl2);
col_var_3= digitalRead(cl3);
if (col_var_1== LOW){
keypress=1;
keyid=10;
Serial.println("*");
delay(global_db_delay);
}
else{
if (col_var_2 == LOW){
keypress=1;
keyid=0;
Serial.println("0");
delay(global_db_delay);
}
else{
if (col_var_3== LOW){
keypress=1;
keyid=12;
Serial.println("#");
delay(global_db_delay);
}
}
}
if(userinput[0]==password[0] && userinput[1]==password[1] &&
userinput[2]==password[2] && userinput[3]==password[3]){
Serial.println("UNLOCKED");
userinput[0]=0;
userinput[1]=0;
userinput[2]=0; //reset user input,index variable and ,keypress'keystate'
userinput[3]=0;
usr_in_count=0;
keypress=0;
}
if(keyid==10){
usr_in_count=0;
userinput[0]=0;
userinput[1]=0;
userinput[2]=0;
userinput[3]=0;
usr_in_count=0;
keypress=0;
Serial.println("locked");
}
if(keypress==1){
userinput[usr_in_count]=keyid;
usr_in_count=usr_in_count+1;
keypress=0;
}
}
///////////////////////////////////////
///////////End of key scan/////////////
//////////////////////////////////////