Hello,
I've build a security system but i have a problem, when the system is triggered i can't turn it off and when i activate the system immediately sees the pir sensor movement. This is the code i used:
#include <FastIO.h>
#include <I2CIO.h>
#include <LCD.h>
#include <LiquidCrystal.h>
#include <LiquidCrystal_I2C.h>
#include <LiquidCrystal_SR.h>
#include <LiquidCrystal_SR2W.h>
#include <LiquidCrystal_SR3W.h>
#include <RTClib.h>
#include <Servo.h>
#include <Keypad.h>
#include <Password.h>
#include <Wire.h>
//Servo
Servo myservo;
int pos = 90; // variable to store the servo position
int passwd_pos = 15; // the postition of the password input
//Password
Password password = Password( "7196" );
const byte ROWS = 4; // Four rows
const byte COLS = 4; // Four columns
char keys[ROWS][COLS] = { // Define the Keymap
{
'1','2','3','A' }
,
{
'4','5','6','B' }
,
{
'7','8','9','C' }
,
{
'*','0','#','D' }
};
byte rowPins[ROWS] = {
8, 7, 6, 9 }; //connect to the row pinouts of the keypad
byte colPins[COLS] = {
5, 4, 3, 2}; //connect to the column pinouts of the keypad
// Create the Keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
// initialize the library with the numbers of the interface pins
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Assignign arduino pins to LCD display module
int greenLED = 12;
int redLED = 11;
int pirPin1 = 10;
int pirPin2 = 10;
int alarmled = 13;
int alarmStatus = 0;
int zone = 0;
int alarmActive = 0;
void setup(){
Serial.begin(9600);
lcd.begin(16, 2);
//Adding time
Wire.begin();
myservo.attach(2); // attaches the servo on pin 2 to the servo object
displayCodeEntryScreen();
pinMode(redLED, OUTPUT);
pinMode(greenLED, OUTPUT);
pinMode(pirPin1, INPUT);
pinMode(pirPin2, INPUT);
digitalWrite(redLED, LOW);
digitalWrite(greenLED, HIGH);
keypad.addEventListener(keypadEvent); //add an event listener for this keypad
myservo.write(pos);
}
void loop(){
keypad.getKey();
if (alarmActive == 1){
if (digitalRead (pirPin1) == HIGH)
{
zone = 0;
alarmTriggered();
}
if (digitalRead(pirPin2) == HIGH)
{
zone = 3;
alarmTriggered();
}
}
}
void keypadEvent(KeypadEvent eKey){
switch (keypad.getState()){
case PRESSED:
if (passwd_pos - 15 >= 5) {
return ;
}
lcd.setCursor((passwd_pos++),0);
switch (eKey){
case '#':
passwd_pos = 15;
checkPassword();
break;
case '*':
password.reset();
passwd_pos = 15;
break;
default:
password.append(eKey);
lcd.print("*");
}
}
}
void alarmTriggered(){
int expected_pos;
int incr;
digitalWrite(alarmled, HIGH);
//
password.reset();
alarmStatus = 1;
// alarmActive = 0;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("BEWEGING! PIN:");
checkPassword;
displayCodeEntryScreen();
lcd.setCursor(0,1);
if (zone == 3)
{
lcd.print("SMS VERSTUURD!");
expected_pos = 65;
delay(1000);
}
if(zone == 0){
expected_pos = 65;
lcd.print("SMS VERSTUURD! ");
delay(1000);
}
if (expected_pos > pos) {
incr = 1;
} else {
incr = -1;
}
for (pos = pos; pos != expected_pos; pos += incr) {
myservo.write(pos);
delay(5);
}
/*
for(pos = 0; pos < angle; pos += 1)
{
myservo.write(pos);
delay(20);
}
for(pos = angle; pos>=1; pos-=1)
{
myservo.write(pos);
delay(20);
}
*/
}
void checkPassword(){ // To check if PIN is corrected, if not, retry!
if (password.evaluate())
{
if(alarmActive == 0 && alarmStatus == 0)
{
activate();
}
else if( alarmActive == 1 || alarmStatus == 1) {
deactivate();
}
}
else {
invalidCode();
}
}
void invalidCode() {
password.reset();
lcd.clear();
lcd.setCursor(0,0);
lcd.print("VERKEERDE CODE!");
lcd.setCursor(0,1);
lcd.print("OPNIEUW!");
digitalWrite(greenLED, LOW);
digitalWrite(redLED, HIGH);
delay(10000);
digitalWrite(redLED, LOW);
delay(1000);
displayCodeEntryScreen();
}
void activate() {
{
digitalWrite(2, HIGH);
lcd.setCursor(0,0);
lcd.print("SYSTEEM ACTIEF!");
lcd.setCursor(0,1);
lcd.print("VERLAAT KAMER!");
alarmActive = 1;
password.reset();
delay(22000);
digitalWrite(redLED, HIGH);
digitalWrite(greenLED, LOW);
}
}
void deactivate()
{
digitalWrite(redLED, LOW);
digitalWrite(greenLED, HIGH);
digitalWrite(alarmled, LOW);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("GEDEACTIVEERD");
alarmActive = 0;
password.reset();
delay(5000);
displayCodeEntryScreen();
}
void displayCodeEntryScreen()
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("PINCODE:");
lcd.setCursor(0,1);
lcd.print("BEVEILIGING!");
}