I'm a newbie in using arduino and now I'm trying to make a security system with PIR motion sensor and password system. I wrote a new codes for this project and it seems like the PIr sensor just won't go off even tho there is no motion anymore. I would appreciate your help because I had been trying to figure this out for quite a long while already and still can't seem to find the problem. I'm sorry if i chose the wrong forum section because I'm not so sure where should i post this.
Here is the code(UPDATED):
#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 (double i=0; i<0.92; i+=0.01) {
tone(speaker,sinh(i+8.294),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;
}
}
unlock();
}
/********************************FUNCTIONS*************************************/
void unlock(){
Serial.println("Check1");
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");
}
else{ //if it's false, retry
tempPassword="";
tone(speaker,500,200);
delay(300);
tone(speaker,500,200);
delay(300);
Serial.println("false");
}
}
}
}
}
//Change current password
void changePassword(){
Serial.println("check2");
retry: //label for goto
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");
goto 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");
}
}
}
}
}
}
better_password.ino (7.04 KB)