Anti theft Alarm problem..

Hi guys Im a university student and currently doing a project (anti theft alarm). I am using a PIR motion sensor and a REED switch to detect movement. The PIR motion sensor sets to LOW when it detects movement and the REED switch state is changed when movement is detected (default value LOW/OFF). Ive also connected a 4x4 matrix keypad and a standard 16x2 LCD display to enable user interaction. My main aim is to create a system that allows the user to press a button to activate the alarm (this will then try and detect any movement from the sensor and if the REED switch state has changed it should trigger the alarm continuously), then a button to deactivate the alarm (this will then display on the LCD “Enter Password” or something similar and if the password is correct the alarm will go to LOW and if not it will continue to stay on HIGH), lastly an enter and a clear button to enable the user to enter the password and clear it. At the moment I have two versions of my code but sadly both of them are not working properly and is not doing what I wanted to do. The first version only detects movement and only triggers the alarm when movement is present( I want the alarm to stay on HIGH as soon as it detects any movement) but the password part does not work (when the enter pass button is pressed the alarm will not stay on HIGH ie alarm is turned off).The second version is similar but the password part works,although the password part works it does not affect the alarm at all(ie it doesnt turn the alarm off, the alarm is still triggered when movement is present ie so even if the correct password is entered and if the sensors detect movement the alarm would still be triggered).I am not sure how to fix it.

This is the first version

#include <Password.h>
#include <LiquidCrystal.h>
#include <Keypad.h> 

enum { 
  ALARM_ON,
  ALARM_OFF,
  ENTER_PASS
} state;

byte ledPin = 13;
int spkrPin = 10;
int reedPin = 12;
int pirPin = 11;

int calibrationTime = 5;
long unsigned int lowIn;
long unsigned int pause = 5000;

const byte ROWS = 4; //four rows
const byte COLS = 4;//four columns //define the symbols on the buttons of the keypads
char keyMap[ROWS][COLS] = { 
{'1','2','3','A'},
{'4','5','6','B'}, 
{'7','8','9','C'},
{'*','0','#','D'} //	ROW 1,2,3,4
}; 
byte rowPins[ROWS] = {2, 3, 4, 5}; // which Arduino pins to connect to
//	COL 1,2,3,4 
byte colPins[COLS] = {6, 7, 8, 9}; // which Arduino pins to connect to
//create instance of keypad
Keypad keypad = Keypad( makeKeymap(keyMap), rowPins, colPins, ROWS, COLS);

LiquidCrystal lcd(14, 15, 16, 17, 18, 19);

Password password = Password("1234");

boolean switchState;



void setup() 
{
  state=ALARM_OFF;
 Serial.begin(9600);
 lcd.begin(16,2);
 pinMode(ledPin, OUTPUT);// set up serial port
 pinMode(spkrPin,OUTPUT);
 pinMode(reedPin,INPUT);
 pinMode(pirPin,INPUT);
 digitalWrite(ledPin, LOW);
 digitalWrite(pirPin,HIGH);
   Serial.print("calibrating sensor ");
    for(int i = 0; i < calibrationTime; i++){
      Serial.print(".");
      delay(1000);
      }
    Serial.println(" done");
    Serial.println("SENSOR ACTIVE");
    delay(50);

 
}

void loop() 
{ 
   char keyPressed = keypad.getKey();
   if (keyPressed != NO_KEY){ // if key has been pressed

digitalWrite(ledPin,HIGH);
digitalWrite(spkrPin,HIGH);
delay (50);
digitalWrite(ledPin,LOW);
digitalWrite(spkrPin,LOW);
delay (50);
Serial.println(keyPressed);

}

 switch (state) {
    case ALARM_ON: {
      if (digitalRead (pirPin) == LOW) {
       digitalWrite(ledPin, HIGH);   //the led visualizes the sensors output pin state
      digitalWrite(spkrPin,HIGH);
      Serial.println("movement detected");
         delay(50);
         digitalWrite(ledPin, LOW);
         digitalWrite(spkrPin,LOW);
         delay(50);
      }
  else{       
    digitalWrite(ledPin, LOW);  //the led visualizes the sensors output pin state
       digitalWrite(spkrPin,LOW);
       delay(50);
         }       
       switchState = digitalRead(reedPin);
       
      if (switchState) {
        digitalWrite(ledPin,HIGH);
        digitalWrite(spkrPin,HIGH);
      }
      else {
        digitalWrite(ledPin,LOW);
        digitalWrite(spkrPin,LOW);
      }
      if (keyPressed == 'B') {
        state = ENTER_PASS;
      }
     break;
    }
    case ALARM_OFF: {
      // sone more stuff
      // look for keypad press to turn alarm on
      //pseudo:
      digitalWrite(ledPin,LOW);
      digitalWrite(spkrPin,LOW);
      if (keyPressed == 'A')
        state = ALARM_ON;
      break;
    }
    case ENTER_PASS: {
      if (keyPressed == '*') {
        if(password.evaluate()) {
          state = ALARM_OFF;
          digitalWrite(ledPin,HIGH);
         delay(2000);
          digitalWrite(ledPin,LOW);
        }
        else {
          // password wrong
            digitalWrite(ledPin,LOW);
     digitalWrite(spkrPin,HIGH);
     lcd.setCursor (0,0);
     lcd.print("Enter Password");
     lcd.setCursor (0,1);
     lcd.print("Incorrect, Try again!");
        }
      
        ///
      }
      else if (keyPressed == '#') {
        password.reset();
      }
      else {
          password.append (keyPressed);
      }
      break;
    }
    
  }
}

Any help would be greatly appreciated. If you want me to post more information about my project just tell me. Thank you for your time on reading my post.

This is the second version

#include <Password.h>
#include <LiquidCrystal.h>
#include <Keypad.h> 

byte ledPin = 12;
int spkrPin = 10;
int reedPin = 13;
int pirPin = 11;

int calibrationTime = 5;
long unsigned int lowIn;
long unsigned int pause = 5000;

const byte ROWS = 4; //four rows
const byte COLS = 4;//four columns //define the symbols on the buttons of the keypads
char keyMap[ROWS][COLS] = { 
{'1','2','3','A'},
{'4','5','6','B'}, 
{'7','8','9','C'},
{'*','0','#','D'} //	ROW 1,2,3,4
}; 
byte rowPins[ROWS] = {2, 3, 4, 5}; // which Arduino pins to connect to
//	COL 1,2,3,4 
byte colPins[COLS] = {6, 7, 8, 9}; // which Arduino pins to connect to
//create instance of keypad
Keypad keypad = Keypad( makeKeymap(keyMap), rowPins, colPins, ROWS, COLS);

LiquidCrystal lcd(14, 15, 16, 17, 18, 19);

Password password = Password("1234");

boolean programMode = false;
boolean switchState;



void setup() 
{
 Serial.begin(9600);
 lcd.begin(16,2);
 pinMode(ledPin, OUTPUT);// set up serial port
 pinMode(spkrPin,OUTPUT);
 pinMode(reedPin,INPUT);
 pinMode(pirPin,INPUT);
 digitalWrite(ledPin, LOW);
 digitalWrite(pirPin,HIGH);
   Serial.print("calibrating sensor ");
    for(int i = 0; i < calibrationTime; i++){
      Serial.print(".");
      delay(1000);
      }
    Serial.println(" done");
    Serial.println("SENSOR ACTIVE");
    delay(50);
 
 keypad.addEventListener(keypadEvent);
 
}

void loop() 
{
 char keyPressed = keypad.getKey();
 if (keyPressed == 'C');{
 activateAlarm();
}
}

//take care of some special events
void keypadEvent(KeypadEvent keyPressed) {
  switch (keypad.getState()) {
      case PRESSED:
    switch(keyPressed){
      case 'A' : enterPassword();
      digitalWrite(ledPin,LOW);
      break;
      case 'B' : password.reset();
      digitalWrite(ledPin,LOW);
      break;
//      case 'C' : activateAlarm();
  //    break;
      default : if (keyPressed != NO_KEY){
      Serial.print("Key Pressed = ");
      lcd.setCursor (0,1);
      lcd.print(keyPressed);
      Serial.println(keyPressed);  
      }
      password.append(keyPressed);  
      digitalWrite(ledPin,HIGH);
      digitalWrite(spkrPin,HIGH);
      break;
    }
    case RELEASED:
    switch (keyPressed){
      default : digitalWrite (ledPin,LOW);
      digitalWrite(spkrPin,LOW);
      break;
    }
    break;
  }
}
void enterPassword(){
  if (password.evaluate()){
      lcd.setCursor (0,0);
    lcd.print("Enter Password:");
      lcd.setCursor (0,1);
    lcd.print("Correct");
    Serial.print("Correct");
    digitalWrite(ledPin,HIGH);
    digitalWrite(spkrPin,LOW);
    password.reset();
    
    }
   else{
     digitalWrite(ledPin,HIGH);
     digitalWrite(spkrPin,HIGH);
     delay(500);
     digitalWrite(ledPin,LOW);
     digitalWrite(spkrPin,LOW);
       lcd.setCursor (0,0);
     lcd.print("Enter Password");
       lcd.setCursor (0,1);
     lcd.print("Incorrect");
     Serial.print("Incorrect");
     password.reset();
}
}

void activateAlarm(){
        lcd.setCursor (0,0);
     lcd.print("Activated");
if (digitalRead (pirPin) == LOW) {
       digitalWrite(ledPin, HIGH);   //the led visualizes the sensors output pin state
      digitalWrite(spkrPin,HIGH);
      Serial.println("movement detected");
         delay(50);
         digitalWrite(ledPin, LOW);
         digitalWrite(spkrPin,LOW);
         delay(50);
      }
  else{       
    digitalWrite(ledPin, LOW);  //the led visualizes the sensors output pin state
       digitalWrite(spkrPin,LOW);
       delay(50);
         }       
       switchState = digitalRead(reedPin);
       
      if (switchState) {
        digitalWrite(ledPin,HIGH);
        digitalWrite(spkrPin,HIGH);
      }
      else {
        digitalWrite(ledPin,LOW);
        digitalWrite(spkrPin,LOW);
      }
}

The first version only detects movement and only triggers the alarm when movement is present( I want the alarm to stay on HIGH as soon as it detects any movement) but the password part does not work (when the enter pass button is pressed the alarm will not stay on HIGH ie alarm is turned off).

When motion is detected, you turn the LED and speaker pins on, wait a very brief time, and turn them off again. This is hardly consistent with what you say you want to do.

If you want the alarm and speaker to come on when the PIR detects movement, and stay on, get rid of the else block. There is nothing to do if the PIR does not detect motion.

Also both versions dont display the user input correctly on the LCD. I am not sure how to fix it.

You'd like us to guess the difference between what you expect to see and what you actually see? I'm not into guessing games. Sorry.

PaulS:

The first version only detects movement and only triggers the alarm when movement is present( I want the alarm to stay on HIGH as soon as it detects any movement) but the password part does not work (when the enter pass button is pressed the alarm will not stay on HIGH ie alarm is turned off).

When motion is detected, you turn the LED and speaker pins on, wait a very brief time, and turn them off again. This is hardly consistent with what you say you want to do.

If you want the alarm and speaker to come on when the PIR detects movement, and stay on, get rid of the else block. There is nothing to do if the PIR does not detect motion.

Also both versions dont display the user input correctly on the LCD. I am not sure how to fix it.

You'd like us to guess the difference between what you expect to see and what you actually see? I'm not into guessing games. Sorry.

Hi PaulS sorry, I deleted that LCD part now because that is not really the main problem..and sorry if what is said is not consistent. I think I put a delay between the LED and speaker pins on and off to mimic like an alarm sound in real alarm systems. Thank you for your quick response I will try and do what you said and hopefully that should fix my problem. Thanks again. I should post back again if my problem was solved.