Arduino 键盘 4x4 到 LCD 激活/停用(家庭安全系统)
Posted
技术标签:
【中文标题】Arduino 键盘 4x4 到 LCD 激活/停用(家庭安全系统)【英文标题】:Arduino keypad 4x4 to LCD activate/deactivate (home security system) 【发布时间】:2016-12-05 03:31:47 【问题描述】:我对 Arduino 的激活/停用系统有疑问。一旦我上传了新的代码副本,我可以让代码激活或停用,但是一旦我在上传后激活它并尝试停用安全系统,它只输入 2 个数字,然后提示我输入错误密码。
#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();
【问题讨论】:
您永远不会真正清除猜到的密码。您只需清除 LCD 屏幕上显示的猜测。 【参考方案1】:所以第一次有效(激活),下一次(停用)无效?
currentPasswordLenght
的唯一出现是这些:
-
全局变量声明和初始化:
byte currentPasswordLength = 0;
processNumberKey
中的增量:currentPasswordLength++;
比较并在processNumberKey
中调用activate
:if (currentPasswordLength == maxPasswordLength)
第三个还解释了为什么第二次按键后停用(第二轮)失败,因为maxPasswordLength
是6
,而激活后currentPasswordLength
是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;
【讨论】:
它根本没有激活。每次我运行它并输入引脚时,它只会停用 @ShivPatel 您是否在第四个数字键之后按了“A”键?只有当密码长度为 6 键时,它才会自动调用activate()
。
那么我该如何更改它以便它不检查密钥长度而只检查密码
我只需要一个简单的布防/撤防控制,我无法让它工作
你必须知道它是否被激活(当前状态),你必须设置byte maxPasswordLength = 4;
并且你必须在评估后清除猜测的密码和currentPasswordLength
。以上是关于Arduino 键盘 4x4 到 LCD 激活/停用(家庭安全系统)的主要内容,如果未能解决你的问题,请参考以下文章
Proteus仿真51单片机4x4矩阵键盘扫描lcd1602显示
Proteus仿真Arduino UNO+数码管显示4x4键盘矩阵按键
Proteus仿真Arduino UNO +74C922键盘解码驱动4X4矩阵键盘