0

So I run into a problem when I am trying to disarm the system. When I upload the code to the arduino it asks me to enter a pin and then it activates the system right away. But then when I try to deactivate the system it takes the pin and just clears the screen and execute the mainScreen function that i have set.

The code is provided below:

#include "Keypad.h"
#include "LiquidCrystal.h"
#include "Password.h"
LiquidCrystal lcd(0,1,10,11,12,13);
char newPasswordString; //hold the new password
char newPassword[4]; //charater string of newPasswordString

//initialize password to 1234
//you can use password.set(newPassword) to overwrite it
Password password = Password("1234");

byte maxPasswordLength = 4; 
byte currentPasswordLength = 4;
// keypad type definition
const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {9,8,7,6}; //Rows 0 to 3
byte colPins[COLS]= {5,4,3,2}; //Columns 0 to 3
int count=0;     
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup()
{    
  lcd.begin(16, 2);
  mainScreen();
}
void loop(){
   char key = keypad.getKey();
   if (key != NO_KEY){
      delay(60); 
      switch (key){
      case 'A': activate(); break; 
      case 'B': break; 
      case 'C': break; 
      case 'D': deactivate(); break; 
      case '#':  break;
      case '*': break;
      default: processNumberKey(key);
      }
   }
}
void processNumberKey(char key) {
   lcd.print(key);
   currentPasswordLength++;
   password.append(key);
   if(password.evaluate()){
    activate();
   }
}    
void activate() {
   if (password.evaluate()){
      lcd.clear();
      lcd.print("Activated.");
      delay(1000);
      mainScreen();
   } else {
      lcd.clear();
      lcd.print("Wrong Password!");
            mainScreen();    
   } 
}
void deactivate(){
    if (password.evaluate()){
      lcd.clear();
      lcd.print("Deactivated.");
      delay(1000);
   } else {
      lcd.clear();
      lcd.print("Wrong Password!");
      mainScreen();
   } 
}
void mainScreen(){
  lcd.clear();
  lcd.print("Enter Pin:");
  keypad.getKey();
  }
Shiv Patel
  • 39
  • 1
  • 1
  • 5

1 Answers1

0

As noted in Arduino keypad 4x4 to LCD activate/deactivate you have to keep current state and you have to CLEAR the password and currenPasswordLength. See the WORKING example in the answer.

It won't help that you set byte currenPasswordLength = 4. If you press first key, it will be incremented to 5, next key increments it to the 6 and so on.

You'll get 4 again after the 256 key-presses!!!!!!! And as you don't clear previously checked password, you'll get "wrong password" result (and eventually stack overflow).

Community
  • 1
  • 1
KIIV
  • 3,534
  • 2
  • 18
  • 23