I have been working on fixing this issue for a while but no luck. I am doing this project with Arduino Mega 2560 and a keypad 4X4 where you have already a password exist in the system, the password as it seen below is ABC, and then I thought of adding a feature where a user can create his/her own password. I approach it by first asking to type the original password that has been written already, ABC, and then if a user presses a '*' and release his finger of the button it goes to another function where it asks to reset the password and create a new one. So if the user presses '1' it do something and if '0' do another thing. The thing is that it does not print out on serial monitor the key that has been pressed. It acts so weird maybe not maybe I am missing a fundamental basis. I even I asked a friend who is a computer engineering and only found that the key that I pressed translated into a space in other words in no key is pressed.
I know that there is a password library exists which really cool but I am trying to do same basic coding so I learn by myself how to code.
Q) After looking at my code, did I miss something might be necessary I should have included ? if it is not, then what is it
here is my code:
//Backspace code:
//else if(key == 'D') {
// --k;
// UL[k] = 0;
// Serial.print(UL[k]);
//
// }
// There are also PRESS,HOLD and RELEASE functions
//===================================
#include <Keypad.h>
unsigned long t_hold;
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
int k = 0;
int l = 0;
int c = 0;
int b = 0;
char UL[3];
char passWord[4] ={'A','B','C'};
char chanPass[4];
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
//Columns
//The first 4 pins from the left are columns: Col1,Col2,Col3 and Col4.
//col1 is connected to 9,col2 is 8, col3 is 7 and col4 is 6 in the Arduino.
//Rows
//The rest of the four pins are rows:row1,row2, row3 and row4.
//row1 is 5, row2 is 4, row3 is 3 and row4 is 2.
//Keypad
// 1 2 3 A
// 4 5 6 B
// 7 8 9 C
// * 0 # D
// Keypad :header1 header2 header3 header4 header5 header6 header7 header8
// confiugration :column1 column2 column3 column4 row1 row2 row3 row4
// Ardunio :pin 9 pin 8 pin 7 pin 6 pin 5 pin 4 pin 3 pin 2
byte rowPins[ROWS] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {9, 8, 7, 6}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup(){
Serial.begin(9600);
//Serial.println("=================================");
//Serial.println("Use three * to change password");
//Serial.println("=================================");
Serial.println("Please enter password: ");
keypad.addEventListener(keypadEvent); //add an event listener for this keypad
keypad.setHoldTime(2000); //hold time the default is one second
}
void changPass(){
char key = keypad.getKey();
Serial.println(" ");
Serial.println("Changing password?");
Serial.println("Please type '1' for Yes or '0' for No to continue");
while (1){
if (key != NO_KEY){
Serial.println("I'm here men");
chanPass[b] = key;
b++;
Serial.println(key);
if (chanPass[b] == '0'){
Serial.println("Don't worry! you will get back to password mode in bit");
delay(1000);
loop();
}
else if (chanPass[b] == '1'){
Serial.println("Tyep your new password:");
Serial.println(" ");
chanPass[c] = key;
c++;
if (c == 3){
Serial.println(" You just changed your password");
Serial.println("Are you stasified with the new password");
Serial.println("'1' for yes and '0'for no?");
if (key == '0'){
Serial.println("ok! lets' start over!");
changPass();
}
else if (key == '1'){
Serial.println("Give me a minute to save the new password");
for(c=0; c<= 3; c++){
UL[c] = chanPass[c];
}
Serial.println("Done! Your new password is:");
Serial.print( UL[0] );
Serial.print( UL[1] );
Serial.print( UL[2] );
}
}
}
}
}
}
void loop(){
char key = keypad.getKey();
if (key != NO_KEY ){
UL[k] = key;
key = '*';
Serial.print(key);
k++;
// compare the pass word and string of UL[]
if (k == 3 && UL[0] == passWord[0] && UL[1] == passWord[1] && UL[2] == passWord[2]){
k =0;
Serial.println(" ");
Serial.println("Password is correct!");
Serial.println("********************");
Serial.println("Please enter password: ");
}
if(k == 3){
k =0;
Serial.println(" ");
Serial.println("password is wrong!");
Serial.println("********************");
Serial.println("Please enter password:");
}
}
}
//take care of some special events
void keypadEvent(KeypadEvent key){
switch (keypad.getState()){
case PRESSED:
// switch (key){
// case '#': break;
// case '*':
break;
case RELEASED:
switch (key){
case '*': changPass();
break;
}
case HOLD:
// switch (key){
// case '*':
// break;
//}
break;
}
}