How do I add my keypad code into my PIR alarm code?

Hi,
I need help to write a code for my project. I have complied two different codes which work for me so far, I have already created a successful project which can detect motion and triggers an buzzer which then needs to be turned off by pressing the switch, this is shown in PIR alarm code, below.
The other code is the 4x4 keypad, I have tested that all buttons work within this code. shown in 4x4 keypad code.
Now, I want to write a code for my PIR motion based security alarm which is activated and deactivated by a 4x4 keypad. I want to set a password which will activate the motion sensor/alarm, if motion is detected then the alarm goes off. To turn off the alarm you must enter the same password.
PIR Alarm:

void setup() {
  // Declaring Pins
const int buzzerPin = 5;
const int ledPin = 6;
const int motionPin = 7;
const int buttonPin = 12;

// Setting Buzzer mode to False
boolean buzzer_mode = false;

// For LED
int ledState = LOW;
long previousMillis = 0; 
long interval = 100;  // Interval at which LED blinks

void setup() {
  pinMode(ledPin,OUTPUT);
  pinMode(buzzerPin,OUTPUT);

  pinMode(buttonPin, INPUT);
  
  // Wait before starting the alarm
  delay(5000);
}

void loop() {
  // To chech whether the motion is detected or not
  if (digitalRead(motionPin)) {
    buzzer_mode = true; 
  }
}

  // If alarm mode is on,blink LED
  if (buzzer_mode){
    unsigned long currentMillis = millis();
    if(currentMillis - previousMillis > interval) {
      previousMillis = currentMillis;   
      if (ledState == LOW)
        ledState = HIGH;
      else
        ledState = LOW;
    // Switch the LED
    digitalWrite(ledPin, ledState);
    }
    tone(buzzerPin,1000);
  }

  // If alarm is off
  if (buzzer_mode == false) {
  
    // No tone & LED off
    noTone(buzzerPin);  
    digitalWrite(ledPin, LOW);
  }

  // If our button is pressed Switch off ringing and Setup
  int button_state = digitalRead(buttonPin);
  if (button_state) {buzzer_mode = false;}
}


}

void loop() {
  // put your main code here, to run repeatedly:

}

4x4 keypad code:

#include <Keypad.h>

const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
char keyMap[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};
byte rowPins[ROWS] = {3, 4, 5, 6}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {7, 8, 9, 10}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keyMap), rowPins, colPins, ROWS, COLS );

void setup(){
  Serial.begin(9600);
}
  
void loop(){
  char key = keypad.getKey();
  
  if (key){
    Serial.println(key);
  }
}

How to merge codes.

Read the forum guidelines to see how to properly post code and some information on how to get the most from this forum.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

You can go back and fix your original post by highlighting the code and clicking the </> in the menu bar.
code tags new

#include <Keypad.h>

const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
char keyMap[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};
const byte rowPins[ROWS] = {3, 4, 5, 6}; //connect to the row pinouts of the keypad
const byte colPins[COLS] = {7, 8, 9, 10}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keyMap), rowPins, colPins, ROWS, COLS );

// Declaring Pins
const int buzzerPin = A1;
const int ledPin = A2;
const int motionPin = A3;
//const int buttonPin = 12;
const unsigned long interval = 100;  // Interval at which LED blinks

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(buzzerPin, OUTPUT);
  //pinMode(buttonPin, INPUT);
  pinMode(motionPin, INPUT);
  delay(5000);
}

void loop() {
  static bool isLEDon = false;
  static boolean buzzer_mode = false;
  static unsigned long previousMillis = 0;
  if (digitalRead(motionPin) == HIGH) {  // To check whether the motion is detected or not
    buzzer_mode = true;
    previousMillis = millis();
  }

  if (buzzer_mode) {                                   // If alarm mode is on,blink LED
    tone(buzzerPin, 1000);
    if (millis() - previousMillis > interval) {
      previousMillis += interval ;
      if (isLEDon ) {
        isLEDon = false;
        digitalWrite(ledPin, LOW);
      }
      else {
        isLEDon = true;
        digitalWrite(ledPin, HIGH);
      }
    }
  } else {                                             // If alarm is off
    noTone(buzzerPin);                                // No tone & LED off
    digitalWrite(ledPin, LOW);
  }

  char x = keypad.getKey();
  if (x != NO_KEY)buzzer_mode = false;    // If our button is pressed Switch off ringing
}

Hi,
Thanks for the reply. However, this is not what I was thinking in mind. Im not sure if I have clearly stated what I want.
I to provide one for code for PIR motion based security alarm which is activated and deactivated by a 4x4 keypad. I want to set a password which will activate the motion sensor/alarm, if motion is detected then the alarm goes off. To turn off the alarm you must enter the same password.

What Arduino board?

You cannot use the same pin for the keypad and another function. You need to assign the buzzer, led and motion to different pins. Do not use pins 0 or 1, they are Serial port and you will need that. You can use the analog input pins as digital pins.

Hi, thanks Iknow I have just provided the two different codes in at the moment, I need to write a whole new code. However, I don't know to apply a keypad into the code which includes a password and can activate the system and then deactivate

Basically what do u want to switch between two different sensor or two perform two different activity by pressing button
If you want to deactivate sensor on button press so it a very easy wat so kindly reply

So at the moment my security alarm system project as shown above is triggered by the PIR motion sensor and when triggered the buzzer activates. To deactivate I must press the switch.
To further advance this system I want to implement a 4x4 keypad instead of a switch. So I want the system to work like this.
Start > Enter Keypad Password To activate the system > if motion has been detected > Buzzer goes off > To turn the buzzer off, re-eneter the password on keypad > Finish

I gave you a link to a tutorial on merging codes. Let's see your attempt. Tell us what the code does and how that differs from what you want and we will try to help fix it.

A state machine might make things easier.

If you just want someone to do it for you, ask a moderator to move this thread to the the Jobs and Paid Consultancy category. Expect to pay.

I updated sketch. now any key set alarm off

Please put the new code in a new post. Don't update old posts as that messes up the thread continuity.

yesterday moderator said I should update
ok. this is not the end, i think. next version i put in new post.

Oh so you want to activate buzzer when motion detected and manually turn off the buzzer by pressing or entering password is it true?

Yes that is true. I have attempted over last week and it has not been working.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.