2016-12-05 1 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

처음으로 작동 (활성화) 및 다음에 비활성화 (비활성화)하지 않습니까? processNumberKeybyte currentPasswordLength = 0;

  • 점진 :

    1. 글로벌 변수 선언과 초기화 : currentPasswordLenght

      유일한 사건이 있습니다 currentPasswordLength++;

    2. 비교 및 ​​전화 activateprocessNumberKey에서 : if (currentPasswordLength == maxPasswordLength) {

    세 번째도 ex 두 번째 키를 누른 후 비활성화 (두 번째 라운드)가 실패한 이유는 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

    그래서 암호 키를 검사하지 않고 암호를 확인하는 방법을 변경하려면 어떻게합니까? –