hello everyone
im coding a password lock
i write mostly all the code but i have some issue for changing password
in my code when you press '' you will go in changing password mode
first you need to enter current password
and then enter new password after this you should press '#' to save password
my problem is when i press '' only i can insert one number and then noting
please help
this is full code of project
#include<Wire.h>
#include <LiquidCrystal_I2C.h>
#include <EEPROM.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
const byte ROWS_COUNT = 4;
const byte COLUMNS_COUNT = 4;
char Keys_map [ROWS_COUNT][COLUMNS_COUNT] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte ROWS[ROWS_COUNT] = {2, 3, 4, 5};
byte COLUMNS[COLUMNS_COUNT] = {6, 7, 8, 9};
char inputKey = '&';
bool Check = false;
int del = 300;
char Saved_PASS[10];
char Input_PASS [10];
char change_password [10];
char Check_current_pass[10];
char star_pass[10];
int door = 10;
bool PASS_Change = false;
bool insert_current_pass = false;
unsigned int index = 0;
void(* resetFunc) (void) = 0;
void setup() {
Serial.begin(9600);
lcd.init();
lcd.backlight();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("*Enter PASSWORD*");
pinMode(door , OUTPUT);
digitalWrite(door, HIGH);
EEPROM.get(0, Saved_PASS);
Serial.println(Saved_PASS);
for (int i = 0; i < 10; i++) {
if (!(Saved_PASS[i] >= '0' && Saved_PASS[i] <= '9')) {
Saved_PASS[i] = '\0';
}
if (!(Input_PASS[i] >= '0' && Input_PASS[i] <= '9')) {
Input_PASS[i] = '\0';
}
if (!(Check_current_pass[i] >= '0' && Check_current_pass[i] <= '9')) {
Check_current_pass[i] = '\0';
}
if (star_pass[i] != '*') {
star_pass[i] = '\0';
}
}
}
void loop() {
pinmode();
inputKey = KeyCheck ();
pass_Check();
change_pass ();
clear_array();
Reset_board();
//star_password();
}
void pinmode() {
for (int i = 0; i < 4; i++) {
pinMode(ROWS[i], OUTPUT);
digitalWrite(ROWS[i], LOW);
pinMode(COLUMNS[i], INPUT_PULLUP);
}
}
char KeyCheck () {
char pressed_Key = '&';
for (int j = 0; j < 4; j++) {
if (digitalRead(COLUMNS[j]) == 0) {
pinMode(COLUMNS[j], OUTPUT);
digitalWrite(COLUMNS[j], LOW);
for (int i = 0; i < 4; i++) {
pinMode(ROWS[i], INPUT_PULLUP);
if (digitalRead(ROWS[i]) == 0) {
delay(del);
pressed_Key = Keys_map[i][j];
Check = true;
return pressed_Key;
}
}
}
}
}
void pass_Check() {
if (inputKey >= '0' && inputKey <= '9' && Check == true && PASS_Change == false) {
Input_PASS[index] = inputKey;
inputKey = '&';
Check = false;
index++;
Serial.println(Input_PASS);
}
bool match = true;
for (int i = 0; i < 10; ++i) {
if (Saved_PASS[i] != Input_PASS[i]) {
match = false;
break;
}
}
if (match == true) {
Serial.println("door is open");
lcd.clear();
lcd.setCursor(1, 0);
lcd.print("***WELLCOME***");
lcd.setCursor(2, 1);
lcd.print("DOOR is OPEN");
digitalWrite(door, LOW);
delay(3000);
digitalWrite(door, HIGH);
Serial.println("door is closed");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("*Enter PASSWORD*");
lcd.setCursor(1, 1);
lcd.print("DOOR is CLOSED");
delay(1000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("*Enter PASSWORD*");
match = false;
for (int i = 0; i < 10; i++) {
Input_PASS[i] = '\0';
index = 0;
}
}
}
void change_pass () {
if (inputKey == '*' && Check == true && PASS_Change == false) {
PASS_Change = true;
insert_current_pass = true;
inputKey = '&';
Check = false;
index = 0;
for (int i = 0; i < 10; i++){
change_password[i] = '\0';
}
Serial.println("insert current PASSWORD");
Serial.println("then insert new PASSWORD");
Serial.println("Press '#' When you are done!");
lcd.clear();
lcd.setCursor(1, 0);
lcd.print("Enter New Pass");
lcd.setCursor(1, 1);
lcd.print("press# when done");
}
if (inputKey >= '0' && inputKey <= '9' && Check == true && PASS_Change == true) {
if (insert_current_pass == true) {
Check_current_pass[index] = inputKey;
inputKey = '&';
Check = false;
index++;
Serial.println(Check_current_pass);
for (int i = 0; i < 10; ++i) {
if (Check_current_pass[i] != Saved_PASS[i]) {
insert_current_pass = true;
}
else{
insert_current_pass = false;
break;
}
}
}
if (insert_current_pass == false) {
change_password[index] = inputKey;
inputKey = '&';
Check = false;
index++;
Serial.println(Input_PASS);
}
}
if (inputKey == '#' && Check == true && PASS_Change == true) {
if (change_password[0] >= '0' && change_password[1] >= '0' && change_password[2] >= '0' && change_password[3] >= '0' ) {
PASS_Change = false;
inputKey = '&';
Check = false;
index = 0;
Serial.println("new PASSWORD is now : ");
Serial.println(change_password);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("New Password is:");
lcd.setCursor(0, 1);
lcd.print(change_password);
EEPROM.put(0, change_password);
delay(50);
for (int i = 0; i < 10 ; i++) {
Saved_PASS[i] = change_password[i];
change_password[i] = '\0';
}
delay(1000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("*Enter PASSWORD*");
}
else {
PASS_Change = false;
inputKey = '&';
Check = false;
index = 0;
for (int i = 0; i < 10 ; i++) {
change_password[i] = '\0';
}
Serial.println("password must be greater than 999");
}
}
}
void clear_array() {
if (inputKey == 'A' && Check == true) {
if (PASS_Change == true) {
for (int i = 0; i < 10; i++) {
change_password[i] = '\0';
}
}
else {
for (int i = 0; i < 10; i++) {
Input_PASS[i] = '\0';
}
}
inputKey = '&';
Check = false;
index = 0;
}
if (inputKey == 'B' && Check == true) {
if (PASS_Change == true) {
change_password[index] = '\0';
Serial.println(change_password);
}
else {
Input_PASS[index] = '\0';
Serial.println(Input_PASS);
}
index--;
inputKey = '&';
Check = false;
}
}
void star_password() {
for (int i = 0; i < index; i++) {
star_pass[i] = '*';
}
for (int i = 0; i < 10; i++) {
if (star_pass[i] > index) {
star_pass[i] = '\0';
}
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("*Enter PASSWORD*");
lcd.setCursor(0, 1);
lcd.print(star_pass);
}
void Reset_board() {
if (inputKey == 'C' && Check == true) {
lcd.clear();
lcd.setCursor(1, 0);
lcd.print("Reset in 3 Sec");
Serial.println("Reset in 3 Sec");
delay(1000);
lcd.clear();
lcd.setCursor(1, 0);
lcd.print("Reset in 2 Sec");
Serial.println("Reset in 2 Sec");
delay(1000);
lcd.clear();
lcd.setCursor(1, 0);
lcd.print("Reset in 1 Sec");
Serial.println("Reset in 1 Sec");
delay(1000);
lcd.clear();
lcd.setCursor(1, 0);
lcd.print("Reseting Board");
Serial.println("Reseting Board");
delay(300);
resetFunc();
}
}