Arduino 键盘 4x4 到 LCD 激活/停用(家庭安全系统) [英] Arduino keypad 4x4 to LCD activate/deactivate (home security system)

查看:26
本文介绍了Arduino 键盘 4x4 到 LCD 激活/停用(家庭安全系统)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 Arduino 激活/停用系统有问题.上传新的代码副本后,我可以获得激活或停用代码,但是一旦我在上传后激活它并尝试停用安全系统,它只需要输入 2 个数字,然后提示我输入错误密码.

I have a problem with an activation/deactivation system for Arduino. I can get the code to activate or deactivate once I upload a fresh copy of the code, but once I activate it after upload and try to deactivate the security system, it only takes in 2 numbers and then prompts me to Wrong password.

#include "Keypad.h"
#include "LiquidCrystal.h" 
#include "Password.h"
LiquidCrystal lcd(0,1,10,11,12,13);
char newPasswordString; //hold the new password
char newPassword[4]; //character string of newPasswordString

//initialize password to 1234
//you can use password.set(newPassword) to overwrite it
Password password = Password("1234");

byte maxPasswordLength = 6; 
byte currentPasswordLength = 0;
// keypad type definition
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};

byte rowPins[ROWS] = {9,8,7,6}; //Rows 0 to 4
byte colPins[COLS]= {5,4,3,2}; //Columns 0 to 4

int count=0;

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup()
{

  lcd.begin(16, 2);
  mainScreen();
}

void loop(){
   char key = keypad.getKey();
   if (key != NO_KEY){
      delay(60); 
      switch (key){
      case 'A': activate(); break; 
      case 'B': break; 
      case 'C': break; 
      case 'D': deactivate(); break; 
      case '#':  break;
      case '*': break;
      default: processNumberKey(key);
      }
   }
}

void processNumberKey(char key) {
   lcd.print(key);
   currentPasswordLength++;
   password.append(key);
   if (currentPasswordLength == maxPasswordLength) {
      activate();
   } 
}

void activate() {
   if (password.evaluate()){
      lcd.clear();
      lcd.print("Activated.");
      delay(1000);
      mainScreen();
   } else {
      lcd.clear();
      lcd.print("Wrong Password!");
   } 
}

void deactivate(){
    if (password.evaluate()){
      lcd.clear();
      lcd.print("Deactivated.");
      delay(1000);
      mainScreen();
   } else {
      lcd.clear();
      lcd.print("Wrong Password!");
      delay(1000);
      mainScreen();
   } 
}

void mainScreen(){
  lcd.clear();
  lcd.print("Enter Pin:");
  keypad.getKey();


}

推荐答案

所以第一次(激活)有效,下次(停用)无效?

So for the first time it works (activation) and next time (deactivation) it doesn't?

currentPasswordLenght 的唯一出现是这些:

  1. 全局变量声明和初始化:byte currentPasswordLength = 0;
  2. processNumberKey 中的增量:currentPasswordLength++;
  3. 比较并调用processNumberKey中的activate:if(currentPasswordLength == maxPasswordLength){
  1. Global variable declaration and initialization: byte currentPasswordLength = 0;
  2. Incrementation in processNumberKey: currentPasswordLength++;
  3. Compare and call activate in processNumberKey: if (currentPasswordLength == maxPasswordLength) {

第三个也解释了为什么在第二次按键后停用(第二轮)失败,因为 maxPasswordLength6 并且激活后 currentPasswordLengthcode> 是 4.

The third one also explains why the deactivation (second round) fails after the second key press as maxPasswordLength is 6 and after the activation the currentPasswordLength is 4.

工作代码示例:

#include <Key.h>
#include <Keypad.h>

#include <Password.h>

const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns

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

const byte rowPins[ROWS] = {9,8,7,6};
const byte colPins[COLS] = {5,4,3,2};

Keypad    keypad { makeKeymap(keys), rowPins, colPins, ROWS, COLS };
Password  passwd { "1234" };

bool   activated = false;
byte       count = 0;

uint32_t timeout = 0;

void setup() {
  Serial.begin(115200);
}

void loop() {
  char key = keypad.getKey();

  switch (key) {
    case '0' ... '9':
      Serial.print(key);
      passwd << key;
      timeout = millis() + 5000;
      if (++count == 4) {
        Serial.println();
        if (passwd.evaluate()) {
          activated = !activated;
          Serial.println(activated ? F("Activated") : F("Deactivated"));
        } else {
          Serial.println(F("Wrong password"));
        }
        passwd.reset();
        count = 0;
      }
      break;
    case 'A' ... 'D':
      break;
    default:
      delay(60);
      break;
  }

  if ((count != 0) && (millis() > timeout)) {
    Serial.println(F("\nTimeout"));
    passwd.reset();
    count = 0;
  }
}

这篇关于Arduino 键盘 4x4 到 LCD 激活/停用(家庭安全系统)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆