Arduino keypad 4x4 to LCD activate/deactivate (home security system)

北城余情 提交于 2019-12-25 09:18:44

问题


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();


}

回答1:


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

The only occurrences of currentPasswordLenght are these:

  1. Global variable declaration and initialization: byte currentPasswordLength = 0;
  2. Incrementation in processNumberKey: currentPasswordLength++;
  3. Compare and call activate in processNumberKey: if (currentPasswordLength == maxPasswordLength) {

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.

Example of working code:

#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;
  }
}


来源:https://stackoverflow.com/questions/40966687/arduino-keypad-4x4-to-lcd-activate-deactivate-home-security-system

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!