Home Security Alarm coding

Hey guys, I would like a bit of help with my project, I already done everything to add or fixed my code it seem not to be working for me, the output of the code is not what I want, I would like to ask if anyone can help me with my code.

Right now I have the code where after I activated my alarm it will be on 10 second countdown before activation but after the activation the buzzer just went on even the sensor did not detect any movement like seriously. I also did put delay and so on, and timing flag but it seem not working for me, I seriously don’t know what to do anymore. Can anyone help me please?

All I want is to add a 10 second countdown when the alarm is trigger like timing flag or delay or anything and also to add timer identical to the output which allow me to enter the deactivation code before the buzzer is triggered.

I put the original coding down below before I add any delay or timing flag

#include <LiquidCrystal.h> // includes the LiquidCrystal Library 
#include <Keypad.h>
#define buzzer 8
#define pir 9
int screenOffMsg =0;
String password="1234";
String tempPassword;
boolean activated = false; // State of the alarm
boolean isActivated;
boolean activateAlarm = false;
boolean alarmActivated = false;
boolean enteredPassword; // State of the entered password to stop the alarm
boolean passChangeMode = false;
boolean passChanged = false;
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char keypressed;
//define the cymbols on the buttons of the keypads
char keyMap[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};
byte rowPins[ROWS] = {14, 15, 16, 17}; //Row pinouts of the keypad
byte colPins[COLS] = {18, 19, 20, 21}; //Column pinouts of the keypad
Keypad myKeypad = Keypad( makeKeymap(keyMap), rowPins, colPins, ROWS, COLS); 
LiquidCrystal lcd(1, 2, 4, 5, 6, 7); // Creates an LC object. Parameters: (rs, enable, d4, d5, d6, d7) 
void setup() { 
  lcd.begin(16,2); 
  pinMode(buzzer, OUTPUT); // Set buzzer as an output
  pinMode(pir, INPUT); // Set trigPin as an input
  digitalWrite(pir, LOW);
}
void loop() {
  if (activateAlarm) {
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Alarm will be");
    lcd.setCursor(0,1);
    lcd.print("activated in");
   
    int countdown = 9; // 9 seconds count down before activating the alarm
    while (countdown != 0) {
      lcd.setCursor(13,1);
      lcd.print(countdown);
      countdown--;
      tone(buzzer, 700, 100);
      delay(1000);
    }
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Alarm Activated!");
    activateAlarm = false;
    alarmActivated = true;
  }
  if (alarmActivated == true)
{
      if ( digitalRead(pir)==HIGH )
      {
        tone(buzzer,1000); // Send 1KHz sound signal 
        lcd.clear();
        enterPassword();
      }
    }
  if (!alarmActivated) {
    if (screenOffMsg == 0 ){
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("A - Activate");
      lcd.setCursor(0,1);
      lcd.print("B - Change Pass");
      screenOffMsg = 1;
    }
    keypressed = myKeypad.getKey();
     if (keypressed =='A'){        //If A is pressed, activate the alarm
      tone(buzzer, 1000, 200);
      activateAlarm = true;            
    }
    else if (keypressed =='B') {
      lcd.clear();
      int i=1;
      tone(buzzer, 2000, 100);
      tempPassword = "";
      lcd.setCursor(0,0);
      lcd.print("Current Password");
      lcd.setCursor(0,1);
      lcd.print(">");
      passChangeMode = true;
      passChanged = true;   
      while(passChanged) {      
      keypressed = myKeypad.getKey();
      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, 2000, 100);
        }
      }
      if (i > 5 || keypressed == '#') {
        tempPassword = "";
        i=1;
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print("Current Password");
        lcd.setCursor(0,1);
        lcd.print(">"); 
      }
      if ( keypressed == '*') {
        i=1;
        tone(buzzer, 2000, 100);
        if (password == tempPassword) {
          tempPassword="";
          lcd.clear();
          lcd.setCursor(0,0);
          lcd.print("Set New Password");
          lcd.setCursor(0,1);
          lcd.print(">");
          while(passChangeMode) {
            keypressed = myKeypad.getKey();
            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, 2000, 100);
              }
            }
            if (i > 5 || keypressed == '#') {
              tempPassword = "";
              i=1;
              tone(buzzer, 2000, 100);
              lcd.clear();
              lcd.setCursor(0,0);
              lcd.print("Set New Password");
              lcd.setCursor(0,1);
              lcd.print(">");
            }
            if ( keypressed == '*') {
              i=1;
              tone(buzzer, 2000, 100);
              password = tempPassword;
              passChangeMode = false;
              passChanged = false;
              screenOffMsg = 0;
            }            
          }
        }
      }
    }
   }
 }
}
void enterPassword() {
  int k=5;
  tempPassword = "";
  activated = true;
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print(" *** ALARM *** ");
  lcd.setCursor(0,1);
  lcd.print("Pass>");
      while(activated) {
      keypressed = myKeypad.getKey();
      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(k,1);
          lcd.print("*");
          k++;
        }
      }
      if (k > 9 || keypressed == '#') {
        tempPassword = "";
        k=5;
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print(" *** ALARM *** ");
        lcd.setCursor(0,1);
        lcd.print("Pass>");
      }
      if ( keypressed == '*') {
        if ( tempPassword == password ) {
          activated = false;
          alarmActivated = false;
          noTone(buzzer);
          screenOffMsg = 0; 
        }
        else if (tempPassword != password) {
          lcd.setCursor(0,1);
          lcd.print("Wrong! Try Again");
          delay(2000);
          lcd.clear();
          lcd.setCursor(0,0);
          lcd.print(" *** ALARM *** ");
          lcd.setCursor(0,1);
          lcd.print("Pass>");
        }
      }    
    }
}

I did my very best and it seem like I cannot get the output that I want, I seriously very bad at this, if anyone can help me with my coding ? Please help me, Thank You.

Two things are disliked here. It's the word "urgent" and using capital letters.
Poor project planing does not give extra help. Shouting (capital letters) has the same result.

Please read the advicing topics like "How to use this forum", especially how to attach code and information.
Fritzing diagrams are useless for engineering purposes. That stuff will never run because there is no power source.

Please edit your post to add code tags.

Your procrastination does not lead to our sense of urgency.

I am so sorry that I offended you two with my post, well I'm new this forum, so I have a lot to learn, so sorry.

New game, new bets. No offence taken. Hopefully more members pic this up.
Follows the replies and update You post.

I would change this line:

LiquidCrystal lcd(1, 2, 4, 5, 6, 7); // Creates an LC object. Parameters: (rs, enable, d4, d5, d6, d7)

as D1 is used to talk to the PC for downloads and stuff.

This is running on a Mega? Inferred from the use of D20, 21 here:

byte colPins[COLS] = {18, 19, 20, 21}; //Column pinouts of the keypad

As for the on-topic question, I guess you have a logic problem somewhere, I'm a little too distracted at the moment to chase it down. Plus my internet keeps going off line, has been a problem since yesterday.

Thank you for correcting your post.

my code it seem not to be working for me, the output of the code is not what I want

You need to clearly explain what the program should do, and what it does instead.
Post examples of incorrect output.

Why do you have four variables related to "activation"? One should suffice.

boolean activated = false; // State of the alarm
boolean isActivated;
boolean activateAlarm = false;
boolean alarmActivated = false;

Well actually, I done it, I already get the outcome that I want, I miss on something and add a bit of code inside and it works fine, hahahaha, thank you for replying me but I finally did it. ??? :-[

Well done. Thanks for telling us.

I would be interested to see your final coding. I too, am working on an alarm system and hope to learn some thing from your code. Could you post the working code with the delay?

Hi, sorry for bothering you I’m also new to Arduino and recently started working on a similar project, and have failed to implement the timer for the buzzer could I please get your code.

Thanks.

Hint use NC (Normally Closed) contacts, you then get an alarm if something breaks in the wiring.

Hi, my advice is just don’t, try to do another project, this project is not worth your time, you gonna get lecture by your lecturer even though I did it, may I suggest you Pet Feeder IOI, Good Luck :relaxed:

If I needed to do that, I would start with this:

and rework it so:

event triggers alarm
log current milliseconds
( Arduino does other things )
when current milliseconds = logged milliseconds + 10 seconds
deactivate alarm

Thanks.

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