Need help changing password

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();
  }
}

in my code when you press (star character) you will go in changing password mode

hard to follow the code

it would be easier if there were a common routine for entering a string of numbers in either a mode for entering a password to unlock the device or to enter a new password. presumably a '#' terminates the entry of a string.

loop() might call that routine. it either returns a string a numbers or a '*'. if it returns a strings of numbers, it checks that against a stored password.

if a '*' is returned, the code sets a mode where it expects to receive strings of numbers used to create a new password following some confirmation procedure. that procedure may have several sub-modes: first enter the current password, enter the new password, re-enter the new password. exist new password mode

if it sees a '*' again, it exits the set password mode.

consider

#define PASSWORD_SIZE   20
char password [PASSWORD_SIZE];

enum { Pass, Verify, Enter, Reenter };
int  mode = Pass;

// -----------------------------------------------------------------------------
char buf [PASSWORD_SIZE] = "";
int  idx   = 0;

char *
getInput (void)
{
    if (! Serial.available ())
        return 0;

    char c = Serial.read ();
    buf [idx++] = c;

    if ('#' == c || PASSWORD_SIZE == idx)  {
        buf [idx-1] = '\0';
        idx = 0;
        return buf;
    }
    else if (1 == idx && '*' == c)  {
        buf [idx] = '\0';
        idx = 0;
        return buf;
    }

    return 0;
}

// -----------------------------------------------------------------------------
char buf2 [PASSWORD_SIZE];

void loop()
{
    char *buf = getInput ();
    if (! buf)
        return;

    Serial.println (buf);
    switch (mode)  {
    case Pass:
        if ('*' == *buf)  {
            Serial.println ("Enter Old Password:");
            mode = Verify;
        }

        else if (! strcmp (password, buf))
            Serial.println ("Unlock - proceed");
        else
            Serial.println ("Password does not match");
        break;

    case Verify:
        if ('*' == *buf)  {
            Serial.println ("Abort");
            mode = Pass;
            break;
        }

        else if (! strcmp (password, buf))  {
            Serial.println ("Enter new password");
            mode = Enter;
        }
        else  {
            Serial.println ("Password does not match");
            mode = Pass;
        }
        break;

    case Enter:
        if ('*' == *buf)  {
            Serial.println ("Abort");
            mode = Pass;
            break;
        }

        strncpy (buf2, buf, PASSWORD_SIZE);
        Serial.println ("Re-enter new password");
        mode = Reenter;
        break;

    case Reenter:
        if ('*' == *buf)  {
            Serial.println ("Abort");
            mode = Pass;
            break;
        }

        if (! strcmp (buf2, buf))  {
            strncpy (password, buf, PASSWORD_SIZE);
            Serial.println ("New password created - enter password to unlock");
            mode = Pass;
        }
        else  {
            Serial.println ("Password does not match - enter new password");
            mode = Enter;
        }
        break;
    }
}

// -----------------------------------------------------------------------------
void setup()
{
    Serial.begin(9600);

    if ('\0' == *password)  {
        mode = Enter;
        Serial.println ("Enter Initial Password:");
    }
}

You forgot to set 'index' back to zero when switching from reading the current password to reading the change password.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.