Hello,
When in the "ALARM_ON" state, for some reason. The text from the "SAFE" state is printed straight afterwards. I have been unable to find a solution, any help would be greatly appreciated.
Many Thanks.
#include <Keypad.h>
#include <Password.h>
#include <LiquidCrystal.h>
enum {
ARMED,
SAFE,
ALARM_ON,
}
state;
//the time we give the sensor to calibrate (10-60 secs according to the datasheet)
int calibrationTime = 10;
//the time when the sensor outputs a low impulse
long unsigned int lowIn;
//the amount of milliseconds the sensor has to be low
//before we assume all motion has stopped
long unsigned int pause = 5000;
int alarmTime = 10;
byte ledPin = 12;
int spkrPin = 11;
int reedPin = 13;
int pirPin = 10;
//Create an instance of lcd class
// (RS,Enable,D4,D5,D6,D7)
LiquidCrystal lcd(14, 15, 16, 17, 18, 19);
Password password = Password("1111");
const byte ROWS = 4; //four rows
const byte COLS = 3;//four columns
char keyMap[ROWS][COLS] = {
{
'1','2','3' }
,
{
'4','5','6' }
,
{
'7','8','9' }
,
{
'*','0','#' }
};
// ROW 1,2,3,4
byte rowPins[ROWS] = {
2,3,4,5}; //connect to the row pinouts of the keypad
// COL 1,2,3
byte colPins[COLS] = {
6,7,8}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keyMap), rowPins, colPins, ROWS, COLS);
boolean switchState;
boolean lockLow = true;
boolean takeLowTime;
void setup()
{
state=SAFE;
Serial.begin(9600);
lcd.begin(16,2);
pinMode(ledPin, OUTPUT);// set up serial port
pinMode(spkrPin,OUTPUT);
pinMode(reedPin,INPUT);
pinMode(pirPin,INPUT);
digitalWrite(ledPin, LOW);
digitalWrite(pirPin,HIGH);
Serial.print("calibrating sensor ");
lcd.print("Calibrating");
lcd.setCursor (0,1);
for(int i = 0; i < calibrationTime; i++){
Serial.print(".");
lcd.print(".");
delay(1000);
}
Serial.println(" done");
Serial.println("SENSOR ACTIVE");
lcd.clear();
delay(50);
}
void loop()
{
char keyPressed = keypad.getKey();
if (keyPressed != NO_KEY){ // if key has been pressed
digitalWrite(ledPin,HIGH);
digitalWrite(spkrPin,HIGH);
delay (50);
digitalWrite(ledPin,LOW);
digitalWrite(spkrPin,LOW);
delay (50);
Serial.print (keyPressed);
}
switch (state) {
case ARMED:
{
if(digitalRead(pirPin) == HIGH){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Alarm Activated");
state = ALARM_ON;
digitalWrite(ledPin, HIGH);
digitalWrite(spkrPin,HIGH); //the led visualizes the sensors output pin state
if(lockLow){
//makes sure we wait for a transition to LOW before any further output is made:
lockLow = false;
Serial.println("---");
Serial.print("motion detected at ");
Serial.print(millis()/1000);
Serial.println(" sec");
delay(50);
}
takeLowTime = true;
}
if(digitalRead(pirPin) == LOW){
digitalWrite(ledPin, LOW); //the led visualizes the sensors output pin state
digitalWrite(spkrPin,LOW);
if(takeLowTime){
lowIn = millis(); //save the time of the transition from high to LOW
takeLowTime = false; //make sure this is only done at the start of a LOW phase
}
//if the sensor is low for more than the given pause,
//we assume that no more motion is going to happen
if(!lockLow && millis() - lowIn > pause){
//makes sure this block of code is only executed again after
//a new motion sequence has been detected
lockLow = true;
Serial.print("motion ended at "); //output
Serial.print((millis() - pause)/1000);
Serial.println(" sec");
delay(50);
}
switchState = digitalRead(reedPin);
if (switchState) {
digitalWrite(ledPin,HIGH);
digitalWrite(spkrPin,HIGH);
}
else {
digitalWrite(ledPin,LOW);
digitalWrite(spkrPin,LOW);
}
break;
}
case SAFE:
{
Serial.println("safe");
digitalWrite(ledPin,LOW);
digitalWrite(spkrPin,LOW);
lcd.setCursor(0,0);
lcd.print("* To Turn On");
lcd.setCursor(0,1);
lcd.print("Alarm");
if (keyPressed == '*') {
lcd.clear();
lcd.setCursor(0,0);
for(int i = 0; i < calibrationTime; i++){
Serial.print(".");
lcd.print(".");
delay(1000);
}
Serial.print("Alarm Armed");
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Alarm Armed");
state = ARMED;
}
break;
}
}
case ALARM_ON:
{
Serial.println("alarm is on");
if (keyPressed == '*') {
if(password.evaluate()) {
state = SAFE;
digitalWrite(ledPin,HIGH);
lcd.setCursor (0,0);
lcd.print("Enter Password");
lcd.setCursor (0,1);
lcd.print("Correct");
delay(2000);
digitalWrite(ledPin,LOW);
digitalWrite(spkrPin,LOW);
password.reset();
}
else {
// password wrong
digitalWrite(ledPin,LOW);
digitalWrite(spkrPin,HIGH);
lcd.setCursor (0,0);
lcd.print("Enter Password");
lcd.setCursor (0,1);
lcd.print("Incorrect, Try again!");
}
}
else {
password.append (keyPressed);
}
break;
}
}
}