CCCPaste

**Servo Lock** rev_47

/********* 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: **Servo Lock**
	- Source Code NOT compiled for: Arduino Nano ESP32
	- Source Code created on: 2024-10-05 07:04:45

********* 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 *****/


/********* User code review feedback **********
#### Feedback 1 ####
- add a button that enable introduction of pin into keypad.
********* User code review feedback **********/

/****** DEFINITION OF LIBRARIES *****/
#include   // https://github.com/deneyapkart/deneyap-servo-arduino-library
#include          // https://github.com/Chris--A/Keypad

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
void updateOutputs(void);
void checkPin(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 button_PIN_D2 = 2; // Button for PIN entry

/***** 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;

/***** DEFINITION OF PWM OUTPUT PINS *****/
const uint8_t servo_Servomotor_PWMSignal_PIN_D3 = 3;

/***** DEFINITION OF OUTPUT RAW VARIABLES *****/
bool keypad_Keypad3x4_C1_PIN_D8_rawData = 0;
bool keypad_Keypad3x4_C2_PIN_D9_rawData = 0;
bool keypad_Keypad3x4_C3_PIN_D10_rawData = 0;
uint8_t servo_Servomotor_PWMSignal_PIN_D3_rawData = 0;

const uint8_t KEYPAD3X4_ROWS = 4; // four rows
const uint8_t KEYPAD3X4_COLS = 3; // three columns
char Keys_Keypad3x4[KEYPAD3X4_ROWS][KEYPAD3X4_COLS] = {
    {'1', '2', '3'},
    {'4', '5', '6'},
    {'7', '8', '9'},
    {'*', '0', '#'},
};

// Define the correct PIN
const char correctPin[] = "1234"; // Example PIN
char enteredPin[5]; // Buffer to store entered PIN
int pinIndex = 0; // Index for entered PIN

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
// Servo instance
Servo myServo;

void setup(void)
{
    // put your setup code here, to run once:
    pinMode(keypad_Keypad3x4_R1_PIN_D4, INPUT_PULLUP);
    pinMode(keypad_Keypad3x4_R2_PIN_D5, INPUT_PULLUP);
    pinMode(keypad_Keypad3x4_R3_PIN_D6, INPUT_PULLUP);
    pinMode(keypad_Keypad3x4_R4_PIN_D7, INPUT_PULLUP);
    pinMode(button_PIN_D2, INPUT_PULLUP); // Initialize button pin

    pinMode(keypad_Keypad3x4_C1_PIN_D8, OUTPUT);
    pinMode(keypad_Keypad3x4_C2_PIN_D9, OUTPUT);
    pinMode(keypad_Keypad3x4_C3_PIN_D10, OUTPUT);
    pinMode(servo_Servomotor_PWMSignal_PIN_D3, OUTPUT);

    myServo.attach(servo_Servomotor_PWMSignal_PIN_D3); // Attach servo to pin
}

void loop(void)
{
    // put your main code here, to run repeatedly:
    updateOutputs(); // Refresh output data

    // Check if the button is pressed
    if (digitalRead(button_PIN_D2) == LOW) {
        checkPin(); // Check entered PIN
    }
}

void updateOutputs()
{
    digitalWrite(keypad_Keypad3x4_C1_PIN_D8, keypad_Keypad3x4_C1_PIN_D8_rawData);
    digitalWrite(keypad_Keypad3x4_C2_PIN_D9, keypad_Keypad3x4_C2_PIN_D9_rawData);
    digitalWrite(keypad_Keypad3x4_C3_PIN_D10, keypad_Keypad3x4_C3_PIN_D10_rawData);
    analogWrite(servo_Servomotor_PWMSignal_PIN_D3, servo_Servomotor_PWMSignal_PIN_D3_rawData);
}

void checkPin()
{
    // Simulate entering a PIN
    char key = getKeypadKey(); // Function to get the key from keypad
    if (key != '\0') {
        if (key == '#') {
            // Check if the entered PIN is correct
            if (strcmp(enteredPin, correctPin) == 0) {
                // Toggle servo lock/unlock
                servo_Servomotor_PWMSignal_PIN_D3_rawData = (servo_Servomotor_PWMSignal_PIN_D3_rawData == 0) ? 180 : 0;
                myServo.write(servo_Servomotor_PWMSignal_PIN_D3_rawData); // Move servo
            }
            // Reset entered PIN
            memset(enteredPin, 0, sizeof(enteredPin));
            pinIndex = 0;
        } else if (pinIndex < 4) {
            enteredPin[pinIndex++] = key; // Store entered key
        }
    }
}

char getKeypadKey() {
    // Logic to read from the keypad
    // This is a placeholder for the actual keypad reading logic
    // You would need to implement this based on your keypad library
    return '\0'; // Return the key pressed
}

/* END CODE */