RFID Main Arduino Code

 #include <Servo.h>

#include <LiquidCrystal_I2C.h>

#include <SPI.h>

#include <MFRC522.h>


#define SS_PIN 10

#define RST_PIN 9

#define buzzer 2

#define servoPin 3


String UID = "93 1C CF A9";//Enter your card or keychain ID

byte lock = 0;


Servo servo;

LiquidCrystal_I2C lcd(0x27, 16, 2);

MFRC522 rfid(SS_PIN, RST_PIN);



void setup() {

  Serial.begin(9600);

  servo.write(45);

  lcd.init();

  lcd.backlight();

  servo.attach(servoPin);

  SPI.begin();

  rfid.PCD_Init();

  pinMode(buzzer, OUTPUT);

}


void loop() {

  lcd.setCursor(4, 0);

  lcd.print("Welcome!");

  lcd.setCursor(1, 1);

  lcd.print("Put your card");


  if ( ! rfid.PICC_IsNewCardPresent())

    return;

  if ( ! rfid.PICC_ReadCardSerial())

    return;


  lcd.clear();

  lcd.setCursor(0, 0);

  lcd.print("Scanning");

  Serial.print("NUID tag is :");

  String ID = "";

  for (byte i = 0; i < rfid.uid.size; i++) {

    lcd.print(".");

    ID.concat(String(rfid.uid.uidByte[i] < 0x10 ? " 0" : " "));

    ID.concat(String(rfid.uid.uidByte[i], HEX));

    delay(300);

  }

  ID.toUpperCase();


  if (ID.substring(1) == UID && lock == 0 ) {

    digitalWrite(buzzer, HIGH);

    delay(300);

    digitalWrite(buzzer, LOW);

    servo.write(120);

    delay(100);

    lcd.clear();

    lcd.setCursor(0, 0);

    lcd.print("Locked");

    delay(1500);

    lcd.clear();

    lock = 1;

  } else if (ID.substring(1) == UID && lock == 1 ) {

    digitalWrite(buzzer, HIGH);

    delay(300);

    digitalWrite(buzzer, LOW);

    servo.write(45);

    delay(100);

    lcd.clear();

    lcd.setCursor(0, 0);

    lcd.print("Unlocked");

    delay(1500);

    lcd.clear();

    lock = 0;

  } else {

    lcd.clear();

    lcd.setCursor(0, 0);

    lcd.print("Wrong card!");

    digitalWrite(buzzer, HIGH);

    delay(200);

    digitalWrite(buzzer, LOW);

    delay(200);

    digitalWrite(buzzer, HIGH);

    delay(200);

    digitalWrite(buzzer, LOW);

    delay(200);

    digitalWrite(buzzer, HIGH);

    delay(200);

    digitalWrite(buzzer, LOW);

    delay(200);

    digitalWrite(buzzer, HIGH);

    delay(200);

    digitalWrite(buzzer, LOW);

    delay(200);

    delay(1500);

    lcd.clear();

  }

Comments

Popular posts from this blog

Arduino Code Car Parking System

Arduino Code