Chiusura automatica porta

il progetto che ho realizzato è composto da un tastierino numerico che tramite l' immissione di un codice mi muove un servo in modo che sblocchi una chiusura. Questo è il codice che ho realizzato prendendo spunto da un sito:

#include <Keypad.h>
#include <ShiftLCD.h>
#include<Servo.h>

ShiftLCD lcd(2, 4, 3);

Servo myservo;

const int pinServo = 5;

int retry = 0;  //variabile sulla quale viene memorizzato il numero delle volte che si sbaglia il codice
const byte ROWS = 4; //quattro righe
const byte COLS = 4; //quattro colonne
char keys[ROWS][COLS] =
 {{'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}};
byte rowPins[ROWS] = {13,12,11,10}; //pin arduino a cui sono connessi i pin delle righe del keypad
byte colPins[COLS] = {9, 8, 7, 6}; //pin arduino a cui sono connessi i pin delle colonne del keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

char PIN[6]={'1','2','A','D','5','6'}; //PIN segreto
char attempt[6]={0,0,0,0,0,0}; //array usato per il confronto
int z=0;

void setup()
{
  lcd.begin(16,2);         // inizzializzazione dell LCD
  lcd.print("  Enter PIN...");
  myservo.attach( pinServo );
  myservo.write(20);
}

void correctPIN() //se il PIN è corretto
{
  lcd.setCursor(1,0);
  lcd.print("Unlocked door");
  myservo.write( 65 );
  delay(4000);
  lcd.clear();
  lcd.print("  Enter PIN...");
}

void incorrectPIN() //se il PIN è sbagliato
{
  lcd.setCursor(1,0);
  lcd.print("Incorrect PIN");
 
  delay(2000);
  lcd.clear();
  lcd.print("  Enter PIN...");
}

void riprova() {
   lcd.clear(); 
   lcd.setCursor(5,0);  
   lcd.print("Alarm!");
   delay(500);
   }

void checkPIN()
{
  int correct = 0;
  for (int q = 0; q < 6; q++)
  {
    if (attempt[q] == PIN[q])
    {
      correct++;
    }
  }
  if (correct == 6)
  {
    correctPIN();
  } else
  {
    incorrectPIN();
    retry = retry++;
  }
  for (int zz = 0; zz < 6; zz++) //pulisco il tentativo
  {
    attempt[zz] = 0;
  }
  if (retry == 3) {
    while(retry == 3) {
      riprova();
      }
   }
}

void readKeypad()
{
  char key = keypad.getKey();
  if (key != NO_KEY)
  {
    switch(key)
    {
    case '*':
      z = 0;
      break;
    case '#':
      delay(100); // for extra debounce
      lcd.clear();
      checkPIN();
      break;
    default:
      attempt[z] = key;
      z++;
    }
  }
}

void loop()
{
  readKeypad();
}

Ora per migliorare questo progetto vorrei modificare il codice in modo che possa riconoscermi un altra sequenza alla quale far corrispondere una serie di azioni, avete qualche suggerimento?
Grazie in anticipo per le risposte.

Hai bisogno di un'altra matrice in cui mettere la seconda password.
Quando l'utente inserisce il codice, devi fare 2 confronti, uno con la prima password ed un altro con la seconda. In base a quale password ha inserito, scegli l'azione.

Ho provato con questo codice, ma non funziona. Dove sbaglio?

#include <Keypad.h>
#include <ShiftLCD.h>
#include<Servo.h>

ShiftLCD lcd(2, 4, 3);

Servo myservo;

const int pinServo = 5;
const int button = 14;
int retry = 0;  //variabile sulla quale viene memorizzato il numero delle volte che si sbaglia il codice
const byte ROWS = 4; //quattro righe
const byte COLS = 4; //quattro colonne
char keys[ROWS][COLS] =
 {{'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}};
byte rowPins[ROWS] = {13,12,11,10}; //pin arduino a cui sono connessi i pin delle righe del keypad
byte colPins[COLS] = {9, 8, 7, 6}; //pin arduino a cui sono connessi i pin delle colonne del keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

char PIN_lock[6]={'1','2','A','D','5','6'}; //PIN segreto
char PIN_unlock[6]={'A','1','B','2','C','3'};
char attempt[6]={0,0,0,0,0,0}; //array usato per il confronto

int z=0;

void setup()
{
  lcd.begin(16,2);         // inizzializzazione dell LCD
  lcd.print("  Enter PIN...");
  myservo.attach( pinServo );
  myservo.write(20);
}

void correctPIN_() //se il PIN è corretto
{
  lcd.setCursor(1,0);
  lcd.print("Unlocked door");
  myservo.write( 60 );
  delay(4000);
  lcd.clear();
  lcd.print("  Enter PIN...");
}

void correctPIN() 
{
  lcd.setCursor(3,0);
  lcd.print("Lock door");
  myservo.write(20);
  delay(4000);
  lcd.clear();
  lcd.print("  Enter PIN...");
  }

void incorrectPIN() //se il PIN è sbagliato
{
  lcd.setCursor(1,0);
  lcd.print("Incorrect PIN");
  delay(2000);
  lcd.clear();
  lcd.print("  Enter PIN...");
}

void riprova() {
   lcd.clear(); 
   lcd.setCursor(5,0);  
   lcd.print("Alarm!");
   delay(500);
   }

void checkPIN()
{
  int correct  = 0;
  int correct_ = 0;
  for (int q = 0; q < 6; q++)
  {
    if (attempt[q] == PIN_lock[q])
    {
      correct_++;
    }
    if (attempt[q] == PIN_unlock[q])
    {
     correct++;
  }
  if (correct_ == 6)
  {
    correctPIN_();
  }
  if (correct == 6)
  { 
    correctPIN();
   }
    else
  {
    incorrectPIN();
    retry = retry++;
  }
  for (int zz = 0; zz < 6; zz++) //pulisco il tentativo
  {
    attempt[zz] = 0;
  }
  if (retry == 3) {
    while(retry == 3) {
      riprova();
      }
   }
}
}

void readKeypad()
{
  char key = keypad.getKey();
  if (key != NO_KEY)
  {
    switch(key)
    {
    case '*':
      z = 0;
      break;
    case '#':
      delay(100); // for extra debounce
      lcd.clear();
      checkPIN();
      break;
    default:
      attempt[z] = key;
      z++;
    }
  }
}

void loop()
{
  readKeypad();
  if (digitalRead(button) == HIGH) {
    myservo.write(60);
    }
}

Ho trovato l' errore e l' ho corretto:

#include <Keypad.h>
#include <ShiftLCD.h>
#include<Servo.h>

ShiftLCD lcd(2, 4, 3);

Servo myservo;

const int pinServo = 5;
const int button = 14;
int retry = 0;  //variabile sulla quale viene memorizzato il numero delle volte che si sbaglia il codice
const byte ROWS = 4; //quattro righe
const byte COLS = 4; //quattro colonne
char keys[ROWS][COLS] =
 {{'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}};
byte rowPins[ROWS] = {13,12,11,10}; //pin arduino a cui sono connessi i pin delle righe del keypad
byte colPins[COLS] = {9, 8, 7, 6}; //pin arduino a cui sono connessi i pin delle colonne del keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

char PIN_lock[6]={'1','2','A','D','5','6'}; //PIN segreto
char PIN_unlock[6]={'A','1','B','2','C','3'};
char attempt[6]={0,0,0,0,0,0}; //array usato per il confronto

int z=0;

void setup()
{
  lcd.begin(16,2);         // inizzializzazione dell LCD
  lcd.print("  Enter PIN...");
  myservo.attach( pinServo );
  myservo.write(20);
}

void correctPIN_() //se il PIN è corretto
{
  lcd.setCursor(1,0);
  lcd.print("Unlocked door");
  myservo.write( 60 );
  delay(4000);
  lcd.clear();
  lcd.print("  Enter PIN...");
}

void correctPIN() 
{
  lcd.setCursor(3,0);
  lcd.print("Lock door");
  myservo.write(20);
  delay(4000);
  lcd.clear();
  lcd.print("  Enter PIN...");
  }

void incorrectPIN() //se il PIN è sbagliato
{
  lcd.setCursor(1,0);
  lcd.print("Incorrect PIN");
  delay(2000);
  lcd.clear();
  lcd.print("  Enter PIN...");
}

void riprova() {
   lcd.clear(); 
   lcd.setCursor(5,0);  
   lcd.print("Alarm!");
   delay(500);
   }

void checkPIN()
{
  int correct  = 0;
  int correct_ = 0;
  for (int q = 0; q < 6; q++)
  {
    if (attempt[q] == PIN_lock[q]) 
    {
      correct++;
    }
  }
  if (correct == 6) 
  {
    correctPIN();
  }
  for (int q_ = 0; q_ < 6; q_++)
  {
    if (attempt[q_] == PIN_unlock[q_]) 
    {
      correct_++;
    }
  }
  if (correct_ == 6) 
  {
    correctPIN_();
  }
 
 else
  {
    incorrectPIN();
    retry = retry++;
  }
  for (int zz = 0; zz < 6; zz++) //pulisco il tentativo
  {
    attempt[zz] = 0;
  }
  if (retry == 3) {
    while(retry == 3) {
      riprova();
   }
  }
 }


void readKeypad()
{
  char key = keypad.getKey();
  if (key != NO_KEY)
  {
    switch(key)
    {
    case '*':
      z = 0;
      break;
    case '#':
      delay(100); // for extra debounce
      lcd.clear();
      checkPIN();
      break;
    default:
      attempt[z] = key;
      z++;
    }
  }
}

void loop()
{
  readKeypad();
  if (digitalRead(button) == HIGH) {
    myservo.write(60);
    }
}

Avrei un ultima domanda riguardo questo progetto: come potrei cambiare la combinazione senza cambiare lo sketch? Cioè vorrei fare in modo che immettendo la precedente psw possa cambiarla con una nuova.
Come potrei fare?

Scrivila sulla EEPROM...