HELP! Can't program a reset password input function in 4x4 keypad in arduino

Hello GUYS :slight_smile:

I'm creating a project on security system and I'm still a complete newbie on arduino codings. and i have no idea on what i am doing wrong in the reset function. I'm at wits end here. Whenever I press the "*" key on the keypad. the reset will occur but the system can't activate or detect if the password is wrong. Here's my partial code because it is still incomplete.

THESIS_inaayos.ino (5.78 KB)

Please post short programs in your Thread so we don't have to download them. I have done it for you this time.

#include <Wire.h>
#include <Password.h> //http://www.arduino.cc/playground/uploads/Code/Password.zip
#include <Keypad.h> //http://www.arduino.cc/playground/uploads/Code/Keypad.zip
#include <LiquidCrystal_I2C.h>

int pos = 90;         // variable to store the servo position 
int passwd_pos = 12;

LiquidCrystal_I2C lcd(0x27,20,4); 

int pirPin1 = 39;
int pirPin2 = 34;
int speakerPin = 35; 
int alarmStatus = 0;
int zone = 0;
int alarmActive = 0;
Password password = Password( "1234" );

const byte ROWS = 4; // Four rows
const byte COLS = 4; // Four columns

char keys[ROWS][COLS] = { // Define the Keymap
  {
    '1','2','3','A'      }
  ,
  {
    '4','5','6','B'      }
  ,
  {
    '7','8','9','C'      }
  ,
  {
    '*','0','#','D'      }
};
byte rowPins[ROWS] = {50, 51, 52, 53}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {46, 47, 48, 49};
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

// initialize the library with the numbers of the interface pins

const int buttonPin = 16;
int buttonState = 0;

#define ledPin 13
void setup(){

 pinMode(speakerPin, OUTPUT);
 pinMode(pirPin1, INPUT);  //Bedroom 2
  pinMode(pirPin2, INPUT);  //Garage
  lcd.init();                      // initialize the lcd 
  lcd.init();
  lcd.backlight();
  Serial.begin(9600);
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Enter PIN:");
  lcd.setCursor(0,2);
  lcd.print("Home Security System");
  lcd.setCursor(0,3);
  lcd.print("By ABBJJ (2015-2016)");
   pinMode(buttonPin, INPUT);  
  lcd.begin(20, 4);
  digitalWrite(ledPin, LOW);   // sets the LED on
  keypad.addEventListener(keypadEvent); //add an event listener for this keypad
  keypad.setDebounceTime(250);
  displayCodeEntryScreen();
}
void loop(){
 
   keypad.getKey();
   if (alarmActive == 1){ 
    if (digitalRead(pirPin1) == HIGH)
    {
      zone = 0;
      alarmTriggered();
    }
    
    if (digitalRead(pirPin2) == HIGH)
     {
     zone = 3 ;
     alarmTriggered();
     }
   }
}
  


void alarmTriggered(){
  int expected_pos;
  int incr;
  digitalWrite(speakerPin, HIGH);
  password.reset();
  alarmStatus = 1;
  alarmActive = 0;
  lcd.clear();
  lcd.setCursor(0,1);
  lcd.print("  SYSTEM TRIGGERED  ");
  lcd.setCursor(0,2);
  if (zone == 1)
  { 
    lcd.print("  Front Door Open  ");
    expected_pos = 65;
    delay(1000);
  }
   if(zone == 0){
    expected_pos = 40;
    lcd.print("Motion in Bedroom 1 ");
    delay(1000);
  }
  else if(zone == 2){
    expected_pos = 10;
    lcd.print("Backdoor Open");
    delay(1000);
  }
   else if(zone == 3){
     expected_pos = 145;
   lcd.print("Motion in Garage");
   delay(1000);
   }
   
   if (expected_pos > pos) {
     incr = 1;
   } else {
     incr = -1;
   }
   
   for (pos = pos; pos != expected_pos; pos += incr) {
               // tell servo to go to position in variable 'pos' 
                             // waits 5ms for the servo to reach the position 
   }
   
   /*
   for(pos = 0; pos < angle; pos += 1)   // goes from 0 degrees to 180 degrees 
  {                                      // in steps of 1 degree 
    myservo.write(pos);                  // tell servo to go to position in variable 'pos' 
    delay(20);                           // waits 15ms for the servo to reach the position 
  } 
  for(pos = angle; pos>=1; pos-=1)       // goes from 180 degrees to 0 degrees 
  {                                
   myservo.write(pos);                   // tell servo to go to position in variable 'pos' 
   delay(20);                            // waits 15ms for the servo to reach the position 
  } 
  */
  
}
void keypadEvent(KeypadEvent eKey){
  switch (keypad.getState()){
  case PRESSED:
    if (passwd_pos - 15 >= 2) { 
      return ;
     lcd.print(eKey);
    }
     
    lcd.setCursor((passwd_pos++),0);
    switch (eKey){
    case '#':                 //# is to validate password 
      
     passwd_pos  = 15;
     checkPassword();
      break;
    case '*':                 //* is to reset password attempt
       password.reset();
      passwd_pos  = 12;
     Serial.begin(9600);
     
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Enter PIN:");
  lcd.setCursor(0,2);
  lcd.print("Home Security");
  lcd.setCursor(0,3);
  lcd.print("By ABBJJ (2015-2016)");
 
     
    lcd.setCursor((passwd_pos++),0);
        keypad.addEventListener(keypadEvent);
   // TODO: clear the screen output 
      break;
      
    default: 
      password.append(eKey);
      lcd.print("*");
     displayCodeEntryScreen();
    }
  }
}
void checkPassword()
{
  if (password.evaluate())
  {  
  Serial.begin(9600);
  lcd.clear();
    lcd.setCursor(1,1);
    lcd.print("SECURITY ACTIVATED");
     lcd.setCursor(0,0);
  
  lcd.setCursor(0,2);
  lcd.print("Home Security System");
  lcd.setCursor(0,3);
  lcd.print("By ABBJJ (2015-2016)");
  activate();
  }
else
{lcd.setCursor (1,1);
    lcd.print("INVALID PASSWORD");
}
}
void invalidCode()    // display meaasge when a invalid is entered
{
  password.reset();
  lcd.clear();
  lcd.setCursor(1,1);
  lcd.print("INVALID CODE! LOL!");
  lcd.setCursor(0,2);
  lcd.print("TRY AGAIN!");
  displayCodeEntryScreen();
}

void activate()      // Activate the system if correct PIN entered and display message on the screen
{
  
    digitalWrite(2, HIGH);
    lcd.setCursor(0,2);
    lcd.print("SYSTEM ACTIVE!"); 
    alarmActive = 1;
    password.reset();
    delay(2000);
  }
  


void deactivate()
{
  //digitalWrite(camera, LOW);
  alarmStatus = 0;
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print(" SYSTEM DEACTIVATED!");
  digitalWrite(speakerPin, LOW);
  alarmActive = 0;
  password.reset();
  delay(5000);
  
displayCodeEntryScreen();
}
void displayCodeEntryScreen()    // Dispalying start screen for users to enter PIN
{
  
  lcd.setCursor(0,0);
  lcd.print("Enter PIN:");
  lcd.setCursor(0,2);
  lcd.print("Home Security");
  lcd.setCursor(0,3);
  lcd.print("By ABBJJ (2015-2016)");
  
}

And, sorry, I am not familiar with the Password library. I had not realized from your title that your problem was with resetting a password. If you edit your Original Post you can update the title.

...R

Oh sorry. I'm just new to this forum so I don't know how to post the codes. Thank you. :slight_smile:

I forgot to include the link How to use the Forum

...R

bjalmadin:
Hello GUYS :slight_smile:

I'm creating a project on security system and I'm still a complete newbie on arduino codings. and i have no idea on what i am doing wrong in the reset function. I'm at wits end here. Whenever I press the "*" key on the keypad. the reset will occur but the system can't activate or detect if the password is wrong. Here's my partial code because it is still incomplete.

Sorry, I don't understand the inner working of your alarm system.

Maybe it's to become a very simple system which operates in just three different states:

  • system deactivated (system is waiting for entering the activation code )
  • system activated (system is waiting for either an alarm trigger to happen or for entering the deactivation code.
  • system alarm (system is providing alarm using sound and light

Besides such an easy system with just three states, it also would be possible to create alarm system with much more different operating states, like "delayed alarm", in which the alarm is triggered by one of the sensors, a soft voice is saying (recorded playback) something like "you now have 15 seconds to enter the deactivation code" and at first nothing else happens, and alarm is delayed: If siomeone enters the deactivation code within a timespan of 15 seconds after triggering a sensor, nothing else happens and the system switches back to inactive state. But if the deactivation code is not entered in time, the alarm will happen as it would happen in the simple system without the voice announcement "you now have 15 seconds to enter the deactivation code"and no delay before alarming functionbegins.

A typically programming technique for programming systems with a fixed amount of "operating states" is called "Finite State Machine", abbreviation is FSM.

In an FSM, each state has its own function how to handle things and how to switch into another state.

Let's come back to the easy alarm system I described above:

In the "deactivated state", the system can only switch into activated state, after entering the activation code. Nothing else will happen in the deactivated state.

In the "activated state"there are two possibilities to come out:
1.) Entering the deactivation code switches state back to "deactivated state"
2.)Triggering a sensor switches state to "alarm state"
And in "alarm state" there is just one possibility to get out:

  • Entering the deactivation code will switch back to "deactivated state"

Is it that what you want?

Perhaps you can tell a little bit more about the system you want to create!

Sorry, I don't understand the inner working of your alarm system.

Maybe it's to become a very simple system which operates in just three different states:

  • system deactivated (system is waiting for entering the activation code )
  • system activated (system is waiting for either an alarm trigger to happen or for entering the deactivation code.
  • system alarm (system is providing alarm using sound and light

Besides such an easy system with just three states, it also would be possible to create alarm system with much more different operating states, like "delayed alarm", in which the alarm is triggered by one of the sensors, a soft voice is saying (recorded playback) something like "you now have 15 seconds to enter the deactivation code" and at first nothing else happens, and alarm is delayed: If siomeone enters the deactivation code within a timespan of 15 seconds after triggering a sensor, nothing else happens and the system switches back to inactive state. But if the deactivation code is not entered in time, the alarm will happen as it would happen in the simple system without the voice announcement "you now have 15 seconds to enter the deactivation code"and no delay before alarming functionbegins.

A typically programming technique for programming systems with a fixed amount of "operating states" is called "Finite State Machine", abbreviation is FSM.

In an FSM, each state has its own function how to handle things and how to switch into another state.

Let's come back to the easy alarm system I described above:

In the "deactivated state", the system can only switch into activated state, after entering the activation code. Nothing else will happen in the deactivated state.

In the "activated state"there are two possibilities to come out:
1.) Entering the deactivation code switches state back to "deactivated state"
2.)Triggering a sensor switches state to "alarm state"
And in "alarm state" there is just one possibility to get out:

  • Entering the deactivation code will switch back to "deactivated state"

Is it that what you want?

Perhaps you can tell a little bit more about the system you want to create!

My security system (actually it's my final project at school before graduating. and we only learn arduino by ourselves so i have no idea what went wrong) is an indoor security system which includes gsm module, ip cameras, and motion sensors. First off.. i want to activate the system using a set of 4 number password. Then when i press "#" key it would activate the system if the password is correct. and when i press "*" it would reset the code i just entered to avoid pressing the wrong code 3 times otherwise it will be disabled. Then to deactivate i will need to retype again the password. The sketch that i posted is still incomplete because i'm still stuck at the password reset problem.

Not using the password library, you can use the below

#include <Keypad.h>

const byte ROWS = 4; // Four rows
const byte COLS = 4; // Four columns

char keys[ROWS][COLS] =   // Define the Keymap
{
  {
    '1', '2', '3', 'A'
  }
  ,
  {
    '4', '5', '6', 'B'
  }
  ,
  {
    '7', '8', '9', 'C'
  }
  ,
  {
    '*', '0', '#', 'D'
  }
};
byte rowPins[ROWS] = {50, 51, 52, 53}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {46, 47, 48, 49};
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );




void setup()
{
  // put your setup code here, to run once:

}

void loop()
{
  // pointer to password entered by used
  char *userPwd;

  // read the keypad
  char ch = keypad.getKey();

  // if key pressed
  if (ch != NO_KEY)
  {
    // add to password
    userPwd = readPWD(ch);
    // if password entry completed
    if (userPwd != NULL)
    {
      // compare
      if (strcmp(userPwd, "1234") == 0)
      {
        // access granted
      }
      else
      {
        // access denied
      }
    }
  }
}


/*
  read keypad for password
  returns NULL while # not pressed, else (pointer to) entered password
*/
char *readPWD(char ch)
{
  static char pwd[5];
  static byte index = 0;

  // if cancel or index 0
  if (ch == '*' || index == 0)
  {
    // clear the password buffer
    memset(pwd, 0, sizeof(pwd));
    // indicate password not complete
    return NULL;
  }

  // if 4 characters entered and # not pressed
  if (index >= 4 && ch != '#')
  {
    // ignore; indicate password not complete
    return NULL;
  }

  // if # pressed
  if (ch == '#')
  {
    // add nul character
    pwd[index] = '\0';
    // return the entered password
    return pwd;
  }

  // add to password and increment index
  pwd[index++] = ch;
  // reset index for next time password needs to be entered
  // indicate password not complete
  return NULL;
}

In the readPWD function, you can use index as the X position on the LCD where you want to write the '*' when the user presses a button.

Compiles but not tested

sterretje:
Not using the password library, you can use the below

#include <Keypad.h>

const byte ROWS = 4; // Four rows
const byte COLS = 4; // Four columns

char keys[ROWS][COLS] =  // Define the Keymap
{
  {
    '1', '2', '3', 'A'
  }
  ,
  {
    '4', '5', '6', 'B'
  }
  ,
  {
    '7', '8', '9', 'C'
  }
  ,
  {
    '*', '0', '#', 'D'
  }
};
byte rowPins[ROWS] = {50, 51, 52, 53}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {46, 47, 48, 49};
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup()
{
  // put your setup code here, to run once:

}

void loop()
{
  // pointer to password entered by used
  char *userPwd;

// read the keypad
  char ch = keypad.getKey();

// if key pressed
  if (ch != NO_KEY)
  {
    // add to password
    userPwd = readPWD(ch);
    // if password entry completed
    if (userPwd != NULL)
    {
      // compare
      if (strcmp(userPwd, "1234") == 0)
      {
        // access granted
      }
      else
      {
        // access denied
      }
    }
  }
}

/*
  read keypad for password
  returns NULL while # not pressed, else (pointer to) entered password
*/
char *readPWD(char ch)
{
  static char pwd[5];
  static byte index = 0;

// if cancel or index 0
  if (ch == '*' || index == 0)
  {
    // clear the password buffer
    memset(pwd, 0, sizeof(pwd));
    // indicate password not complete
    return NULL;
  }

// if 4 characters entered and # not pressed
  if (index >= 4 && ch != '#')
  {
    // ignore; indicate password not complete
    return NULL;
  }

// if # pressed
  if (ch == '#')
  {
    // add nul character
    pwd[index] = '\0';
    // return the entered password
    return pwd;
  }

// add to password and increment index
  pwd[index++] = ch;
  // reset index for next time password needs to be entered
  // indicate password not complete
  return NULL;
}



In the readPWD function, you can use index as the X position on the LCD where you want to write the '*' when the user presses a button.

Compiles but not tested

sorry but it's not working.

sorry but it's not working.

It IS working. That it does not do what you want is a different story. You need to tell us what the code ACTUALLY does, and how that differs from what you want.