所以当我试图解除系统撤防时遇到问题。当我将代码上传到arduino时,它要求我输入一个引脚然后立即激活系统。但是当我尝试停用系统时,它会占用引脚并清除屏幕并执行我设置的主屏幕功能。
代码如下:
#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]; //charater string of newPasswordString
//initialize password to 1234
//you can use password.set(newPassword) to overwrite it
Password password = Password("1234");
byte maxPasswordLength = 4;
byte currentPasswordLength = 4;
// keypad type definition
const byte ROWS = 4; //four rows
const byte COLS = 4; //three 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 3
byte colPins[COLS]= {5,4,3,2}; //Columns 0 to 3
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(password.evaluate()){
activate();
}
}
void activate() {
if (password.evaluate()){
lcd.clear();
lcd.print("Activated.");
delay(1000);
mainScreen();
} else {
lcd.clear();
lcd.print("Wrong Password!");
mainScreen();
}
}
void deactivate(){
if (password.evaluate()){
lcd.clear();
lcd.print("Deactivated.");
delay(1000);
} else {
lcd.clear();
lcd.print("Wrong Password!");
mainScreen();
}
}
void mainScreen(){
lcd.clear();
lcd.print("Enter Pin:");
keypad.getKey();
}
答案 0 :(得分:0)
如Arduino keypad 4x4 to LCD activate/deactivate中所述,您必须保持当前状态,并且您必须清除 password
和currenPasswordLength
。请参阅答案中的WORKING示例。
设置byte currenPasswordLength = 4
无济于事。如果按第一个键,它将递增到5,下一个键将其递增到6,依此类推。
256键按下后你会再次获得4分!!!!!!!而且,由于您没有清除以前检查过的密码,您将收到“密码错误”结果(最终堆栈溢出)。