【问题标题】:How to accept input from user after alarm is triggered触发警报后如何接受用户的输入
【发布时间】:2016-12-14 16:38:26
【问题描述】:

所以现在当我布防系统并将我的手移到 PIR 传感器前面时,它说系统触发了我如何让它从用户那里获取密码以停用系统。此外,当系统处于非活动状态时,它应该在屏幕上显示“未激活”

#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:");
}

【问题讨论】:

  • 到底是什么问题?你的代码怎么不工作?我们不是代码编写服务。
  • 另外,为什么这是您的第三个帐户对这个项目提出问题?

标签: arduino lcd keypad


【解决方案1】:

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

【讨论】:

  • 哦,我没看到。是的,在他的代码中。好吧,如果不是他写的,他应该尝试了解代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-10-14
  • 1970-01-01
  • 2015-04-08
  • 2012-12-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多