My project is simulating a security system that has a PIR Movement sensor, and once it senses movement a buzzer goes off, a LCD shows the alarm is going off, and 2 LED lights blink back and forth. I have a keypad to read in a password however it seems that the user would need to press the button at the exact time as the code in the loop. Does anybody have any solutions?
Here's the code:
#include <Keypad.h>
#include <LiquidCrystal.h> // For LCD
int LED_1=23; //LED 1
int LED_2=25; //LED 2
int readyLED=24;
int motionS=52; //PIR Motion sensor
int detection;
//PIR output storage
int PIR_store=0;
int screenOffMsg=0;
String password="8900";
String tempPassword;
boolean enteredPassword;
long hold; //duration
boolean activateAlarm=false;
boolean alarmActivated=false;
boolean activated=false;
const byte Rows=4;
const byte Col=4;
char keypressed; //Keypad inputs
char keyMap[Rows][Col]= {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
const byte Rowpins[Rows]= {14,15,16,17};
const byte Colpins[Col]= {18,19,20,21};
#define buzzer 38
Keypad myKeypad= Keypad(makeKeymap(keyMap) , Rowpins, Colpins, Rows, Col);
LiquidCrystal lcd(2,3,4,5,6,7);
void setup()
{
pinMode(LED_1, OUTPUT);
pinMode(LED_2, OUTPUT);
pinMode(motionS,OUTPUT);
pinMode(readyLED,OUTPUT);
pinMode(motionS,INPUT);
pinMode(buzzer,OUTPUT);
lcd.begin(16,2);
Serial.begin(9600);
}
void Blink() //Flashing LED Lights when Motion is detected
{
if(activated)
{
digitalWrite(LED_1,HIGH);
delay(400);
digitalWrite(LED_1,LOW);
digitalWrite(LED_2,HIGH);
delay(400);
digitalWrite(LED_2,LOW);
}
}
void passCode() //Passcode function
{
int k=6;
tempPassword = "";
//activated = true;
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" *** ALARM *** ");
lcd.setCursor(0,1);
lcd.print("Pass>>");
while(activated) {
Blink();
keypressed = myKeypad.getKey();
tone(buzzer,200);
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(k,1);
lcd.print("*");
k++;
delay(500);
}
}
if (k > 10 || keypressed == '#') {
tempPassword = "";
k=6;
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" *** ALARM *** ");
lcd.setCursor(0,1);
lcd.print("Pass>>");
}
if ( keypressed == '*') {
if ( tempPassword == password ) {
activated = false;
alarmActivated = false;
noTone(buzzer);
screenOffMsg = 0;
lcd.clear();
lcd.setCursor(3,0);
lcd.print("DISARMED");
delay(2000);
PIR_store=0;
}
else if (tempPassword != password) {
lcd.setCursor(0,1);
lcd.print("Incorrect Pass");
delay(2000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" *** ALARM *** ");
lcd.setCursor(0,1);
lcd.print("Pass>>");
}
}
}
}
void loop()
{
if(alarmActivated!=1) //LCD options screen
{
lcd.setCursor(1,0);
lcd.print("Press A to");
lcd.setCursor(5,2);
lcd.print("Activate");
keypressed=myKeypad.getKey();
if(keypressed=='A') //Activating Alarm
{
activateAlarm=true;
}
}
if (activateAlarm==true)
{
lcd.clear();
lcd.setCursor(1,0);
lcd.print("Alarm will be");
lcd.setCursor(1,1);
lcd.print("activated in");
int countdown=30; //30 second countdown
while(countdown!=0) //Alarm activation countdown
{
lcd.setCursor(14,1);
lcd.print(countdown);
--countdown;
if(countdown<9)
{
countdown++;
lcd.setCursor(14,1);
lcd.print(" ");
lcd.setCursor(15,1);
lcd.print(countdown);
countdown--;
}
tone(buzzer,600,100);
digitalWrite(readyLED,HIGH);
delay(300);
digitalWrite(readyLED,LOW);
delay(1000);
}
lcd.clear();
lcd.setCursor(3,0);
lcd.print("ACTIVATED !");
delay(2000);
lcd.clear();
activateAlarm=false;
alarmActivated=true;
}
if(alarmActivated==true)
{
PIR_store= digitalRead(motionS);
lcd.setCursor(2,0);
lcd.print("*HOUSE ARMED*"); //Activation is fully set
if(PIR_store==1) //Motion detector senses a motion and sets off alarm
{
while(PIR_store==1)
{
activated=true;
Blink();
passCode();
}
}
}
}