Go Down

Topic: home alarm system problem in code (Read 4494 times) previous topic - next topic

a.d

So show us the code that doesn't work?

Nick Gammon


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


...


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


Forgive me for saying it isn't clear if you have a problem, or not.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

enterpc

#17
Nov 04, 2011, 10:46 pm Last Edit: Nov 06, 2011, 09:25 am by enterpc Reason: 1
My problem is how to make the delay with milis() this is the code with millis()

Code: [Select]


/*
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();
 }

}



AWOL

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

enterpc

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

is it wrong?

AWOL

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

enterpc

#21
Nov 04, 2011, 11:23 pm Last Edit: Nov 06, 2011, 09:26 am by enterpc Reason: 1
Code: [Select]


/*
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
  unsigned long currentMillis = millis();
 
 if (run == HIGH  &&buttonState_door == LOW){
   if(currentMillis - previousMillis > interval)
   // save the last time you blinked the LED
   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();
 }

}



AWOL

Click on 'modify', highlight the code, click on the # icon, click on 'save'
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.


Nick Gammon

Please put your code inside [ code ] tags using the method described by AWOL.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

floresta

Quote
sorry i can understand you


Look at how the program code looks in your original post and compare its appearance to the other code that you have posted in your recent replies.  It is much easier to read because the moderator put it in a "Code Box" for you as mentioned in the edit that he added below the code (Moderator edit: Code put into code box. AWOL).

In reply #12 he gave you instructions on how to do this and in reply #22 he told you again.

In reply #24 you were again told how to find the instructions.


Don


enterpc

#26
Nov 04, 2011, 11:55 pm Last Edit: Nov 06, 2011, 09:26 am by enterpc Reason: 1
Code: [Select]
/*
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
  unsigned long currentMillis = millis();
 
 if (run == HIGH  &&buttonState_door == LOW){
   if(currentMillis - previousMillis > interval)
   // save the last time you blinked the LED
   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();
 }

}




enterpc

I made it  according to your instructions. can you help me now ? about my problem?

Nick Gammon

Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

enterpc

when I use the code in #26  .The ledPin_alarm start after 10 sec automatically
i need the ledpin alarm will beginning if i didn't the correct password in time 10sec

Go Up