본문 바로가기
프로그래밍/임베디드

[아두이노] Key Pad를 이용한 도어락

by JR2 2021. 1. 8.

#define keyPad_col0 8 // 3*4 KeyPad Pin3
#define keyPad_col1 6 // 3*4 KeyPad Pin1
#define keyPad_col2 10 // 3*4 KeyPad Pin5
#define keyPad_row0 7 // 3*4 KeyPad Pin2
#define keyPad_row1 5 // 3*4 KeyPad Pin7
#define keyPad_row2 11 // 3*4 KeyPad Pin6
#define keyPad_row3 9 // 3*4 KeyPad Pin4

#define relay_pin 4

int keyPad_NUM = 0; // 입력받은 숫자 저장
int pw[20] = {2,5,6,11,12 }; // 0 -> 11, # -> 12;
int try_pw[20];

int getStar(void);
void getPW(void);
int pwCheck(void);

void setup() {

  for(int i = 0; i < 20; i++) try_pw[i] = 0;
  // put your setup code here, to run once:
  pinMode(relay_pin, OUTPUT); // relay pin output 설정
  pinMode(keyPad_col0, OUTPUT); // col0:2은 출력으로 지정해줘야함.
  pinMode(keyPad_col1, OUTPUT);
  pinMode(keyPad_col2, OUTPUT);
  pinMode(keyPad_row0, INPUT_PULLUP); // row0:3은 입력으로 지정해줘야함.
  pinMode(keyPad_row1, INPUT_PULLUP); // 아두이노의 내부 풀업저항 사용.
  pinMode(keyPad_row2, INPUT_PULLUP);
  pinMode(keyPad_row3, INPUT_PULLUP);

  digitalWrite(relay_pin, HIGH); // 초기에 door lock은 붙어있어야함.

  Serial.begin(9600); // serial 통신시작

}

void loop() {
  if(getStar())
  {
    Serial.println("Entering PW Mode");
    for(int i = 0; i < 20; i++)
    {
      getPW();
      try_pw[i] = keyPad_NUM;
      Serial.println(keyPad_NUM); // 입력받은 값을 시리얼 모니터에 출력.
      if(keyPad_NUM == 12) { break; }
    }
    
    if(pwCheck()) { Serial.println("Door OPEN"); digitalWrite(relay_pin,LOW); }
    else { Serial.println("Wrong Password!"); }
    
    for(int i = 0; i < 20; i++) try_pw[i] = 0;  // clear arr try_pw
    while(!(getStar()));
    digitalWrite(relay_pin,HIGH);
    Serial.println("Door Close");
  }
}

int getStar(void)
{
  keyPad_NUM = 0; // 입력받은 값 초기화
  digitalWrite(keyPad_col0, LOW); // col0을 low로 활성화 시켜 1,4,7,* 을 받아옴
  if(!(digitalRead(keyPad_row0))){ keyPad_NUM = 1; while(!(digitalRead(keyPad_row0))); }
  // col0,row0가 동시에 활성화 되면 1이 입력된 것으로 판단함. 눌렀다가 뗄 때 while에서 빠져나와서 다시 동작
  else if(!(digitalRead(keyPad_row1))){ keyPad_NUM = 4; while(!(digitalRead(keyPad_row1))); }
  else if(!(digitalRead(keyPad_row2))){ keyPad_NUM = 7; while(!(digitalRead(keyPad_row2))); }
  else if(!(digitalRead(keyPad_row3))){ keyPad_NUM = 10; while(!(digitalRead(keyPad_row3))); }
  else {}
  digitalWrite(keyPad_col0, HIGH); // col0를 HIGH로 초기화
  delay(100);
  if(keyPad_NUM == 10) return 1;
  else return 0;
}


void getPW(void)
{
  while(1)
  {
    keyPad_NUM = 0;
  digitalWrite(keyPad_col0, HIGH); // col0:2 HIGH로 초기화
  digitalWrite(keyPad_col1, HIGH);
  digitalWrite(keyPad_col2, HIGH);

  digitalWrite(keyPad_col0, LOW); // col0을 low로 활성화 시켜 1,4,7,* 을 받아옴
  if(!(digitalRead(keyPad_row0))){ keyPad_NUM = 1; while(!(digitalRead(keyPad_row0))); }
  // col0,row0가 동시에 활성화 되면 1이 입력된 것으로 판단함. 눌렀다가 뗄 때 while에서 빠져나와서 다시 동작
  else if(!(digitalRead(keyPad_row1))){ keyPad_NUM = 4; while(!(digitalRead(keyPad_row1))); }
  else if(!(digitalRead(keyPad_row2))){ keyPad_NUM = 7; while(!(digitalRead(keyPad_row2))); }
  else if(!(digitalRead(keyPad_row3))){ keyPad_NUM = 10; while(!(digitalRead(keyPad_row3))); }
  else {}
  digitalWrite(keyPad_col0, HIGH); // col0를 HIGH로 초기화

  digitalWrite(keyPad_col1, LOW); // col0을 low로 활성화 시켜 2,5,8,0 을 받아옴
  if(!(digitalRead(keyPad_row0))){ keyPad_NUM = 2; while(!(digitalRead(keyPad_row0))); }
  // col1,row0가 동시에 활성화 되면 2가 입력된 것으로 판단함. 눌렀다가 뗄 때 while에서 빠져나와서 다시 동작
  else if(!(digitalRead(keyPad_row1))){ keyPad_NUM = 5; while(!(digitalRead(keyPad_row1))); }
  else if(!(digitalRead(keyPad_row2))){ keyPad_NUM = 8; while(!(digitalRead(keyPad_row2))); }
  else if(!(digitalRead(keyPad_row3))){ keyPad_NUM = 11; while(!(digitalRead(keyPad_row3))); }
  else {}
  digitalWrite(keyPad_col1, HIGH); // col1를 HIGH로 초기화

  digitalWrite(keyPad_col2, LOW); // col2을 low로 활성화 시켜 3,6,9,# 을 받아옴
  if(!(digitalRead(keyPad_row0))){ keyPad_NUM = 3; while(!(digitalRead(keyPad_row0))); }
  // col2,row0가 동시에 활성화 되면 3이 입력된 것으로 판단함. 눌렀다가 뗄 때 while에서 빠져나와서 다시 동작
  else if(!(digitalRead(keyPad_row1))){ keyPad_NUM = 6; while(!(digitalRead(keyPad_row1))); }
  else if(!(digitalRead(keyPad_row2))){ keyPad_NUM = 9; while(!(digitalRead(keyPad_row2))); }
  else if(!(digitalRead(keyPad_row3))){ keyPad_NUM = 12; while(!(digitalRead(keyPad_row3))); }
  else {}
  digitalWrite(keyPad_col2, HIGH); // col2를 HIGH로 초기화

  delay(100);

  if(keyPad_NUM != 0) break;
  
  }
  return;
}

int pwCheck(void)
{
  int success_FLAG = 0;
  for(int i = 0; i < 20; i++)
  {
    if(try_pw[i] == 12 && pw[i] == 12) { success_FLAG = 1; break; }
    if(try_pw[i] != pw[i]) break;
  }
  return success_FLAG;
}

댓글