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 passwordchar newPassword[4]; //character string of newPasswordString//initialize password to 1234//you can use password.set(newPassword) to overwrite itPassword password = Password("1234");byte maxPasswordLength = 6;byte currentPasswordLength = 0;// keypad type definitionconst byte ROWS = 4; //four rowsconst byte COLS = 4; //four columnschar 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 4byte colPins[COLS]= {5,4,3,2}; //Columns 0 to 4int 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 的唯一出现是这些:全局变量声明和初始化:byte currentPasswordLength = 0;processNumberKey 中的增量:currentPasswordLength++;比较并调用processNumberKey中的activate:if(currentPasswordLength == maxPasswordLength){Global variable declaration and initialization: byte currentPasswordLength = 0;Incrementation in processNumberKey: currentPasswordLength++;Compare and call activate in processNumberKey: if (currentPasswordLength == maxPasswordLength) {第三个也解释了为什么在第二次按键后停用(第二轮)失败,因为 maxPasswordLength 是 6 并且激活后 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 rowsconst byte COLS = 4; //four columnsconst 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 激活/停用(家庭安全系统)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
06-20 15:39