Insert a loop for "change password"

Hi. I had a code for “security door system”, so I face a problem on how to set a loop which can reset the password, so i can rewrite a new password in it.

My coding as below:
// include the library code:
#include <LiquidCrystal.h>
#include <Keypad.h>
#include <Password.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
const int nada[10]={
131,175,262,349,523,698,1047,1397};
char keys[ROWS][COLS] = {
{
‘1’,‘2’,‘3’,‘A’ }
,
{
‘4’,‘5’,‘6’,‘B’ }
,
{
‘7’,‘8’,‘9’,‘C’ }
,
{
‘*’,‘0’,’#’,‘D’ }
};
char berita[16]={
’ ‘,’ ‘,’ ‘,’ ',
’ ‘,’ ‘,’ ‘,’ ',
’ ‘,’ ‘,’ ‘,’ ',
’ ‘,’ ‘,’ ‘,’ '};
char tampil[16]={
’ ‘,’ ‘,’ ‘,’ ',
’ ‘,’ ‘,’ ‘,’ ',
’ ‘,’ ‘,’ ‘,’ ',
’ ‘,’ ‘,’ ‘,’ '};
byte rowPins[ROWS] = {
14,15,16,17}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {
9, 8, 7, 6}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
byte k=4,n=0;

String jawab,kunci=“123456”;

void setup() {
pinMode(13,OUTPUT);
pinMode(18,OUTPUT);
pinMode(10,OUTPUT);
digitalWrite(13,LOW);
digitalWrite(18,LOW);
lcd.begin(16, 2);
lcd.print(“Password:”);
noTone(10);
}

void loop() {

//check key pressed
char key = keypad.getKey();
if (key != NO_KEY){
berita[n]=key;
tampil[n]=’*’;
tone(10, nada[k],250);
n++;
if(n>15){
for(n=1;n<=15;n++)berita[n-1]=berita[n];
n=15;
berita[n]=’ ‘;
}
lcd.setCursor(0, 1);
lcd.print(tampil);
noTone(10);
}
if(key==’#’){
// jawaban 6 digit pertama yang masuk dipindah ke string
for(n=0;n<6;n++)jawab+=berita[n];
if(jawab==kunci){
digitalWrite(13,HIGH);
lcd.setCursor(0, 1);
lcd.print(“Access Granted !”);
tone(10, nada[7],1000);
}
else {
digitalWrite(18,HIGH);
lcd.setCursor(0, 1);
lcd.print("Access Denied ! ");
tone(10, nada[1],500);
}
delay(1500);
for(n=0;n<=15;n++){
berita[n]=’ ‘;
tampil[n]=’ ';
}
lcd.setCursor(0, 1);
lcd.print(tampil);
n=0;
jawab=String();
}
else
digitalWrite(13,LOW);
digitalWrite(18,LOW);
delay(100);
}

Thank you.

I had a code for "security door system"

which is a) improperly posted and b) wasting resources on the String class.

so I face a problem on how to set a loop which can reset the password

How do you plan to trigger this "loop"? Loop is the wrong term.

You need to create a function that returns true or false in which you get and validate the password WITHOUT using the String class (it is easy to do without that wasteful class).

You need to call that function in setup(), NOT loop(). You need to call that function in a while loop, exiting only when the correct password has been entered.

Then, you need to determine how you are going to trigger a call to another function that gets the new password AND SAVES IT IN SOME PERSISTENT WAY.

When that trigger event happens, and you detect it in loop(), you call the function that gets and stores the new password.

You need to then modify setup() so that it gets the stored password, not some useless hard-coded password.