CCCPaste

**Door Lock** rev_78

/********* Pleasedontcode.com **********

	Pleasedontcode thanks you for automatic code generation! Enjoy your code!

	- Terms and Conditions:
	You have a non-exclusive, revocable, worldwide, royalty-free license
	for personal and commercial use. Attribution is optional; modifications
	are allowed, but you're responsible for code maintenance. We're not
	liable for any loss or damage. For full terms,
	please visit pleasedontcode.com/termsandconditions.

	- Project: **Door Lock**
	- Source Code NOT compiled for: Arduino Nano ESP32
	- Source Code created on: 2024-10-06 00:20:04

********* Pleasedontcode.com **********/

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* The servo motor installed in the door will lock or */
	/* unlock based on the correct PIN code received by */
	/* the button. If the door is unlocked and the PIN is */
	/* correct, it will lock. If the door is locked and */
	/* the PIN is correct, it will unlock. */
/****** SYSTEM REQUIREMENT 2 *****/
	/* The webserver provides a web page about lock or */
	/* unlock state of the door. */
/****** END SYSTEM REQUIREMENTS *****/

/****** DEFINITION OF LIBRARIES *****/
#include   // https://github.com/deneyapkart/deneyap-servo-arduino-library
#include          // https://github.com/Chris--A/Keypad
#include      // https://github.com/evert-arias/EasyButton
#include       // https://github.com/dniklaus/arduino-display-lcdkeypad
#include "DoorLockSystem.h"  // Include the header for the door lock system

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);

/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t keypad_Keypad3x4_R1_PIN_D4 = 4;
const uint8_t keypad_Keypad3x4_R2_PIN_D5 = 5;
const uint8_t keypad_Keypad3x4_R3_PIN_D6 = 6;
const uint8_t keypad_Keypad3x4_R4_PIN_D7 = 7;
const uint8_t pinButton_PushButton_PIN_D2 = 2;

/***** DEFINITION OF DIGITAL OUTPUT PINS *****/
const uint8_t keypad_Keypad3x4_C1_PIN_D8 = 8;
const uint8_t keypad_Keypad3x4_C2_PIN_D9 = 9;
const uint8_t keypad_Keypad3x4_C3_PIN_D10 = 10;

// Define row and column pins for the keypad
byte rowPins[KEYPAD3X4_ROWS] = { keypad_Keypad3x4_R1_PIN_D4, keypad_Keypad3x4_R2_PIN_D5, keypad_Keypad3x4_R3_PIN_D6, keypad_Keypad3x4_R4_PIN_D7 };
byte colPins[KEYPAD3X4_COLS] = { keypad_Keypad3x4_C1_PIN_D8, keypad_Keypad3x4_C2_PIN_D9, keypad_Keypad3x4_C3_PIN_D10 };

void setup(void) {
    // Attach the servo to the specified pin
    doorServo.attach(3);  // Assuming the servo is connected to pin 3

    // Initialize the keypad
    keypad.begin();

    // Initialize the button
    button.begin();

    // Initialize the LCD keypad
    lcdKeypad.begin(16, 2);
    lcdKeypad.clear();
    lcdKeypad.print("Enter PIN:");
}

void loop(void) {
    // Check if the button is pressed
    button.update();
    if (button.read()) {
        // Read the entered PIN from the keypad
        String enteredPIN = "";
        char key;
        while (true) {
            key = keypad.getKey();
            if (key) {
                enteredPIN += key;
                lcdKeypad.print(key);
            }
            // Check if the Enter key is pressed
            if (key == '#') {
                break;
            }
        }
        // Check the entered PIN
        if (checkPIN(enteredPIN)) {
            toggleLock();
            lcdKeypad.clear();
            lcdKeypad.print(isLocked ? "Locked" : "Unlocked");
        } else {
            lcdKeypad.clear();
            lcdKeypad.print("Wrong PIN");
        }
    }
    // Add a small delay to avoid bouncing
    delay(100);
}