触发警报后如何接受用户输入

时间:2016-12-14 16:38:26

标签: arduino lcd keypad

现在,当我操纵系统并将手移到PIR传感器前面时,系统会触发如何从用户那里获取密码以停用系统。此外,当系统处于非活动状态时,它应在屏幕上显示“Not Active”

#include <LiquidCrystal.h>
#include <Password.h>
#include <Keypad.h>
//Password
Password password = Password("1234");
LiquidCrystal lcd(0, 1, 10, 11, 12, 13);

const byte ROWS = 4;
const byte COLS = 4;

char keys[ROWS][COLS] =  { // Define the Keymap
  {
    '1','2','3','A'      }
  ,
  {
    '4','5','6','B'      }
  ,
  {
    '7','8','9','C'      }
  ,
  {
    '*','0','#','D'      }
};

byte rowPins[ROWS] = {9,8,7,6};
byte colPins[COLS] = {5,4,3,2};
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS);
int armed = 0;
const int pir1 = A4;
int sensorHit = 0;
int alarmStatus = 0;
int alarmActive = 0;
int zone = 0;

void setup() {
  // put your setup code here, to run once:
  lcd.begin(16,2);
  pinMode(pir1, INPUT);
  mainScreen();
  keypad.addEventListener(keypadEvent);
}

void loop() {
  // put your main code here, to run repeatedly:
  keypad.getKey();
  if(alarmActive == 1){
    if(digitalRead(pir1) == HIGH){
      zone = 0;
      alarmTriggered();
    }
  }
}
void keypadEvent(KeypadEvent eKey){
  switch (keypad.getState()){
    case PRESSED:
    lcd.print(eKey);
  switch (eKey){
    case '#': checkPassword(); break;
    default:
    password.append(eKey);
  }
  }
}
void alarmTriggered(){
  password.reset();
  alarmStatus = 1;
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("SYSTEM TRIGGERED");
  lcd.print(0,1);
  if(zone == 0){
    lcd.print("  FRONT DOOR OPEN  ");
  }
}
void checkPassword(){
  if (password.evaluate()){       //if code is correct:
    lcd.clear();                  //clear LCD
    lcd.print("VALID PASSWORD");  //print message
    password.reset();             //resets password after correct entry
    delay(1500);                  //wait...
    lcd.clear();                  //clear
    if (alarmStatus==0 && alarmActive == 0){                //if system is off (ie: disarmed)
      lcd.print("ARMED!");         //display message
      alarmActive=1;                      //system armed
      alarmStatus=1;
      delay(2000);                  //wait
      lcd.clear();                  //clear
      lcd.setCursor(0, 0);          //return to top left of LCD
      lcd.print("Code to disarm:"); //back to where we began
    }
    else{
      lcd.print("DISARMED!");         //display message
      alarmActive=0;                      //system unarmed
      alarmStatus=0;
      delay(2000);                  //wait
      lcd.clear();                  //clear
      lcd.setCursor(0, 0);          //return to top left of LCD
      lcd.print("Code to arm:");     //back to where we began
    }
  }
  else{                            //if password is incorrect:
    lcd.clear();
    lcd.print("INVALID PASSWORD");
    password.reset();             //resets password after INCORRECT entry
    delay(2000);
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Retry Code:");
  }
}
void mainScreen(){
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Enter Pin:");
}

1 个答案:

答案 0 :(得分:0)

您需要输入设备才能输入密码。一个简单的例子可以是10个开关,其中每个开关代表1,因此我们的密码必须在0到10之间。您可以将开关值一起添加并与您设置的密码进行比较。剩下的应该很容易。