Demo entry 6838796

code

   

Submitted by anonymous on Jun 11, 2019 at 19:23
Language: Arduino. Code size: 4.5 kB.

#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <Servo.h>
#include <MFRC522.h>

#define RST_PIN 5
#define SS_PIN 53

MFRC522 mfrc522(SS_PIN, RST_PIN);
byte IDbuffer[] = {0x00, 0x00, 0x00, 0x00};

const int colPins[4] = {31, 29, 27, 25};
const int rowPins[4] = {33, 35, 37, 39};
const int keymap[4][4] = {
  {1, 2, 3, -1},
  {4, 5, 6, -1},
  {7, 8, 9, -1},
  {-1, 0, -1, -1}
};
const int SERVO = 9;
LiquidCrystal_I2C lcd(0x3f, 16, 2);
Servo myservo;

int password = 1534;
long lastDebounce = 0;
int p = 0;
int pressedNumbers = 0;
int numbers[4] = {0, 0, 0, 0};

int ErrorCount = 0;

#define Red 17
#define Green 18
#define Blue 19

#define Speaker 16 

void setup() {
  Serial.begin(9600); // 시리얼모니터 설정
  pinMode(Red, OUTPUT);
  pinMode(Green, OUTPUT);
  pinMode(Blue, OUTPUT); // LED 설정
  pinMode(Speaker, OUTPUT); // 피에조 부저 설정
  for(int index =0; index<4; index++) {
    pinMode(rowPins[index], INPUT_PULLUP);
    pinMode(colPins[index], OUTPUT);
    digitalWrite(colPins[index], HIGH);
  } // 키매트릭스
  lcd.init();
  lcd.backlight(); // 디스플레이 설정
  myservo.attach(SERVO); // 서보모터 설정
  SPI.begin();
  mfrc522.PCD_Init();
  while ( ! mfrc522.PICC_IsNewCardPresent());
  while ( ! mfrc522.PICC_ReadCardSerial());
  storeID(mfrc522.uid.uidByte, mfrc522.uid.size);
  mfrc522.PICC_HaltA(); // RFID 설정
}

void loop() {
  lcd.setCursor(0,0);
  lcd.print("The Authorized Only");
  myservo.write(180);
  setColor(255, 255, 0);
  char key = -1;
  for (int column = 0; column<4; column++) {
    digitalWrite(colPins[column], LOW);
    for (int row = 0; row<4; row++) {
      if (digitalRead(rowPins[row]) == LOW) {
        if (millis() - lastDebounce > 200) {
          lastDebounce = millis();
          key = keymap[row][column];
        }
      }
    }
    digitalWrite(colPins[column], HIGH);
  }
  if (key != -1) {
    lcd.setCursor(0 + p, 1);
    lcd.print("#");
    delay(300);
    numbers[p] = key;
    pressedNumbers = 1000*numbers[0] + 100*numbers[1] + 10*numbers[2] + numbers[3];
    p++;
    if (p == 3) {
      if (pressedNumbers == password) {
        lcd.clear();
        lcd.setCursor(3, 0);
        lcd.print("Authorized");
        setColor(0, 255, 0);
        myservo.write(0);
        ErrorCount == 0;
        delay(10000);
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print("The Authorized Only");
        myservo.write(0);
      }
      else {
        lcd.clear();
        lcd.setCursor(3, 0);
        lcd.print("Access Denied");
        setColor(255, 0, 0);
        Serial.println("Alert!! Unathorized Access Detected");
        ErrorCount++;
        delay(5000);
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print("The Authorized Only");
        myservo.write(0);
      }
      p = 0;
    }
  } // 키매트릭스 관련 스케치
  
  if ( ! mfrc522.PICC_IsNewCardPresent()) {
    return;
  }
  if ( ! mfrc522.PICC_ReadCardSerial()) {
    return;
  }
  compareID(mfrc522.uid.uidByte, mfrc522.uid.size);
  mfrc522.PICC_HaltA(); // RFID 관련 스케치
  if (ErrorCount == 3) {
    Alert(true);
  }
}

void compareID(byte*buffer, byte bufferSize) {
  for (byte k = 0; k < bufferSize; k++) {
    if (buffer[k] == IDbuffer[k]) {
      continue;
    }
    else {
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("Password Changed");
      password == 1534;
      Serial.println("Password Change Detected");
      setColor(80, 0, 80);
      delay(500);
      setColor(80, 0, 80);
      delay(500);
      setColor(80, 0, 80);
      delay(500);
      setColor(255, 255, 0);
      delay(3000);
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("The Authorized Only");
    }
  }
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Password Changed");
  password == 1544;
  setColor(80, 0, 80);
  delay(500);
  setColor(80, 0, 80);
  delay(500);
  setColor(80, 0, 80);
  delay(500);
  setColor(255, 255, 0);
  delay(3000);
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("The Authorized Only");
}

void storeID(byte*buffer, byte bufferSize) {
  for (int h = 0; h < bufferSize; h++) {
    IDbuffer[h] = buffer[h];
  }
}

void Alert(int Error) {
  if (Error) {
    setColor(255, 0, 0);
    tone(Speaker, 2093, 100);
  }
}

void setColor(int R, int G, int B){
  digitalWrite(Red, R);
  digitalWrite(Green, G);
  digitalWrite(Blue, B);
}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).