2016-12-05 73 views
0

我有一個Arduino激活/去激活系統的問題。一旦我上傳了一個新的代碼副本,我就可以得到激活或停用的代碼,但是一旦我上傳後激活它並嘗試停用安全系統,它只需要2個數字,然後提示我輸入錯誤的密碼。Arduino鍵盤4x4到LCD激活/停用(家庭安全系統)

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


} 
+0

你從來沒有真正清除猜到密碼。您只能清除LCD屏幕上顯示的猜測。 –

回答

0

因此,它第一次工作(激活)和下一次(停用)它不?

currentPasswordLenght唯一出現的是這些:

  1. 全局變量聲明和初始化:byte currentPasswordLength = 0;
  2. 增量方向在processNumberKeycurrentPasswordLength++;
  3. 比較,並呼籲activateprocessNumberKeyif (currentPasswordLength == maxPasswordLength) {

第三個也是平原爲何在第二次按鍵之後失效(第二輪)失敗,因爲maxPasswordLength6並且在激活之後currentPasswordLength4

的工作代碼示例:

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

它根本不激活。每次我運行它並將其放入引腳時,它只會關閉 –

+0

@ShivPatel您是否在第四個數字鍵後按下了'A'鍵?只有當密碼長度爲6個密鑰時,它纔會自動調用'activate()'。 – KIIV

+0

那麼如何改變它,所以它不檢查密鑰長度,只是檢查密碼 –