Help to change the code

Hello
I do not have good English, so I use Google Translate.

The problem is that this code only opens the relay module for 20 seconds, And I want the relay to stay open until the # key is pressed.
Now I'm asking you to help me change the code

code:

#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
const int RELAY_PIN  = A0; // the Arduino pin, which connects to the IN pin of relay
const int ROW_NUM    = 4; //four rows
const int COLUMN_NUM = 4; //four columns

char keys[ROW_NUM][COLUMN_NUM] = {
  {'1','2','3', 'A'},
  {'4','5','6', 'B'},
  {'7','8','9', 'C'},
  {'*','0','#', 'D'}
};

byte pin_rows[ROW_NUM] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte pin_column[COLUMN_NUM] = {5, 4, 3, 2}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27, 16 column and 2 rows

const String password_1 = "9874ABC"; // change your password here
const String password_2 = "5142CD";  // change your password here
const String password_3 = "03D07A98";// change your password here
String input_password;

void setup(){
  Serial.begin(9600);
  input_password.reserve(32);    // maximum input characters is 33, change if needed
  pinMode(RELAY_PIN, OUTPUT);    // initialize pin as an output.
  digitalWrite(RELAY_PIN, HIGH); // lock the door
  lcd.init();                    // initialize the lcd
  lcd.backlight();
}

void loop(){
  char key = keypad.getKey();

  if (key){
    Serial.println(key);

    if(key == '*') {
      input_password = ""; // reset the input password
      lcd.clear();
    } else if(key == '#') {
      lcd.clear();
      if(input_password == password_1 || input_password == password_2 || input_password == password_3) {
        Serial.println("password is correct.UNLOCK FOR 20s");
        lcd.setCursor(0, 0);
        lcd.print("CORRECT!");
        lcd.setCursor(0, 1);
        lcd.print("UNLOCK FOR 20s");

        digitalWrite(RELAY_PIN, LOW);  // unlock the door for 20 seconds
        delay(20000);
        digitalWrite(RELAY_PIN, HIGH); // lock the door
      } else {
        Serial.println("password is incorrect, try again");
        lcd.setCursor(0, 0);
        lcd.print("INCORRECT!");
        lcd.setCursor(0, 1);
        lcd.print("ACCESS DENIED!");
      }

      input_password = ""; // reset the input password
    } else {
      if(input_password.length() == 0) {
        lcd.clear();
      }

      input_password += key; // append new character to input password string
      lcd.setCursor(input_password.length(), 0); // move cursor to new position
      lcd.print('*');                 // print * key as hiden character
    }
  }
}

Thank you.

remove this

add after lcd.clear

Welcome to the forum

Your post was MOVED to its current location as it is more suitable

Thank you very much

Hi again
I found a new problem with the code.
When I enter more than 3 passwords in the code, it does not accept additional passwords.
guide me.

Hello,
publish the new sketch.


#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
const int RELAY_PIN  = A0; // the Arduino pin, which connects to the IN pin of relay
const int ROW_NUM    = 4; //four rows
const int COLUMN_NUM = 4; //four columns

char keys[ROW_NUM][COLUMN_NUM] = {
  {'1','2','3', 'A'},
  {'4','5','6', 'B'},
  {'7','8','9', 'C'},
  {'*','0','#', 'D'}
};

byte pin_rows[ROW_NUM] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte pin_column[COLUMN_NUM] = {5, 4, 3, 2}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27, 16 column and 2 rows

const String password_1 = "D38596BCA"; // change your password here
const String password_2 = "96325700D";  // change your password here
const String password_3 = "39850ABCD";// change your password here
const String password_4 = "440044";
const String password_5 = "550055";
const String password_6 = "660066";
const String password_7 = "770077";
String input_password;

void setup(){
  Serial.begin(9600);
  input_password.reserve(32);    // maximum input characters is 33, change if needed
  pinMode(RELAY_PIN, OUTPUT);    // initialize pin as an output.
  digitalWrite(RELAY_PIN, HIGH); // lock the door
  lcd.init();                    // initialize the lcd
  lcd.backlight();
}

void loop(){
  char key = keypad.getKey();

  if (key){
    Serial.println(key);

    if(key == '*') {
      input_password = ""; // reset the input password
      lcd.clear();
    } else if(key == '#') {
      lcd.clear();
      digitalWrite(RELAY_PIN, HIGH);
      if(input_password == password_1 || input_password == password_2 || input_password == password_3) {
        Serial.println("password is correct.UNLOCK");
        lcd.setCursor(0, 0);
        lcd.print("CORRECT!");
        lcd.setCursor(0, 1);
        lcd.print("PRESS # TO LOCK");

        digitalWrite(RELAY_PIN, LOW);  // unlock the door for 20 seconds
        
      } else {
        Serial.println("password is incorrect, try again");
        lcd.setCursor(0, 0);
        lcd.print("INCORRECT!");
        lcd.setCursor(0, 1);
        lcd.print("ACCESS DENIED!");
      }

      input_password = ""; // reset the input password
    } else {
      if(input_password.length() == 0) {
        lcd.clear();
      }

      input_password += key; // append new character to input password string
      lcd.setCursor(input_password.length(), 0); // move cursor to new position
      lcd.print('*');                 // print * key as hiden character
    }
  }
}

That is because you never test whether they have been entered

      if (input_password == password_1 || input_password == password_2 || input_password == password_3)

What if you had say 50 passwords ? What would that line of code above be ?

I suggest that you don't add any more passwords until you have investigated a better way to store them and check them such as by putting them in an array

you only compare to first 3

Thank you
Do you have a suggestion for this?
Or enter the next passwords like this?
And what other changes do I need to make if I want to lock it with *?

Hello,
you may take piece of paper and a pencil and design a program structure to know what happens.

You know, I almost do not know what is going on, so I came here to get your help.

Read up on arrays then consider something like this

const String passwords[] = {"D38596BCA", "96325700D", "39850ABCD", "440044"}; //etc
const byte numberOfPasswords = sizeof(passwords) / sizeof(passwords[0]);

then

      for (int p = 0; p < numberOfPasswords; p++)
      {
        if (input_password == passwords[p])
        {
          Serial.println("password is correct.UNLOCK");
etc

Once you have that working then consider not using Strings and use C style strings (lowercase s) instead, but I suggest that you get it working with Strings first

Thanks for the code as well as your suggestion.

As an exercise to show that you have learned something, please add comments to the 2 code snippets in post #13 explaining what each line of code does

Why lie, I did not understand anything :pensive:

I did not expect you to understand immediately but please follow my advice