Go Down

Topic: PIR motion sensor with password system (Read 574 times) previous topic - next topic

larryd

for (double i=0; i<0.92; i+=0.01)

Stick with integers.



No technical PMs.
If you are asked a question, please respond with an answer.
If you are asked for more information, please supply it.
If you need clarification, ask for help.

jremington

#16
Aug 22, 2019, 06:53 pm Last Edit: Aug 22, 2019, 06:58 pm by jremington
Post ALL the code. Post a wiring diagram, and links to the sensors and other modules.

Note: the keyword "void" used before a function means that the function returns no value.

Quote
I'm in hurry with this project
That is part of your problem, but not our problem. You need to take some time, learn the programming language and for posting, read the "How to use this forum" sticky.

Delta_G

Code: [Select]
while(!checkPassword){

Was that supposed to be a function call.  You know with parenthesis on the end? 
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

wvmarle

Get rid of all those goto statements, and post your (now untangled and much more readable) code.

Fair chance that in the process of untangling this mess your problems disappear.
Quality of answers is related to the quality of questions. Good questions will get good answers. Useless answers are a sign of a poor question.

Aquoter

#19
Aug 23, 2019, 01:56 pm Last Edit: Aug 23, 2019, 02:10 pm by Aquoter
Code: [Select]
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>

/*------------------------------PIR------------------------------------------*/
int i;
int led = 10;
int sensor = 11;
int state = LOW;
int val = 0;
int speaker = 12;

/*-------------------------------KEYPAD---------------------------------------*/
const byte numRows= 4; //number of rows on the keypad
const byte numCols= 4; //number of columns on the keypad
char keypressed;
char keymap[numRows][numCols]=
{
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};

byte rowPins[numRows] = {5 ,4 ,3 ,2 }; //2=r1,3=r2 ( from left to right = decrease )
byte colPins[numCols] = {9 ,8 ,7 ,6 }; //9=c1,8=c2 ( from left to right = increase )
   
Keypad myKeypad= Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);

/*-------------------------------CONSTANTS------------------------------------*/
LiquidCrystal_I2C lcd(0x27,16,2);  //LCD

/*-------------------------------VARIABLES------------------------------------*/
String password="2580"; //Variable to store the current password
String tempPassword=""; //Variable to store the input password
int doublecheck;    //Check twice the new passoword
boolean armed = false;  //Variable for system state (armed:true / unarmed:false)
boolean input_pass;   //Variable for input password (correct:true / wrong:false)
boolean storedPassword = true;
boolean changedPassword = false;
boolean checkPassword = false;
int j = 1; //variable to index an array
/*----------------------------------------------------------------------------*/

void setup(){
  lcd.init(); //Setup the LCD's number of columns and rows
  //Print welcome message...
  lcd.backlight();
  lcd.setCursor(0,0);
  lcd.print("Arduino Security");
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print(" opener system  ");
  lcd.setCursor(0,1);
  lcd.print(" with password  ");
  delay(1000);
  lcd.clear();
  pinMode(led, OUTPUT);
  pinMode(sensor, INPUT);
  Serial.begin(9600);
}

void loop() { //Main loop
  val = digitalRead(sensor);
  if (val == HIGH) {
      Serial.println("a");
    digitalWrite(led, HIGH);
    delay(200);
    for (i=700; i<800; i+=2) {
      tone(speaker,i,10);
      delay(1);
    }
   
    if (state == LOW) {
      Serial.println("ALERT");
      state = HIGH;
    }
  }
 
  else {
    digitalWrite(led,LOW);
    digitalWrite(speaker,LOW);

    if (state == HIGH) {
      Serial.println("ESCAPED");
      state = LOW;
    }
  }

/********************************FUNCTIONS*************************************/
  Serial.println("Check1");
  retry:
  tempPassword="";
  lcd.clear();
  j=6;
  while(!checkPassword){
    lcd.setCursor(0,0);
    lcd.print("Open the door:  ");
    lcd.setCursor(0,1);
    lcd.print("PASS>");
    keypressed = myKeypad.getKey();   //Read pressed keys
    if (keypressed != NO_KEY){    //Accept only numbers and * from keypad
      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(j,1);
        lcd.print("*");       //Put * on lcd
        j++;
        Serial.println("LockAgain");
      }
      else if (keypressed == 'A'){
        changePassword();
        Serial.println("A Pressed");
      }
      else if (keypressed=='#'){
        lcd.setCursor(0,1);
        lcd.clear();
        Serial.println("# pressed");
      }
      else if (keypressed == '*'){  //Check for password
        if (password==tempPassword){//If it's correct...
          lcd.clear();     
          lcd.setCursor(0,0);
          lcd.print("Correct password");
          lcd.setCursor(0,1);
          lcd.print("Door is unlocked");
          digitalWrite(led, LOW);
          digitalWrite(speaker,LOW);
          delay(5000);
          Serial.println("* pressed");
          goto retry;
         
        }
        else{           //if it's false, retry
          tempPassword="";
          tone(speaker,500,200);
          delay(300);
          tone(speaker,500,200);
          delay(300);
          Serial.println("false");
          goto retry;
        }       
      }
    }
  }
}


//Change current password
void changePassword(){
    Serial.println("check2");
  tempPassword="";
  lcd.clear();
  j=1;
  while(!changedPassword){        //Waiting for current password
    keypressed = myKeypad.getKey();   //Read pressed keys
    lcd.setCursor(0,0);
    lcd.print("CURRENT PASSWORD");
    lcd.setCursor(0,1);
    lcd.print(">");
    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(j,1);
        lcd.print("*");
        j++;
        Serial.println("changePassword");   
      }
      else if (keypressed=='#'){
        break;
        Serial.println("CP#");
      }
      else if (keypressed == '*'){
        j=1;
        if (password==tempPassword){
          storedPassword=false;
          newPassword();          //Password is corrent, so call the newPassword function
          break;
          Serial.println("CP*");
        }
        else{               //Try again
          tempPassword="";
          Serial.println("REtry");
        }
      }
    }
  }
}
String firstpass;


//Setup new password
void newPassword(){
  Serial.println("check3");
  tempPassword="";
  changedPassword=false;
  lcd.clear();
  j=1;
  while(!storedPassword){
    keypressed = myKeypad.getKey();   //Read pressed keys
    if (doublecheck==0){
      lcd.setCursor(0,0);
      lcd.print("SET NEW PASSWORD");
      lcd.setCursor(0,1);
      lcd.print(">");
      Serial.println("NewPAss");
    }
    else{
      lcd.setCursor(0,0);
      lcd.print("One more time...");
      lcd.setCursor(0,1);
      lcd.print(">");
      Serial.println("LCD");
    }
    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(j,1);
        lcd.print("*");
        j++;
        Serial.println("check");
      }
      else if (keypressed=='#'){
        break;
        Serial.println("NP#");
      }
      else if (keypressed == '*'){
        if (doublecheck == 0){
          firstpass=tempPassword;
          doublecheck=1;
          newPassword();
          Serial.println("NP*");
        }
        if (doublecheck==1){
          doublecheck=0;
          if (firstpass==tempPassword){
            j=1;
            firstpass="";
            password = tempPassword; // New password saved
            tempPassword="";//erase temp password
            lcd.setCursor(0,0);
            lcd.print("PASSWORD CHANGED");
            lcd.setCursor(0,1);
            lcd.print("----------------");
              storedPassword=true;
              delay(2000);
              lcd.clear();
              break;
              Serial.println("doublecheck");
          }
          else{
            firstpass="";
            newPassword();
            Serial.println("retry");
          }
        }
      }
    }
  }
}



This is the full code.

Aquoter

Code: [Select]
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>

/*------------------------------PIR------------------------------------------*/
int i;
int led = 10;
int sensor = 11;
int state = LOW;
int val = 0;
int speaker = 12;

/*-------------------------------KEYPAD---------------------------------------*/
const byte numRows= 4; //number of rows on the keypad
const byte numCols= 4; //number of columns on the keypad
char keypressed;
char keymap[numRows][numCols]=
{
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};

byte rowPins[numRows] = {5 ,4 ,3 ,2 }; //2=r1,3=r2 ( from left to right = decrease )
byte colPins[numCols] = {9 ,8 ,7 ,6 }; //9=c1,8=c2 ( from left to right = increase )
   
Keypad myKeypad= Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);

/*-------------------------------CONSTANTS------------------------------------*/
LiquidCrystal_I2C lcd(0x27,16,2);  //LCD

/*-------------------------------VARIABLES------------------------------------*/
String password="2580"; //Variable to store the current password
String tempPassword=""; //Variable to store the input password
int doublecheck;    //Check twice the new passoword
boolean armed = false;  //Variable for system state (armed:true / unarmed:false)
boolean input_pass;   //Variable for input password (correct:true / wrong:false)
boolean storedPassword = true;
boolean changedPassword = false;
boolean checkPassword = false;
int j = 1; //variable to index an array
/*----------------------------------------------------------------------------*/

void setup(){
  lcd.init(); //Setup the LCD's number of columns and rows
  //Print welcome message...
  lcd.backlight();
  lcd.setCursor(0,0);
  lcd.print("Arduino Security");
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print(" opener system  ");
  lcd.setCursor(0,1);
  lcd.print(" with password  ");
  delay(1000);
  lcd.clear();
  pinMode(led, OUTPUT);
  pinMode(sensor, INPUT);
  Serial.begin(9600);
}

void loop() { //Main loop
  val = digitalRead(sensor);
  if (val == HIGH) {
      Serial.println("a");
    digitalWrite(led, HIGH);
    delay(200);
    for (i=700; i<800; i+=2) {
      tone(speaker,i,10);
      delay(1);
    }
   
    if (state == LOW) {
      Serial.println("ALERT");
      state = HIGH;
    }
  }
 
  else {
    digitalWrite(led,LOW);
    digitalWrite(speaker,LOW);

    if (state == HIGH) {
      Serial.println("ESCAPED");
      state = LOW;
    }
  }

/********************************FUNCTIONS*************************************/
  Serial.println("Check1");
  retry:
  tempPassword="";
  lcd.clear();
  j=6;
  while(!checkPassword){
    lcd.setCursor(0,0);
    lcd.print("Open the door:  ");
    lcd.setCursor(0,1);
    lcd.print("PASS>");
    keypressed = myKeypad.getKey();   //Read pressed keys
    if (keypressed != NO_KEY){    //Accept only numbers and * from keypad
      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(j,1);
        lcd.print("*");       //Put * on lcd
        j++;
        Serial.println("LockAgain");
      }
      else if (keypressed == 'A'){
        changePassword();
        Serial.println("A Pressed");
      }
      else if (keypressed=='#'){
        lcd.setCursor(0,1);
        lcd.clear();
        Serial.println("# pressed");
      }
      else if (keypressed == '*'){  //Check for password
        if (password==tempPassword){//If it's correct...
          lcd.clear();     
          lcd.setCursor(0,0);
          lcd.print("Correct password");
          lcd.setCursor(0,1);
          lcd.print("Door is unlocked");
          digitalWrite(led, LOW);
          digitalWrite(speaker,LOW);
          delay(5000);
          Serial.println("* pressed");
          goto retry;
         
        }
        else{           //if it's false, retry
          tempPassword="";
          tone(speaker,500,200);
          delay(300);
          tone(speaker,500,200);
          delay(300);
          Serial.println("false");
          goto retry;
        }       
      }
    }
  }
}


//Change current password
void changePassword(){
    Serial.println("check2");
  tempPassword="";
  lcd.clear();
  j=1;
  while(!changedPassword){        //Waiting for current password
    keypressed = myKeypad.getKey();   //Read pressed keys
    lcd.setCursor(0,0);
    lcd.print("CURRENT PASSWORD");
    lcd.setCursor(0,1);
    lcd.print(">");
    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(j,1);
        lcd.print("*");
        j++;
        Serial.println("changePassword");   
      }
      else if (keypressed=='#'){
        break;
        Serial.println("CP#");
      }
      else if (keypressed == '*'){
        j=1;
        if (password==tempPassword){
          storedPassword=false;
          newPassword();          //Password is corrent, so call the newPassword function
          break;
          Serial.println("CP*");
        }
        else{               //Try again
          tempPassword="";
          Serial.println("REtry");
        }
      }
    }
  }
}
String firstpass;


//Setup new password
void newPassword(){
  Serial.println("check3");
  tempPassword="";
  changedPassword=false;
  lcd.clear();
  j=1;
  while(!storedPassword){
    keypressed = myKeypad.getKey();   //Read pressed keys
    if (doublecheck==0){
      lcd.setCursor(0,0);
      lcd.print("SET NEW PASSWORD");
      lcd.setCursor(0,1);
      lcd.print(">");
      Serial.println("NewPAss");
    }
    else{
      lcd.setCursor(0,0);
      lcd.print("One more time...");
      lcd.setCursor(0,1);
      lcd.print(">");
      Serial.println("LCD");
    }
    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(j,1);
        lcd.print("*");
        j++;
        Serial.println("check");
      }
      else if (keypressed=='#'){
        break;
        Serial.println("NP#");
      }
      else if (keypressed == '*'){
        if (doublecheck == 0){
          firstpass=tempPassword;
          doublecheck=1;
          newPassword();
          Serial.println("NP*");
        }
        if (doublecheck==1){
          doublecheck=0;
          if (firstpass==tempPassword){
            j=1;
            firstpass="";
            password = tempPassword; // New password saved
            tempPassword="";//erase temp password
            lcd.setCursor(0,0);
            lcd.print("PASSWORD CHANGED");
            lcd.setCursor(0,1);
            lcd.print("----------------");
              storedPassword=true;
              delay(2000);
              lcd.clear();
              break;
              Serial.println("doublecheck");
          }
          else{
            firstpass="";
            newPassword();
            Serial.println("retry");
          }
        }
      }
    }
  }
}




There's a some goto statements I can't removed right now because it will print wrong sentences in the LCD display.


dougp

There's a some goto statements I can't removed right now because it will print wrong sentences in the LCD display.
Sometimes you have to take two steps back to go one step forward.  Bite the bullet.
Everything we call real is made of things that cannot be regarded as real.  If quantum mechanics hasn't profoundly shocked you, you haven't understood it yet. - Niels Bohr

No private consultations undertaken!

wvmarle

I don't see any "steps back" involved when it comes to removing goto statements from code and making it more readable.
Quality of answers is related to the quality of questions. Good questions will get good answers. Useless answers are a sign of a poor question.

Aquoter

I don't see any "steps back" involved when it comes to removing goto statements from code and making it more readable.
Nvm I removed all the "goto" statements and made some small changes for the LCD to display the sentences i want.

jremington

#24
Aug 23, 2019, 07:23 pm Last Edit: Aug 23, 2019, 07:23 pm by jremington
Thanks for posting the full code. When you get around to it, post a wiring diagram and links to the sensors.

Post the evidence demonstrating that "loop isn't looping".

wvmarle

So is it working now as you want?

If not, feel free to post the updated code with clear description of what it does and how that's different from what you want it to do.
Quality of answers is related to the quality of questions. Good questions will get good answers. Useless answers are a sign of a poor question.

Aquoter

So is it working now as you want?

If not, feel free to post the updated code with clear description of what it does and how that's different from what you want it to do.
Nope, the sensor only detects motion at the very first moment when the code uploaded.

Aquoter

#27
Aug 23, 2019, 09:58 pm Last Edit: Aug 23, 2019, 09:59 pm by Aquoter
Thanks for posting the full code. When you get around to it, post a wiring diagram and links to the sensors.

Post the evidence demonstrating that "loop isn't looping".
The Serial.println that i added never get to print out again although normally it will keep looping.

TheMemberFormerlyKnownAsAWOL

#28
Aug 23, 2019, 10:07 pm Last Edit: Aug 23, 2019, 10:08 pm by TheMemberFormerlyKnownAsAWOL
Code: [Select]
retry:
Uh-oh.

Blackfin

You initialize checkPassword at the top of your code:

Code: [Select]
boolean checkPassword = false;

and in loop() have a while loop:

Code: [Select]
while (!checkPassword)

but I don't see anywhere in your code where checkPassword is set to "true".

BTW, you might consider re-writing this using a state machine style of coding rather than umpteen else/ifs and goto statements.

Go Up