Hello everyone ,
I'm having a problem inside my code here.
/*
Transplant Wiring
Arduino GND -> Breadboard
Arduino +5V -> Breadboard
Arduino 8 -> Speaker
Arduino 7 -> Reed_Switch
Arduino 4 -> PIR_Sensor
Arduino A0 -> Photo_Resistor
Arduino A4 -> PCF8574 - LCD/Keypad
Arduino A5 -> PCF8574 - LCD/Keypad
*/
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <i2ckeypad.h>
#define Speaker 8
#define Switch 7
#define PIR 4
#define P_R A0
#define PCF8574_ADDR 0x20
#define ROWS 4
#define COLS 4
char* ourCode = "1234";
int currentPosition = 0;
//keypad variable
int reedPin = 0;
//Reed Switch variable
int pirPin = 0;
//PIR Sensor variable
int phPin = 0;
//Photo Resistor variable
int alarmActive = 0;
int zone;
LiquidCrystal_I2C lcd (0x38, 16, 2); // set the LCD address to 0x20 for a 16 chars and 2 line display
i2ckeypad kpd = i2ckeypad(PCF8574_ADDR, ROWS, COLS);
void setup()
{
pinMode(Speaker, OUTPUT);
//Speaker variable
pinMode(PIR, INPUT);
//PIR Sensor variable
pinMode(Switch, INPUT);
digitalWrite(Switch, HIGH);
//Reed Switch variable
pinMode(P_R, INPUT);
//Photo Resistor variable
Wire.begin();
kpd.init();
//keypad variable
lcd.init();
lcd. backlight();
displayEntryCodeScreen();
}
void loop()
{
checkPass();
if (alarmActive == 1)
{
pirPin = digitalRead(PIR);
phPin = analogRead(P_R);
reedPin = digitalRead(Switch);
if (reedPin == HIGH && alarmStatus == 0)
{
zone = 0;
alarmStatus = 1;
alarmTriggered();
}
else if (pirPin == HIGH && alarmStatus == 0)
{
zone = 1;
alarmStatus = 1;
alarmTriggered();
}
else if (phPin < 700 && alarmStatus == 0)
{
zone = 2;
alarmStatus = 1;
alarmTriggered();
}
}
}
void checkPass()
{
char key = kpd.get_key();
if (int(key) != 0)
{
lcd.setCursor(14, 3);
lcd.print(" ");
lcd.setCursor(14, 3);
if (key == ourCode[currentPosition])
{
++currentPosition;
if (currentPosition == 4 && alarmActive == 0 )
{
armAlarm();
currentPosition = 0;
}
else if (currentPosition == 4 && alarmActive == 1 )
{
disarmAlarm();
currentPosition = 0;
}
}
else if (key != ourCode[currentPosition] && alarmActive == 0)
{
invalidCode_1();
currentPosition = 0;
}
else if (key != ourCode[currentPosition] && alarmActive == 1)
{
invalidCode_2();
currentPosition = 0;
}
}
}
void armAlarm()// alarm armed and countdown start
{
clearScreen();
lcd.setCursor(0, 0);
lcd.print(" Alarm ARMED ");
noTone(Speaker);
delay(1000);
countDown_1();
}
void countDown_1()// alarm countdown
{
lcd.setCursor(0, 0);
lcd.print("Countdown Start ");
int count = 5;
while (count != 0 )
{
count--;
lcd.setCursor(0, 1);
lcd.print("------ ");
lcd.print(count);
lcd.print(" -------");
delay(1000);
}
if (count == 0)
{
clearScreen();
lcd.setCursor(0, 0);
lcd.print("<<System READY>>");
alarmActive = 1;
} // when countdown reach zero
}
void countDown_2()// entry sensor countdown
{
clearScreen();
lcd.setCursor(0, 0);
lcd.print("-Enter Password-");
int count = 10;
char key = kpd.get_key();
attachInterrupt(0x20, checkPass, HIGH);
while ( count != 0) // ten seconds to enter the correct code
{
count--;
lcd.setCursor(0, 1);
lcd.print("------ ");
lcd.print(count);
lcd.print(" -------");
delay(1000);
if (count == 0) // if countdown reach zero alarm will triggered
{
lcd.setCursor(0, 0);
lcd.print("Countdown Ended ");
lcd.setCursor(0, 1);
lcd.println("Alarm Triggered!");
tone(Speaker, 1200);
}
}
}
void disarmAlarm()// code for disarming alarm
{
lcd.setCursor(0, 0);
lcd.print(" Alarm DISARMED ");
lcd.setCursor(0, 1);
lcd.println(" Access GRANTED ");
noTone(Speaker);
delay(1000);
displayEntryCodeScreen();
}
void invalidCode_1()// when alarm is not armed even if password input is incorrect no tone will be sound
{
lcd.setCursor(0, 0);
lcd.print(" Access DENIED ");
lcd.setCursor(0, 1);
lcd.println(" Invalid code ");
}
void invalidCode_2()// when alarm is not armed even if password input is incorrect no tone will be sound
{
lcd.setCursor(0, 0);
lcd.print(" Access DENIED ");
lcd.setCursor(0, 1);
lcd.println(" Invalid code ");
tone (Speaker, 1200);
}
void clearScreen()// code for clearing the screen
{
lcd.setCursor(0, 0);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print(" ");
}
void displayEntryCodeScreen()// code for displaying the entry code screen
{
clearScreen();
lcd.setCursor(0, 0);
lcd.print("-Enter Password-");
}
void alarmTriggered() // when alarm is triggered, which zone will be printed on the LCD and tone will play
{
clearScreen();
lcd.setCursor(0, 0);
lcd.print("Alarm Triggered!");
if (zone == 0)// if zone zero is triggered
{
lcd.setCursor(0, 1);
lcd.print("Countdown Start ");
countDown_2();
}
else if (zone == 1)// if zone one is triggered
{
lcd.setCursor(0, 1);
lcd.print("Motion in Zone 1 ");
tone(Speaker, 1200);
}
else if (zone == 2)// if zone two is triggered
{
lcd.setCursor(0, 1);
lcd.print("Motion in Zone 2 ");
tone(Speaker, 1200);
}
}
void entryTriggered()
{
lcd.setCursor(0, 0);
lcd.print("Countdown Ended ");
lcd.setCursor(0, 1);
lcd.println("Alarm Triggered!");
tone(Speaker, 1200);
}
Most of the loop works fine, but I'm struggling over for the reed switch countdown loop, while it is counting down, I wish to enter my password and stop the countdown, any suggestion? One more problem is, if I triggered one of the alarm and disarm it, the second time I armed the alarm it cannot detect anything on all the sensors any more, please help!![tt]
[/tt]