Code Arduino RFID+Keypad Password มีปัญหา

พอรันที่ละโปรแกรมทำงาน พอเอามารวมกัน Keypad ไม่ทำงาน
[code]#include <EEPROM.h>     
#include <SPI.h>       
#include <MFRC522.h>  
#include <Password.h>  
#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 21);
boolean match = false;         
boolean programMode = false;  
boolean replaceMaster = false;
int successRead;    
byte storedCard[4];  
byte readCard[4];  
byte masterCard[4];  
#define SS_PIN 53
#define RST_PIN 49
MFRC522 mfrc522(SS_PIN, RST_PIN);
const char ROWS = 4;
const char COLS = 3;
char keys[ROWS][COLS] = {
  {'1', '2', '3'},
  {'4', '5', '6'},
  {'7', '8', '9'},
  {'*', '0', '#'}
};
byte rowPin[ROWS] = {9, 8, 7, 6};
byte colPin[COLS] = {5, 4, 3};
Keypad keypad = Keypad(makeKeymap(keys), rowPin, colPin, ROWS, COLS);
char initial_password[4],new_password[4];
char password[4];
int i=0;
char key_pressed=0;
void setup() {
  pinMode(redLed, OUTPUT);
  pinMode(greenLed, OUTPUT);
  pinMode(blueLed, OUTPUT);
  pinMode(relay, OUTPUT);

  digitalWrite(relay, HIGH);    
  digitalWrite(redLed, LED_OFF);  
  digitalWrite(greenLed, LED_OFF);  
  digitalWrite(blueLed, LED_OFF);

  Serial.begin(9600);  
  SPI.begin();          
  mfrc522.PCD_Init();    
  lcd.begin();

  if (EEPROM.read(1) != 143) {
    lcd.clear();
    lcd.print("No Master Card");
    lcd.setCursor(0,1);
    lcd.print("Scan Master Card");
    do {
      successRead = getID();            
      digitalWrite(blueLed, LED_ON);    
      delay(200);
      digitalWrite(blueLed, LED_OFF);
      delay(200);
    }
    while (!successRead);                 
    for ( int j = 0; j < 4; j++ ) {       
      EEPROM.write( 2 + j, readCard[j] );  
    }
    EEPROM.write(1, 143);                 
  }
  for ( int i = 0; i < 4; i++ ) {          
    masterCard = EEPROM.read(2 + i);    
  }
  cycleLeds();  
  
  lcd.clear();
  lcd.print("Password or Card");
  initialpassword();
}
void loop () {
  key_pressed = keypad.getKey();
  do {
    successRead = getID();
    if (programMode) {
      cycleLeds();            
    }
    else {
      normalModeOn();
    }
  }
   while (!successRead);
  if (programMode) {
    if ( isMaster(readCard) ) {
      Serial.println(F("Master Card Scan"));
      Serial.println(F("Exiting Program Mode"));
      Serial.println(F("-----------------------------"));
      lcd.clear();
      lcd.print("Exited from");
      lcd.setCursor(0,1);
      lcd.print("Program Mode");
      programMode = false;
      delay(2000);
        lcd.clear();
        lcd.print("Password or Card");
      return;
    }
    else {
      if ( findID(readCard) ) {
        deleteID(readCard);
        lcd.setCursor(9,1);
        lcd.print("Removed");
        delay(2000);
      lcd.print("Program Mode");
      lcd.setCursor(0,1);
      lcd.print("Add/Remove Cards");
      }
      else {
        writeID(readCard);
        lcd.setCursor(10,1);
        lcd.print("Add");
        delay(2000);
      lcd.print("Program Mode");
      lcd.setCursor(0,1);
      lcd.print("Add/Remove Cards");
      }
    }
  }
  else {
    if ( isMaster(readCard)) {
      programMode = true;
      lcd.clear();
      lcd.print("Program Mode");
      lcd.setCursor(0,1);
      lcd.print("Add/Remove Cards");
      int count = EEPROM.read(0);
    }
    else {
      if ( findID(readCard) ) {
        lcd.setCursor(3,1);
        lcd.print("XXXXX Open");
        granted(300);
        delay(10000);
        lcd.clear();
        lcd.print("Password or Card");
      }
      else {
        lcd.setCursor(10,1);
        lcd.print("Wrong");
        Serial.println(F("Your card is wrong"));
        denied();
        delay(2000);
        lcd.clear();
        lcd.print("Password or Card");
      }
    }
  }
  if(key_pressed=='#')
  change();
  if (key_pressed)
  {
    password[i++]=key_pressed;
    lcd.print(key_pressed);
  }
  if(i==4)
  {
    delay(200);
    for(int j=0;j<4;j++)
    initial_password[j]=EEPROM.read(j);
    if(!(strncmp(password, initial_password,4)))
    {
      lcd.clear();
      lcd.print("Pass Accepted");
      delay(2000);
      lcd.setCursor(0,1);
      lcd.print("Pres # to change");
      delay(2000);
      lcd.clear();
      lcd.print("Password or Card");
      lcd.setCursor(0,1);
      i=0;
    }
    else
    {
        lcd.clear();
        lcd.print("Wrong Password");
        lcd.setCursor(0,1);
        lcd.print("Pres # to Change");
        delay(2000);
        lcd.clear();
        lcd.print("Password or Card");
        lcd.setCursor(0,1);
        i=0;
    }
  }
}
void granted (int setDelay) {
}
void denied() {

}
int getID() {
  if ( ! mfrc522.PICC_IsNewCardPresent()) {
    return 0;
  }
  if ( ! mfrc522.PICC_ReadCardSerial()) {
    return 0;
  }
  lcd.clear();
  lcd.print("Card UID:");
  lcd.setCursor(0,1);
  for (int i = 0; i < 4; i++) {  
    readCard = mfrc522.uid.uidByte;
    Serial.print(readCard, HEX);
    lcd.print(readCard, HEX);
  }
  Serial.println("");
  mfrc522.PICC_HaltA();
  return 1;
}
void cycleLeds() {
}
void normalModeOn () {
}
void readID( int number ) {
  int start = (number * 4 ) + 2;
  for ( int i = 0; i < 4; i++ ) {
    storedCard = EEPROM.read(start + i);
  }
}
void writeID( byte a[] ) {
  if ( !findID( a ) ) {
    int num = EEPROM.read(0);     
    int start = ( num * 4 ) + 6;  
    num++;               
    EEPROM.write( 0, num );    
    for ( int j = 0; j < 4; j++ ) {
      EEPROM.write( start + j, a[j] );  
    }
    successWrite();
  }
  else {
    failedWrite();
  }
}
void deleteID( byte a[] ) {
  if ( !findID( a ) ) {
    failedWrite();
  }
  else {
    int num = EEPROM.read(0);  
    int slot;       
    int start;     
    int looping;    
    int j;
    int count = EEPROM.read(0);
    slot = findIDSLOT( a );  
    start = (slot * 4) + 2;
    looping = ((num - slot) * 4);
    num--;     
    EEPROM.write( 0, num );  
    for ( j = 0; j < looping; j++ ) {         
      EEPROM.write( start + j, EEPROM.read(start + 4 + j));  
    }
    for ( int k = 0; k < 4; k++ ) {         
      EEPROM.write( start + j + k, 0);
    }
    successDelete();
  }
}
boolean checkTwo ( byte a[], byte b[] ) {
  if ( a[0] != 0 )       
    match = true;     
  for ( int k = 0; k < 4; k++ ) {  
    if ( a[k] != b[k] )    
      match = false;
  }
  if ( match ) {     
    return true;     
  }
  else  {
    return false;     
  }
}
  
int findIDSLOT( byte find[] ) {
  int count = EEPROM.read(0);
  for ( int i = 1; i <= count; i++ ) {
    readID(i);               
    if ( checkTwo( find, storedCard ) ) {
      return i;         
    }
  }
}
boolean findID( byte find[] ) {
  int count = EEPROM.read(0);     
  for ( int i = 1; i <= count; i++ ) {    
    readID(i);          
    if ( checkTwo( find, storedCard ) ) {  
      return true;
      break;
    }
    else {
    }
  }
  return false;
}
void successWrite() {
}
void failedWrite() {
}
void successDelete() {
}
boolean isMaster( byte test[] ) {
  if ( checkTwo( test, masterCard ) )
    return true;
  else
    return false;
}
void change()
{
  int j=20;
  lcd.clear();
  lcd.print("Current Password");
  lcd.setCursor(0,1);
  while(j<24)
  {
    char key=keypad.getKey();
    if(key)
    {
      new_password[j++]=key;
      lcd.print("*");
    }
    key=0;
  }
  delay(500);
  if((strncmp(new_password, initial_password, 4)))
  {
    lcd.clear();
    lcd.print("Wrong Password");
    lcd.setCursor(0,1);
    lcd.print("Try Again");
    delay(1000);
  }
  else
  {
    j=20;
    lcd.clear();
    lcd.print("New Password:");
    lcd.setCursor(0,1);
    while(j<24)
    {
    char key=keypad.getKey();
    if(key)
      {
        initial_password[j]=key;
        lcd.print(key);
        EEPROM.write(j,key);
        j++;
      }
    }
    lcd.setCursor(5,1);
    lcd.print("Pass Chang");
    delay(1000);
  }
    lcd.clear();
    lcd.print("Enter Password");
    lcd.setCursor(0,1);
    key_pressed=0;
}
void initialpassword()
{
  for(int j=0;j<4;j++)
  EEPROM.write(j, j+49);
  for(int j=0;j<4;j++)
  initial_password[j]=EEPROM.read(j);
}[/code]
แก้ไขข้อความเมื่อ
แสดงความคิดเห็น
โปรดศึกษาและยอมรับนโยบายข้อมูลส่วนบุคคลก่อนเริ่มใช้งาน อ่านเพิ่มเติมได้ที่นี่