Hello guys, this is my second post here. I'm working on a project right now, a system that will accept a 4 digit pass, will turn on a piece of equipment, user will have the option to change the password. My code right now looks like this:
#include <Keypad_I2C.h>
#include <Keypad.h>
#include <Wire.h>
#include <Password.h>
#define I2CADDR 0x3F //PCF8574 address
Password password = Password("1234"); //set default password
const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = { 0, 1, 2, 3 }; //connect to the row pinouts of the keypad
byte colPins[COLS] = { 4, 5, 6, 7 }; //connect to the column pinouts of the keypad
Keypad_I2C kpd(makeKeymap(keys), rowPins, colPins, ROWS, COLS, I2CADDR, PCF8574);
void setup()
{
Serial.begin(9600);
Wire.begin();
DDRB |= 1 << 0;
DDRB |= 1 << 1;
kpd.setDebounceTime(30);
kpd.setHoldTime(40);
kpd.begin(makeKeymap(keys));
kpd.addEventListener(keypadEvent);
}
void loop()
{
kpd.getKey();
}
void keypadEvent(KeypadEvent keyPress)
{
switch (kpd.getState()) {
case PRESSED:
switch (keyPress) {
case '#':
checkPassword();
break;
case '*':
password.reset();
PORTB &= ~(1 << 0);
break;
case 'A':
changePassword();
break;
default:
if (!password.evaluate()) {
password.append(keyPress); //append to password only if is not authenticated yet
Serial.print("+");
}
}
}
}
void checkPassword()
{
if (password.evaluate()) //if password is correct
{
PORTB |= 1 << 0;
tone(7, 440, 500);
} else {
tone(7, 500, 500);
delay(510);
noTone(7);
tone(7, 600, 500);
delay(510);
PORTB |= 1 << 1;
delay(100);
PORTB &= ~(1 << 1);
delay(100);
PORTB |= 1 << 1;
delay(100);
PORTB &= ~(1 << 1);
password.reset(); //reset password
}
}
void changePassword()
{
char pass[5]; //new password storage
byte index; //index for pass array
if (password.evaluate()) {
PORTB |= 1 << 0;
delay(100);
PORTB &= ~(1 << 0);
index = 0;
Serial.println("CP"); //print debug message "Change Pass"
while (index <= 3) { //check to see if you received 4 characters in buffer
if (kpd.getKey() != NO_KEY) {
pass[index] = kpd.getKey(); //store character in buffer one by one
Serial.print(index);
Serial.print(pass[index]);
index = index + 1; //increment index
}
}
//Serial.println(pass);
Serial.println("Password changed");
}
password.reset();
PORTB &= ~(1 << 0);
}
The problem is this: in the changePassword routine i'm waiting for 4 digits witch i put in an array (i increment the index), but when i try to print any element of the array ... every element appear blank ... like no character was append . Could you please ... put me on the right traks?
Also .. (after this problem will be solved) how could i make a string from array elements as i will need to set new password with password.set(char_from_array);
Thank you in advance.