0

I am new to arduino, currently I am looking for a way to make the keypad able to stop the buzzer making a siren alarm when password are keyed in wrong and able to move into password screen by pressing any button on the keypad. From all the videos i have seen, only 1 fits the way i want it to work.

https://www.hackster.io/thehack904/motion-sensing-alarm-with-keypad-password-53f05c

Below is the code I have completed with but i want to try adding a siren alarm function that is able to stop using keypad. Can anyone please teach me how to use a keypad to stop the buzzer and move into master key screen.

//Libraries
#include <LiquidCrystal.h>
#include <Keypad.h>

/*-------------------------------KEYPAD---------------------------------------*/
const byte numRows = 4; //number of rows on the keypad
const byte numCols = 4; //number of columns on the keypad
char keypressed;
char keymap[numRows][numCols] =
{
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};
//Code that shows the the keypad connections to the arduino terminals
byte rowPins[numRows] = {2, 3, 4, 5};//Rows 0 to 3
byte colPins[numCols] = {6, 7, 8, 9};//Columns 0 to 3
//initializes an instance of the Keypad class
Keypad myKeypad = Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);

/*-------------------------------CONSTANTS------------------------------------*/
LiquidCrystal lcd(A0, A1, A2, A3, A4, A5); //LCD
const int buzzer = 10;        //Buzzer/small speaker
int solenoidPin = 11;         //Solenoid

/*-------------------------------VARIABLES------------------------------------*/
String password = "0"; //Variable to store the current password
String master_key = "999"; //Variable to store the master key
String tempPassword = ""; //Variable to store the input password
int doublecheck;    //Check twice the new passoword
boolean armed = false;  //Variable for system state (armed:true / unarmed:false)
boolean input_pass;   //Variable for input password (correct:true / wrong:false)
boolean storedPassword = true;
boolean changedPassword = false;
boolean checkPassword = false;
int i = 1; //variable to index an array
unsigned char WrongAttempts = 0; //global variable to keep the number of wrong attempts done at the code.

/*----------------------------------------------------------------------------*/
void setup() {
  lcd.begin(16, 2); //Setup the LCD's number of columns and rows
  pinMode(solenoidPin, OUTPUT);
  pinMode(buzzer, OUTPUT);
  Serial.begin(9600);
}

void loop() { //Main loop
  unlockTheDoor();
}
/********************************FUNCTIONS*************************************/
void unlockTheDoor() {
lockAgain: //goto label
  digitalWrite(solenoidPin, HIGH);       //Switch Solenoid OFF
  tempPassword = "";
  lcd.clear();
  i = 6;
  noTone(buzzer);
  while (!checkPassword) {
    lcd.setCursor(0, 0);
    lcd.print("Open the door:  ");
    lcd.setCursor(0, 1);
    lcd.print("PASS>");
    keypressed = myKeypad.getKey();   //Read pressed keys
    if (keypressed != NO_KEY) {   //Accept only numbers and * from keypad
      if (keypressed == '0' || keypressed == '1' || keypressed == '2' || keypressed == '3' ||
          keypressed == '4' || keypressed == '5' || keypressed == '6' || keypressed == '7' ||
          keypressed == '8' || keypressed == '9' ) {
        tempPassword += keypressed;
        lcd.setCursor(i, 1);
        lcd.print("*");       //Put * on lcd
        i++;
        tone(buzzer, 800, 200); //Button tone
      }
      else if (keypressed == 'A') {
        changePassword();
        goto lockAgain;
      }
      else if (keypressed == 'D') {
        break;
      }
      else if (keypressed == '*') { //Check for password
        if (password == tempPassword) { //If it's correct...
          lcd.clear();
          lcd.setCursor(0, 0);
          lcd.print("Correct Password");
          lcd.setCursor(0, 1);
          lcd.print("Door is unlocked");
          tone(buzzer, 100);   //Play a tone while door is unlocked
          delay(100);        // ...for 0.1 sec
          noTone(buzzer);     // Stop sound...
          digitalWrite(solenoidPin, LOW);      //Switch Solenoid ON
          delay(5000);
          WrongAttempts = 0;
          changePassword(); {
            setLocked (true); ///.......to lock again
            tone(buzzer, 1000); // Send 1KHz sound signal...
            delay(100);        // ...for 0.1 sec
            noTone(buzzer);     // Stop sound...
            delay(1000);
            lcd.clear();
            goto lockAgain;
          }
          if (keypressed == '#') {
            setLocked (true); ///.......to lock again
            tone(buzzer, 1000); // Send 1KHz sound signal...
            delay(100);        // ...for 0.1 sec
            noTone(buzzer);     // Stop sound...
            delay(1000);
            lcd.clear();
            goto lockAgain;
          }
        }
        else {           //if it's false, retry
          tempPassword = "";
          lcd.clear();
          lcd.setCursor(0, 0);
          lcd.print("*Wrong Password*");
          tone(buzzer, 500, 200);
          delay(300);
          tone(buzzer, 500, 200);
          delay(300);
          WrongAttempts++;  //ADDED
          Attempts();
        }
      }
    }
  }
}

//Change current password
void changePassword() {
retry: //label for goto
  tempPassword = "";
  lcd.clear();
  i = 1;
  while (!changedPassword) {      //Waiting for current password
    keypressed = myKeypad.getKey();   //Read pressed keys
    lcd.setCursor(0, 0);
    lcd.print("CURRENT PASSWORD");
    lcd.setCursor(0, 1);
    lcd.print(">");
    if (keypressed != NO_KEY) {
      if (keypressed == '0' || keypressed == '1' || keypressed == '2' || keypressed == '3' ||
          keypressed == '4' || keypressed == '5' || keypressed == '6' || keypressed == '7' ||
          keypressed == '8' || keypressed == '9' ) {
        tempPassword += keypressed;
        lcd.setCursor(i, 1);
        lcd.print("*");
        i++;
        tone(buzzer, 800, 200);
      }
      else if (keypressed == '#') {
        break;
      }
      else if (keypressed == 'D') {
        goto retry;
      }
      else if (keypressed == '*') {
        i = 1;
        if (password == tempPassword) {
          storedPassword = false;
          tone(buzzer, 500, 200);
          newPassword();          //Password is corrent, so call the newPassword function
          break;
        }
        else {              //Try again
          tempPassword = "";
          lcd.clear();
          lcd.setCursor(0, 0);
          lcd.print("*Wrong Password*");
          tone(buzzer, 500, 200);
          delay(300);
          tone(buzzer, 500, 200);
          delay(300);
          WrongAttempts++;  //ADDED
          Attempts();
          goto retry;
        }
      }
    }
  }
}

String firstpass;
//Setup new password
void newPassword() {
retry: //label for goto
  tempPassword = "";
  changedPassword = false;
  lcd.clear();
  i = 1;
  while (!storedPassword) {
    keypressed = myKeypad.getKey();   //Read pressed keys
    if (doublecheck == 0) {
      lcd.setCursor(0, 0);
      lcd.print("SET NEW PASSWORD");
      lcd.setCursor(0, 1);
      lcd.print(">");
    }
    else {
      lcd.setCursor(0, 0);
      lcd.print("One more time...");
      lcd.setCursor(0, 1);
      lcd.print(">");
    }
    if (keypressed != NO_KEY) {
      if (keypressed == '0' || keypressed == '1' || keypressed == '2' || keypressed == '3' ||
          keypressed == '4' || keypressed == '5' || keypressed == '6' || keypressed == '7' ||
          keypressed == '8' || keypressed == '9' ) {
        tempPassword += keypressed;
        lcd.setCursor(i, 1);
        lcd.print("*");
        i++;
        tone(buzzer, 800, 200);
      }
      else if (keypressed == '#') {
        break;
      }
      else if (keypressed == 'D') {
        goto retry;
      }
      else if (keypressed == '*') {
        if (doublecheck == 0) {
          firstpass = tempPassword;
          doublecheck = 1;
          newPassword();
        }
        if (doublecheck == 1) {
          doublecheck = 0;
          if (firstpass == tempPassword) {
            i = 1;
            firstpass = "";
            password = tempPassword; // New password saved
            tempPassword = ""; //erase temp password
            lcd.setCursor(0, 0);
            lcd.print("PASSWORD CHANGED");
            lcd.setCursor(0, 1);
            lcd.print("----------------");
            storedPassword = true;
            tone(buzzer, 500, 400);
            delay(2000);
            lcd.clear();
            break;
          }
          else {
            firstpass = "";
            newPassword();
          }
        }
      }
    }
  }
}

void setLocked(int locked) {
  if (locked) {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("*** LOCKED ***");
  }
  else {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("*** UNLOCKED ***");;
  }
}

void Attempts() {
  if (WrongAttempts >= 3) { //UPS....
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("TO UNLOCK PLEASE");
    lcd.setCursor(0, 1);
    lcd.print("INPUT MASTER KEY");
    digitalWrite(buzzer, HIGH);
    delay (5000);
    master();
  }
}

void master() {
lockAgain: //goto label
retry: //label for goto
  tempPassword = "";
  lcd.clear();
  i = 6;
  noTone(buzzer);
  while (!checkPassword) {
    lcd.setCursor(0, 0);
    lcd.print("***MASTER KEY***");
    lcd.setCursor(0, 1);
    lcd.print(">");
    keypressed = myKeypad.getKey();   //Read pressed keys
    if (keypressed != NO_KEY) {   //Accept only numbers and * from keypad
      if (keypressed == '0' || keypressed == '1' || keypressed == '2' || keypressed == '3' ||
          keypressed == '4' || keypressed == '5' || keypressed == '6' || keypressed == '7' ||
          keypressed == '8' || keypressed == '9' ) {
        tempPassword += keypressed;
        lcd.setCursor(i, 1);
        lcd.print("*");       //Put * on lcd
        i++;
        tone(buzzer, 800, 200); //Button tone
      }
      else if (keypressed == 'D') {
        goto retry;
      }
      else if (keypressed == '*') { //Check for password
        if (master_key == tempPassword) { //If it's correct...
          lcd.clear();
          lcd.setCursor(0, 0);
          lcd.print("Correct Password");
          lcd.setCursor(0, 1);
          lcd.print("Door is unlocked");
          tone(buzzer, 100);   //Play a tone while door is unlocked
          delay(100);        // ...for 0.1 sec
          noTone(buzzer);     // Stop sound...
          digitalWrite(solenoidPin, LOW);      //Switch Solenoid ON
          delay(5000);
          WrongAttempts = 0;
          changePassword(); {
            setLocked (true); ///.......to lock again
            tone(buzzer, 1000); // Send 1KHz sound signal...
            delay(100);        // ...for 0.1 sec
            noTone(buzzer);     // Stop sound...
            delay(1000);
            lcd.clear();
            unlockTheDoor();
          }
          if (keypressed == '#') {
            setLocked (true); ///.......to lock again
            tone(buzzer, 1000); // Send 1KHz sound signal...
            delay(100);        // ...for 0.1 sec
            noTone(buzzer);     // Stop sound...
            delay(1000);
            lcd.clear();
            unlockTheDoor();
          }
        }
        else {           //if it's false, retry
          tempPassword = "";
          lcd.clear();
          lcd.setCursor(0, 0);
          lcd.print("*Wrong Password*");
          tone(buzzer, 500, 200);
          delay(300);
          tone(buzzer, 500, 200);
          delay(300);
          WrongAttempts++;  //ADDED
          Attempts();
        }
      }
    }
  }
}
  • Almost always for these types of questions the answer is to use a state machine. Basically you endlessly loop checking if inputs have occurred which will move you to a different state. And, as much as it might pain you, YOU SHOULD NOT USE DELAYS in a state machine. State machines are not usually the 1st project a new programmer should tackle. But if you want, I can make a more formal attempt at explaining a state machine in an answer below. – st2000 Nov 2 at 21:18
  • Yes, please. Thank you. – Fiery Kenichi Nov 2 at 21:31
  • Ok, keep in mind not many here will write complete software solutions as an answer. My self included. What most will do is to explain or point to resources to answer the question. I will essentially tell you to start over and completely define what you want to do then write your own code using a state machine. Hence why I said this is not necessary the best approach for a new programmer. – st2000 Nov 2 at 22:01
  • I have to say, now that I read the questions several times, it is not clear what you want to do. I think, you want to stop the buzzer from ever making noise. Hows about just removing it from the project? – st2000 Nov 2 at 22:09
  • you have the wrong idea about the interaction between a microcontroller, a keypad and a siren .... the siren is never controlled by the kepad ... it is always controlled by the microcontroller .... the keypad only supplies keypresses to the microcontroller ... what is done as a result of those keypresses is determined by the program ... so, all you need is to detect a sequence of kepresses .... whether the siren is active during those keypresses is irrelevant to the detection of the key sequence .... start by writing a key sequence detect program – jsotola Nov 2 at 22:16
0

Consider defining exactly what you want your Arduino application to do using a state machine approach. Drafting a state machine diagram allows a developer to visualize the inputs to the state machine, the number & types of states necessary and the outputs of the state machine. The following diagram from the above wikipedia.org link has two states:

enter image description here

A "Locked" and an "Unlocked" state. You see that a "Coin" is necessary to change from the "Locked" to the "Unlocked" state.

For the number-coded-lock in the question there are likely many more state to consider. For instance, there may be a state for each button number entered. The correct number will advance to the next state. The incorrect number will revert to the state where the state machine expects to see the 1st number in the code. If all the numbers in the code have been entered correctly, the last state will likely be the state that unlocks the device. A buzzer may have a state of its own. While in the buzzer state the buzzer will be making noise. An input to the state machine indicating that 1/2 second has elapsed could be used to exit from the buzzer state.

In this way any desired behavior can be created. In addition, by using a state diagram, the intentions of the final program are made clear. And any logical errors are exposed before code creation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.