home alarm system problem in code

(i would if i’m in situation alarm on in alarm out situation
if the door open i don’t press the correct password the alarm begin
after alarm begin if i press the correct password i’ll go in situation alarm off)

this is the code

#include <Password.h>

#include <Keypad.h>

Password password = Password( "7777" );

const byte ROWS = 4; 
const byte COLS = 4; 
// Define the Keymap
char keys[ROWS][COLS] = {
  {'1','2','3', 'A'},
  {'4','5','6', 'B'},
  {'7','8','9', 'C'},
  {'*','0','#', 'D'}
};

byte rowPins[ROWS] = {  8, 7, 6,5 };
byte colPins[COLS] = { 12, 11, 10,9 };


// Create the Keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

#define ledPin 13

#include <LiquidCrystal.h>

LiquidCrystal lcd(4, 3, 2, 1, 0,14);

const int buttonPin_DOOR =15;    // the number of the pushbutton pin
const int buttonPin_WINDOWS=16;
const int buttonPin_ON_IN=17;
const int buttonPin_ON_OUT=19;
const int ledPin_ALARM= 18;
int ON_IN=0;
int ON_OUT=0;

int buttonState_DOOR = 1;         // variable for reading the pushbutton status
int buttonState_WINDOWS= 1;
int buttonState_ON_IN= 0;
int buttonState_ON_OUT= 0;

 

void setup(){
  
  pinMode(buttonPin_DOOR, INPUT);  
 pinMode(buttonPin_WINDOWS,INPUT);
pinMode(buttonPin_ON_IN,INPUT);
pinMode(buttonPin_ON_OUT ,INPUT);
  pinMode(ledPin_ALARM ,  OUTPUT);
      lcd.begin(16, 2);
  lcd.setCursor(0,0);

 
  digitalWrite(ledPin, LOW);   

  keypad.addEventListener(keypadEvent); 
  keypad.setDebounceTime(250);
  
 
}

void loop(){
   buttonState_DOOR = digitalRead(buttonPin_DOOR);
   buttonState_WINDOWS = digitalRead(buttonPin_WINDOWS);
   buttonState_ON_IN= digitalRead(buttonPin_ON_IN);
   buttonState_ON_OUT= digitalRead(buttonPin_ON_OUT);
   
   
   
   if (buttonState_ON_IN == HIGH  &&buttonState_WINDOWS == LOW) {
    
  lcd.setCursor(0,0);
  lcd.print("OPEN WINDOWS     ");
    lcd.setCursor(0,1);
  lcd.print("                                  ");
   }
    if (buttonState_ON_IN == HIGH  &&buttonState_DOOR  == LOW) {
    
  lcd.setCursor(0,0);
  lcd.print("OPEN DOOR     ");
  lcd.setCursor(0,1);
  lcd.print("                                  ");
   }
   
    if (buttonState_ON_IN == HIGH  &&buttonState_WINDOWS == LOW && buttonState_DOOR  == LOW) {
    
  lcd.setCursor(0,0);
  lcd.print("OPEN WINDOWS AND ");
  lcd.setCursor(0,1);
  lcd.print("OPEN DOOR     ");
   }
   if (buttonState_ON_IN == HIGH  &&buttonState_DOOR  == HIGH &&buttonState_WINDOWS  == HIGH) {
      lcd.setCursor(0,0);
  lcd.print("ALARM ON        ");
  lcd.setCursor(0,1);
  lcd.print("ALARM      ");
(ON_IN = HIGH);
   }
    if (ON_IN == HIGH  &&buttonState_DOOR == LOW){
      digitalWrite(ledPin_ALARM,HIGH);
      lcd.setCursor(0,0);
  lcd.print("ALARM          ");
  lcd.setCursor(0,1);
  lcd.print("                                  ");
  
    }
      if (ON_IN == HIGH  && buttonState_WINDOWS  == LOW){
        digitalWrite(ledPin_ALARM,HIGH);
      lcd.setCursor(0,0);
  lcd.print("ALARM          ");
  lcd.setCursor(0,1);
  lcd.print("                                  ");
  
    }
      if (buttonState_ON_OUT == HIGH  &&buttonState_WINDOWS == LOW) {
    
  lcd.setCursor(0,0);
  lcd.print("OPEN WINDOWS     ");
    lcd.setCursor(0,1);
  lcd.print("                                  ");
   }
    if (buttonState_ON_OUT == HIGH  &&buttonState_DOOR  == LOW) {
    
  lcd.setCursor(0,0);
  lcd.print("OPEN DOOR     ");
  lcd.setCursor(0,1);
  lcd.print("                                  ");
   }
   
    if (buttonState_ON_OUT == HIGH  &&buttonState_WINDOWS == LOW && buttonState_DOOR  == LOW) {
    
  lcd.setCursor(0,0);
  lcd.print("OPEN WINDOWS AND ");
  lcd.setCursor(0,1);
  lcd.print("OPEN DOOR     ");
   }
   if (buttonState_ON_OUT == HIGH  &&buttonState_DOOR  == HIGH &&buttonState_WINDOWS  == HIGH) {
      lcd.setCursor(0,0);
  lcd.print("ALARM ON        ");
  lcd.setCursor(0,1);
  lcd.print("ALARM      ");
(ON_OUT = HIGH);
   }
    if (ON_OUT == HIGH  &&buttonState_DOOR == LOW){
(i would if i'm in situation alarm on  in  alarm out situation
if the door open i don't press the correct password the alarm begin 
after alarm begin if i press the correct password i'll go in situation alarm off)

      digitalWrite(ledPin_ALARM,HIGH);
      lcd.setCursor(0,0);
  lcd.print("ALARM          ");
  lcd.setCursor(0,1);
  lcd.print("                                  ");
  
    }
      if (ON_OUT == HIGH  && buttonState_WINDOWS  == LOW){
        digitalWrite(ledPin_ALARM,HIGH);
      lcd.setCursor(0,0);
  lcd.print("ALARM          ");
  lcd.setCursor(0,1);
  lcd.print("                                  ");
  
    }
  
  
  keypad.getKey();
}


void keypadEvent(KeypadEvent eKey){
  switch (keypad.getState()){
    case PRESSED:
	
	switch (eKey){
	  case '#': guessPassword(); break;
	   default:
		   password.append(eKey);
  }
}
 
   
}





void guessPassword(){
     
     if (password.evaluate()){
       digitalWrite(ledPin_ALARM,LOW);
       (ON_IN   = LOW);
       (ON_OUT = LOW);
       
       lcd.begin(16, 2);
  lcd.setCursor(0,0);
  lcd.print("CORRECT PASSWORD ");
  delay(1000);
       lcd.setCursor(0,0);
  lcd.print("ALARM                    ");
  lcd.setCursor(0,1);
  lcd.print("OFF                        ");
  
       	     
	  
              password.reset(); 
     }else{
	     


	     
       lcd.begin(16, 2);
  lcd.setCursor(0,0);
  lcd.print("WRONG PASSWORD ");
              password.reset(); 
     }
   
     }

Moderator edit: Code put into code box. AWOL

What is the question?

(i would if i'm in situation alarm on in alarm out situation if the door open i don't press the correct password the alarm begin after alarm begin if i press the correct password i'll go in situation alarm off)

how i can do it ? it isn't a simple delay(10000)

enterpc: (i would if i'm in situation alarm on in alarm out situation if the door open i don't press the correct password the alarm begin after alarm begin if i press the correct password i'll go in situation alarm off)

This does not make sense - rewrite it so that there is no chance of confusion.

A numbered list might work well.

You seem to have already written a whole lot of code. Please explain what you expect it to do and what it does instead. Point of where in the code you think the problem resides.

When you explain your problem, using proper sentences (capital letters and punctuation) adds readability.

enterpc: this is the code

Can you post code that actually compiles? This doesn't look as if it has a hope of compiling:

    if (ON_OUT == HIGH  &&buttonState_DOOR == LOW){
(i would if i'm in situation alarm on  in  alarm out situation
if the door open i don't press the correct password the alarm begin 
after alarm begin if i press the correct password i'll go in situation alarm off)

      digitalWrite(ledPin_ALARM,HIGH);       lcd.setCursor(0,0);

I am not familiar with this coding style:

       (ON_IN   = LOW);
       (ON_OUT = LOW);

Why are you putting things in brackets?

You have to break your project down into small steps and get each part working in sequence.

Use an LED to represent the alarm and a push button to represent the magnetic sensor (switch) on your door. (1) LED on with door open and off with door closed.

Add push buttons to represent additional doors and windows. (2) LED OFF when all doors and windows are closed, LED goes on when any door and/or window is open. (3) LED OFF when all doors and windows are closed, LED goes on when any door and/or window is open, LED stays on even if all doors and windows are then closed.

Add a switch to arm / disarm the alarm. (4) LED works as in (3) when the alarm is armed, LED stays off even when doors and/or window is open when alarm is disarmed.

Add a keypad. (5) Correct sequence on keypad arms / disarms the alarm

Add an LCD display. (6) Alarm status indicated on LCD display

Don't forget to debounce your door and window switches and your keypad.

Don

I have tried these .I will try to describe exactly my problem

1) When the alarm is activated and the window open. start the alarm siren if I press the correct code stop the alarm siren and turn off the system (this is ok )

2) When the alarm is activated and door open. Wait 10 sec and if i dont press the correct code in 10 sec, start the alarm siren. If I press the correct code in 10 sec . Stop the alarm siren and turn off the system. After the 10 sec if I press the correct code stop the alarm siren

The second piece is the my problem i nead a delay of 10 sec

The second piece is the my problem i nead a delay of 10 sec

Why didn't you ask that in the first place?

First figure out how many milliseconds you need to delay. Then look at the 'Blink without Delay' Tutorial.

By the way.... Two out of the three words in the title of this tutorial are misleading so you might initially think it is not of much use for your application. They are blinking an LED but this is just representing that they are doing one thing and then later on they are doing something else. They are NOT doing this without a delay, they are doing this without using the delay() routine - which is a lot different. The tutorial should really be called something like 'Wait without using delay()'.

Don

if I understand you correctly . you're saying me to use the function millis()

if I understand you correctly . you're saying me to use the function millis()

Yes!

Don

My problem is that when I open the door I need delay 10 sec i tried the millis() but nothing this is my code

/*
This is a simple alarm system 
that is activated by pressing a button
and  deactivated when you press the correct code
*/
#include <Password.h>

#include <Keypad.h>

Password password = Password( "7777" );

const byte ROWS = 4; 
const byte COLS = 4; 
// Define the Keymap
char keys[ROWS][COLS] = {
  {
    '1','2','3', 'A'  }
  ,
  {
    '4','5','6', 'B'  }
  ,
  {
    '7','8','9', 'C'  }
  ,
  {
    '*','0','#', 'D'  }
};

byte rowPins[ROWS] = {  
  8, 7, 6,5 };
byte colPins[COLS] = { 
  12, 11, 10,9 };


// Create the Keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );



#include <LiquidCrystal.h>

LiquidCrystal lcd(4, 3, 2, 1, 0,14);

const int buttonPin_door =15;    // Magnetic door contact
const int buttonPin_on=17;       // Power system  button
const int ledPin_alarm= 18;      // Alarm Led
int run =0;

int buttonState_door = 1;         // Door initial state
int buttonState_on= 0;            // Power system  button initial state 



void setup(){

  pinMode(buttonPin_door, INPUT);  
  pinMode(buttonPin_on,INPUT);
  pinMode(ledPin_alarm ,  OUTPUT);
  lcd.begin(16, 2);
  lcd.setCursor(0,0);


  

  keypad.addEventListener(keypadEvent); 
  keypad.setDebounceTime(250);           //Keypad button time


}

void loop(){
  buttonState_door = digitalRead(buttonPin_door);  
  buttonState_on= digitalRead(buttonPin_on);

//conditions for  activation the  system

  if (buttonState_on == HIGH  && buttonState_door  == HIGH ) {
    lcd.setCursor(0,0);
    lcd.print("run         ");

    run = HIGH;
  }
  
  // conditions when you return home and open the door
  
  if (run == HIGH  &&buttonState_door == LOW){
    digitalWrite(ledPin_alarm,HIGH);
    lcd.setCursor(0,0);
    lcd.print("alarm...          ");
    lcd.setCursor(0,1);
    lcd.print("                                  ");
  }
  
  


  keypad.getKey();
}


void keypadEvent(KeypadEvent eKey){
  switch (keypad.getState()){
  case PRESSED:

    switch (eKey){
    case '#': 
      guessPassword(); 
      break;
    default:
      password.append(eKey);
    }
  }


}





void guessPassword(){

  if (password.evaluate()){
    digitalWrite(ledPin_alarm,LOW);
    (run = LOW);
    lcd.begin(16, 2);
    lcd.setCursor(0,0);
    lcd.print("CORRECT PASSWORD ");
    delay(1000);
    lcd.setCursor(0,0);
    lcd.print("system          ");
    lcd.setCursor(0,1);
    lcd.print("off       ");



    password.reset(); 
  }
  else{




    lcd.begin(16, 2);
    lcd.setCursor(0,0);
    lcd.print("WRONG PASSWORD ");
    password.reset(); 
  }

}

You're going to have to start posting your code correctly. Use the # icon on the editors toolbar. You can (and should) go back and edit your posts. Highlight the code, then click on the # icon.

My problem is that when I open the door I need delay 10 sec i tried the millis() but nothing this is my code

I didn't find millis() anywhere in your code.

Don

yes this code work ok. It's without millis() if i use millis() i have problem

So show us the code that doesn't work?

enterpc: My problem is that when I open the door I need delay 10 sec i tried the millis() but nothing this is my code

...

enterpc: yes this code work ok. It's without millis()

Forgive me for saying it isn't clear if you have a problem, or not.

My problem is how to make the delay with milis() this is the code with millis()

/*
This is a simple alarm system 
that is activated by pressing a button
and  deactivated when you press the correct code
*/
#include <Password.h>

#include <Keypad.h>

Password password = Password( "7777" );

const byte ROWS = 4; 
const byte COLS = 4; 
// Define the Keymap
char keys[ROWS][COLS] = {
  {
    '1','2','3', 'A'  }
  ,
  {
    '4','5','6', 'B'  }
  ,
  {
    '7','8','9', 'C'  }
  ,
  {
    '*','0','#', 'D'  }
};

byte rowPins[ROWS] = {  
  8, 7, 6,5 };
byte colPins[COLS] = { 
  12, 11, 10,9 };


// Create the Keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );



#include <LiquidCrystal.h>

LiquidCrystal lcd(4, 3, 2, 1, 0,14);

const int buttonPin_door =15;    // Magnetic door contact
const int buttonPin_on=17;       // Power system  button
const int ledPin_alarm= 18;      // Alarm Led
int run =0;

int buttonState_door = 1;         // Door initial state
int buttonState_on= 0;            // Power system  button initialstate 
long previousMillis = 0;
long interval = 10000;


void setup(){

  pinMode(buttonPin_door, INPUT);  
  pinMode(buttonPin_on,INPUT);
  pinMode(ledPin_alarm ,  OUTPUT);
  lcd.begin(16, 2);
  lcd.setCursor(0,0);


  

  keypad.addEventListener(keypadEvent); 
  keypad.setDebounceTime(250);           //Keypad button time


}

void loop(){
  buttonState_door = digitalRead(buttonPin_door);  
  buttonState_on= digitalRead(buttonPin_on);

//conditions for  activation the  system

  if (buttonState_on == HIGH  && buttonState_door  == HIGH ) {
    lcd.setCursor(0,0);
    lcd.print("run         ");

    run = HIGH;
  }
  
  // conditions when you return home and open the door
  
  if (run == HIGH  &&buttonState_door == LOW){
    if(currentMillis - previousMillis > interval) {
      previousMillis = currentMillis; 
      if (run == HIGH);
      
    digitalWrite(ledPin_alarm,HIGH);
    lcd.setCursor(0,0);
    lcd.print("alarm...          ");
    lcd.setCursor(0,1);
    lcd.print("                                  ");
  }
  
  


  keypad.getKey();
}


void keypadEvent(KeypadEvent eKey){
  switch (keypad.getState()){
  case PRESSED:

    switch (eKey){
    case '#': 
      guessPassword(); 
      break;
    default:
      password.append(eKey);
    }
  }


}





void guessPassword(){

  if (password.evaluate()){
    digitalWrite(ledPin_alarm,LOW);
    (run = LOW);
    lcd.begin(16, 2);
    lcd.setCursor(0,0);
    lcd.print("CORRECT PASSWORD ");
    delay(1000);
    lcd.setCursor(0,0);
    lcd.print("system          ");
    lcd.setCursor(0,1);
    lcd.print("off       ");



    password.reset(); 
  }
  else{




    lcd.begin(16, 2);
    lcd.setCursor(0,0);
    lcd.print("WRONG PASSWORD ");
    password.reset(); 
  }

}
if (run == HIGH);

Why?

if run == low dont digitalWrite(ledPin_alarm,HIGH); only if run = high digitalWrite(ledPin_alarm,HIGH);

is it wrong?