sorry its kinda of long, just wanted to show the part of the problem.
#include <Password.h>
#include <LiquidCrystal.h>
#include <Keypad.h>
const int RED = 42; //RED LED
const int intervalMinor = 100; // number of millisecs that LED is off between flashes
const int intervalMajor = 200; // number of millisecs that LED is off between cycles
const int blinkDuration = 80; // number of millisecs that LED is on
const int flashesInCycle = 3; // number of flashes inside a cycle
const int GREEN = 40; //GREEN LED
const int Buzz = 10; // Buzzer
const int YELLOW = 44; // YELLOW LED to test PIR Sensor
const int PIR = 50; // PIR Sensor
const int relay = 37;
int horizPos = 5;
boolean activateMotion = false;
boolean MalarmActivated = false;
boolean activated = false; //variable to activate
boolean alarm = false;
int var = 0;
int countdown = 9; // 9 second count down before activating the alarm
byte targetLedState = HIGH; // this variable is necesary to help arduino monitor the status of LED
//the initial condition (t=0) can be either HIGH or LOW
unsigned long currentMillis = 0; // stores the value of millis() in each iteration of loop()
unsigned long previousLedMillis = 0; // will store last time the LED was updated
int flashCount = 0;
unsigned long lcdinterval = 2000;
unsigned long LcdMillis = 0;
unsigned long currentMillis2 = 0;
LiquidCrystal lcd(22, 24, 26, 28, 30, 32); // initialize the library with the numbers of the interface pins
Password password = Password("1234"); //PASSWORD TO ACTIVATE MOTION ALARM
Password password2 = Password("A"); //PASSWORD TO DEACTIVATE MOTION ALARM
const byte ROWS = 4; // Four rows
const byte COLS = 4; // Four columns
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = { 5, 4, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = { 9, 8, 7, 6}; //connect to the column pinouts of the keypad
// Create the Keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS);
void setup() {
Serial.begin(9600);
lcd.begin(16, 2);
keypad.addEventListener(keypadEvent);
pinMode(GREEN, OUTPUT); // Green Light
pinMode(RED, OUTPUT); // Red Light
pinMode (YELLOW, OUTPUT); // Yellow light to test PIR
pinMode(Buzz, OUTPUT); // Piezo buzzer
pinMode(PIR, INPUT); // PIR sensor
pinMode(relay, OUTPUT);
ShowEnterPass();
}
void ShowEnterPass() {
lcd.clear();
lcd.setCursor(1, 0);
lcd.print(" PLEASE ");
lcd.setCursor(0, 1);
lcd.print(" ENTER PASSWORD ");
delay(2000);
lcd.clear();
}
void loop() {
keypad.getKey();
if (activated == true) {
activateMotion = true;
}
if (activateMotion == true) {
lcd.setCursor(0, 0);
lcd.print(" MOTION ALARM");
lcd.setCursor(0, 1);
lcd.print("ACTIVATING IN");
int countdown = 9; // 9 second count down before activating the alarm
while (countdown != 0) {
lcd.setCursor(14, 1);
lcd.print(countdown);
countdown--;
tone(Buzz, 700, 100);
delay(1000);
}
lcd.clear();
lcd.setCursor(0, 0);
digitalWrite(RED, HIGH);
lcd.print(" MOTION ALARM");
lcd.setCursor(0, 1);
lcd.print(" ACTIVATED");
tone(Buzz, 1110, 700);
delay(2500);
digitalWrite(RED, LOW);
lcd.clear();
activateMotion = false;
MalarmActivated = true;
activated = false;
}
if (MalarmActivated == true) {
if (digitalRead(PIR) == HIGH) {
MotionAlarmTriggered();
Serial.println(currentMillis2 / 1000);
switch (var) {
case 0:
lcd.setCursor(0, 0);
lcd.print(" *** ALARM *** ");
if (digitalRead(PIR) == LOW) {
lcd.clear();
}
break;
case 1:
currentMillis2 = millis();
lcd.setCursor(0, 0);
lcd.print("PASSWORD INVALID");
lcd.setCursor(0, 1);
lcd.print(" TRY AGAIN !");
if (currentMillis2 - LcdMillis >= lcdinterval) {
currentMillis2 = LcdMillis;
lcd.clear();
var = 0;
}
break;
default:
break;
}
} else {
Serial.println("MOTION STOPPED");
digitalWrite(relay, LOW); digitalWrite(RED, LOW);
}
}
} //void loop ends
void MotionAlarmTriggered() {
currentMillis = millis(); // capture the latest value of millis()
alarm = true;
digitalWrite(relay, HIGH);
playtone();
int offInterval ;
if (targetLedState == LOW) { //if the LED is OFF
Serial.println("LOW");
offInterval = (flashCount < flashesInCycle) ? intervalMinor : intervalMajor; //choose interval value
if ((unsigned long)currentMillis - previousLedMillis >= offInterval) {
targetLedState = HIGH;
digitalWrite(RED, targetLedState);
previousLedMillis += offInterval;
flashCount = (flashCount < flashesInCycle) ? (flashCount += 1) : 0; //update the counter
}
}
else { // i.e. if onBoardLedState is HIGH (if the LED is ON)
Serial.println("HIGH");
if ((unsigned long)currentMillis - previousLedMillis >= blinkDuration) {
targetLedState = LOW;
digitalWrite(RED, targetLedState);
previousLedMillis += blinkDuration;
}
}
}
void playtone() {
tone(Buzz, 200, 160);
}
void keypadEvent(KeypadEvent eKey) {
lcd.setCursor(0, 1);
lcd.print("PASS:");
switch (keypad.getState()) {
case PRESSED:
lcd.setCursor(horizPos, 1);
horizPos = horizPos + 1;
lcd.print('*');
switch (eKey) {
case '*':
checkPassword(); //* is to validate password
horizPos = (horizPos * 0) + 5;
case '#':
horizPos = (horizPos * 0) + 5;
password.reset();
password2.reset();
lcd.clear();
break; //# is to reset password attempt
default:
password.append(eKey);
password2.append(eKey);
if (keypad.getState() == PRESSED) {
tone(Buzz, 1000, 20);
}
}
}
}
void checkPassword() {
keypad.getKey();
if (password.evaluate()) {
password.reset();
activated = true;
}
else if (password2.evaluate()) {
Serial.println("MOTION ALARM DEACTIVATED");
var = 0;
lcd.clear();
alarm = false;
password2.reset();
activated = false;
MalarmActivated = false;
digitalWrite(relay, LOW);
digitalWrite(RED, LOW);
digitalWrite(GREEN, HIGH);
tone(Buzz, 1000, 1000);
delay(100);
tone(Buzz, 500, 100);
lcd.setCursor(0, 0);
lcd.print(" MOTION ALARM");
lcd.setCursor(0, 1);
lcd.print(" DEACTIVATED");
delay(1500);
lcd.clear();
digitalWrite(GREEN, LOW);
}
else { //if password is wrong
var = 1;
if (alarm == true) {
} else {
tone(Buzz, 1600, 1500);
password.reset(); //resets password after incorrect entry
password2.reset();
digitalWrite(RED, HIGH); //RED Light will turn on
lcd.setCursor(0, 0);
lcd.print("PASSWORD INVALID");
lcd.setCursor(0, 1);
lcd.print(" TRY AGAIN !");
delay(2500);
digitalWrite(RED, LOW); //RED light will turn off
lcd.clear();
}
}
}