# Help to change the code

**URL:** <https://forum.arduino.cc/t/help-to-change-the-code/951947>\
**Category:** Programming\
**Created:** [January 27, 2022, 7:54am UTC](https://forum.arduino.cc/t/help-to-change-the-code/951947 "2022-01-27T07:54:35Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![smalaki](https://avatars.discourse-cdn.com/v4/letter/s/bc8723/32.png) [@smalaki](https://forum.arduino.cc/u/smalaki)\
**Post date:** [January 27, 2022, 7:54am UTC](https://forum.arduino.cc/t/help-to-change-the-code/951947/1 "2022-01-27T07:54:35Z")

</div>

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:

```cpp
#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.

---

<div class="post-metadata">

**Author:** ![killzone\_kid](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/killzone_kid/32/1098074_2.png) [@killzone\_kid](https://forum.arduino.cc/u/killzone_kid)\
**Post date:** [January 27, 2022, 8:14am UTC](https://forum.arduino.cc/t/help-to-change-the-code/951947/2 "2022-01-27T08:14:52Z")

</div>

remove this

> [@smalaki](#):
>
> ```arduino
> delay(20000);
> digitalWrite(RELAY_PIN, HIGH); // lock the door
> 
> ```

add after lcd.clear

> [@smalaki](#):
>
> ```arduino
> else if(key == '#') {
> lcd.clear();
> 
> // add this
> digitalWrite(RELAY_PIN, HIGH);
> 
> ```

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [January 27, 2022, 8:22am UTC](https://forum.arduino.cc/t/help-to-change-the-code/951947/3 "2022-01-27T08:22:39Z")

</div>

Welcome to the forum

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

---

<div class="post-metadata">

**Author:** ![smalaki](https://avatars.discourse-cdn.com/v4/letter/s/bc8723/32.png) [@smalaki](https://forum.arduino.cc/u/smalaki)\
**Post date:** [January 27, 2022, 8:27am UTC](https://forum.arduino.cc/t/help-to-change-the-code/951947/4 "2022-01-27T08:27:28Z")

</div>

Thank you very much

---

<div class="post-metadata">

**Author:** ![smalaki](https://avatars.discourse-cdn.com/v4/letter/s/bc8723/32.png) [@smalaki](https://forum.arduino.cc/u/smalaki)\
**Post date:** [January 28, 2022, 9:59am UTC](https://forum.arduino.cc/t/help-to-change-the-code/951947/5 "2022-01-28T09:59:55Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![paulpaulson](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paulpaulson/32/363643_2.png) [@paulpaulson](https://forum.arduino.cc/u/paulpaulson)\
**Post date:** [January 28, 2022, 10:02am UTC](https://forum.arduino.cc/t/help-to-change-the-code/951947/6 "2022-01-28T10:02:49Z")

</div>

Hello,  
publish the new sketch.

---

<div class="post-metadata">

**Author:** ![smalaki](https://avatars.discourse-cdn.com/v4/letter/s/bc8723/32.png) [@smalaki](https://forum.arduino.cc/u/smalaki)\
**Post date:** [January 28, 2022, 10:05am UTC](https://forum.arduino.cc/t/help-to-change-the-code/951947/7 "2022-01-28T10:05:44Z")

</div>

```cpp

#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
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [January 28, 2022, 10:11am UTC](https://forum.arduino.cc/t/help-to-change-the-code/951947/8 "2022-01-28T10:11:44Z")

</div>

> [@smalaki](#):
>
> When I enter more than 3 passwords in the code, it does not accept additional passwords.

That is because you never test whether they have been entered

```arduino
      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

---

<div class="post-metadata">

**Author:** ![killzone\_kid](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/killzone_kid/32/1098074_2.png) [@killzone\_kid](https://forum.arduino.cc/u/killzone_kid)\
**Post date:** [January 28, 2022, 10:13am UTC](https://forum.arduino.cc/t/help-to-change-the-code/951947/9 "2022-01-28T10:13:49Z")

</div>

> [@smalaki](#):
>
> ` if(input_password == password_1 || input_password == password_2 || input_password == password_3)`

you only compare to first 3

---

<div class="post-metadata">

**Author:** ![smalaki](https://avatars.discourse-cdn.com/v4/letter/s/bc8723/32.png) [@smalaki](https://forum.arduino.cc/u/smalaki)\
**Post date:** [January 28, 2022, 10:16am UTC](https://forum.arduino.cc/t/help-to-change-the-code/951947/10 "2022-01-28T10:16:42Z")

</div>

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 \*?

---

<div class="post-metadata">

**Author:** ![paulpaulson](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paulpaulson/32/363643_2.png) [@paulpaulson](https://forum.arduino.cc/u/paulpaulson)\
**Post date:** [January 28, 2022, 10:20am UTC](https://forum.arduino.cc/t/help-to-change-the-code/951947/11 "2022-01-28T10:20:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![smalaki](https://avatars.discourse-cdn.com/v4/letter/s/bc8723/32.png) [@smalaki](https://forum.arduino.cc/u/smalaki)\
**Post date:** [January 28, 2022, 10:25am UTC](https://forum.arduino.cc/t/help-to-change-the-code/951947/12 "2022-01-28T10:25:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [January 28, 2022, 10:32am UTC](https://forum.arduino.cc/t/help-to-change-the-code/951947/13 "2022-01-28T10:32:23Z")

</div>

> [@smalaki](#):
>
> Do you have a suggestion for this?

Read up on arrays then consider something like this

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

then

```arduino
      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

---

<div class="post-metadata">

**Author:** ![smalaki](https://avatars.discourse-cdn.com/v4/letter/s/bc8723/32.png) [@smalaki](https://forum.arduino.cc/u/smalaki)\
**Post date:** [January 28, 2022, 10:36am UTC](https://forum.arduino.cc/t/help-to-change-the-code/951947/14 "2022-01-28T10:36:49Z")

</div>

Thanks for the code as well as your suggestion.

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [January 28, 2022, 10:40am UTC](https://forum.arduino.cc/t/help-to-change-the-code/951947/15 "2022-01-28T10:40:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![smalaki](https://avatars.discourse-cdn.com/v4/letter/s/bc8723/32.png) [@smalaki](https://forum.arduino.cc/u/smalaki)\
**Post date:** [January 28, 2022, 10:45am UTC](https://forum.arduino.cc/t/help-to-change-the-code/951947/16 "2022-01-28T10:45:50Z")

</div>

Why lie, I did not understand anything 😔

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [January 28, 2022, 11:38am UTC](https://forum.arduino.cc/t/help-to-change-the-code/951947/17 "2022-01-28T11:38:47Z")

</div>

> [@UKHeliBob](#):
>
> Read up on arrays

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

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [July 27, 2022, 11:39am UTC](https://forum.arduino.cc/t/help-to-change-the-code/951947/18 "2022-07-27T11:39:28Z")

</div>

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