Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <LiquidCrystal_I2C.h>
- #include <SPI.h>
- #include <MFRC522.h>
- #include <Servo.h>
- #include <Keypad.h>
- #define SS_PIN 10
- #define RST_PIN 9
- constexpr uint8_t greenLed = 16;
- constexpr uint8_t redLed = 15;
- MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
- Servo Tahm;
- LiquidCrystal_I2C screen(0x3f,16,2);
- int i=0;
- char initial_password [4]={'4','4','2','1'};
- int key_pressed = 0;
- char password[4];
- const byte ROWS = 4; //four rows
- const byte COLS = 4; //four columns
- char keys[ROWS][COLS] = {
- {'1','2','3',},
- {'4','5','6',},
- {'7','8','9',},
- {'*','0','#',}
- };
- byte rowPins[ROWS] = { 8,7, 6, 5}; //connect to the row pinouts of the keypad
- byte colPins[COLS] = { 4, 3, 2,}; //connect to the column pinouts of the keypad
- Keypad keypad_key = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
- //Keypad keypad_key = Keypad( makeKeymap(keys), row_pins, column_pins, rows, columns);
- void setup()
- {
- screen.begin();
- screen.setBacklight((uint8_t)1);
- Serial.begin(9600); // Initiate a serial communication
- SPI.begin(); // Initiate SPI bus
- mfrc522.PCD_Init(); // Initiate MFRC522
- Serial.println("Searching for a card.");
- Serial.println();
- Tahm.attach (14);
- Tahm.write (0);
- msg();
- }
- void msg() {
- screen.clear();
- screen.print(" Scan");
- screen.setCursor (0,1);
- screen.print (" a card..");
- }
- void loop()
- {
- // Look for new cards
- if ( ! mfrc522.PICC_IsNewCardPresent())
- {
- return;
- }
- // Select one of the cards
- if ( ! mfrc522.PICC_ReadCardSerial())
- {
- return;
- }
- //Show UID on serial monitor
- Serial.print("UID tag :");
- String content= "";
- byte letter;
- for (byte i = 0; i < mfrc522.uid.size; i++)
- {
- Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
- Serial.print(mfrc522.uid.uidByte[i], HEX);
- content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
- content.concat(String(mfrc522.uid.uidByte[i], HEX));
- }
- Serial.println();
- Serial.print("Message : ");
- content.toUpperCase();
- if (content.substring(1) == "20 90 E6 89") //change here the UID of the card/cards that you want to give access
- {
- Serial.println("card matched");
- Serial.println();
- screen.clear();
- screen.print (" card matched");
- delay (2000);
- screen.clear ();
- screen.print ("enter passcode");
- int i=0;
- while (i<4)
- {
- key_pressed = keypad_key.getKey(); // Storing keys
- if (key_pressed)
- {
- password[i++] = key_pressed; // Storing in password variable
- screen.print("*");
- delay(5);
- }
- }
- delay(200);
- if (!(strncmp(password, initial_password, 4))) // If password is matched
- {
- screen.clear();
- screen.print("Pass Accepted");
- Tahm.write(90); // Door Opened
- digitalWrite(greenLed, HIGH);
- delay(3000);
- digitalWrite(greenLed, LOW);
- Tahm.write(0); // Door Closed
- screen.clear();
- i = 0;
- msg();
- }
- else // If password is not matched
- {
- screen.clear();
- screen.print("Wrong Password ");
- digitalWrite(redLed, HIGH);
- delay(3000);
- digitalWrite(redLed, LOW);
- screen.clear();
- i = 0;
- msg();
- }
- }
- //
- // Tahm.write (0);
- // delay (5000);
- // Tahm.write (179);
- // msg();
- else {
- Serial.println("card denied");
- screen.clear();
- screen.print (" Byeee");
- delay(5000);
- msg();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement