Advertisement
pleasedontcode

Keypad Lock rev_123

Oct 7th, 2024
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: Keypad Lock
  13.     - Source Code compiled for: Arduino Nano ESP32
  14.     - Source Code created on: 2024-10-07 22:44:47
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* The servo motor installed in the door will lock or */
  21.     /* unlock based on the correct PIN code received by */
  22.     /* the button. If the door is unlocked and the PIN is */
  23.     /* correct, it will lock. If the door is locked and */
  24.     /* the PIN is correct, it will unlock. */
  25. /****** SYSTEM REQUIREMENT 2 *****/
  26.     /* The webserver provides a web page about lock or */
  27.     /* unlock state of the door. */
  28. /****** END SYSTEM REQUIREMENTS *****/
  29.  
  30.  
  31. /********* User code review feedback **********
  32. #### Feedback 1 ####
  33. - create a const where to allocate PIN and check this PIN on check
  34. Keypad.
  35. ********* User code review feedback **********/
  36. /* START CODE */
  37.  
  38. /****** DEFINITION OF LIBRARIES *****/
  39. #include <Deneyap_Servo.h>  //https://github.com/deneyapkart/deneyap-servo-arduino-library
  40. #include <Keypad.h>         //https://github.com/Chris--A/Keypad
  41. #include "ServoControl.h"   // Include the ServoControl header
  42. #include "KeypadHandler.h"   // Include the KeypadHandler header
  43.  
  44. /****** FUNCTION PROTOTYPES *****/
  45. void setup(void);
  46. void loop(void);
  47. void updateOutputs(void);
  48.  
  49. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  50. const uint8_t keypad_Keypad3x4_R1_PIN_D4 = 4;
  51. const uint8_t keypad_Keypad3x4_R2_PIN_D5 = 5;
  52. const uint8_t keypad_Keypad3x4_R3_PIN_D6 = 6;
  53. const uint8_t keypad_Keypad3x4_R4_PIN_D7 = 7;
  54. const uint8_t pinButton_PushButton_PIN_D2 = 2;
  55.  
  56. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  57. const uint8_t keypad_Keypad3x4_C1_PIN_D8 = 8;
  58. const uint8_t keypad_Keypad3x4_C2_PIN_D9 = 9;
  59. const uint8_t keypad_Keypad3x4_C3_PIN_D10 = 10; // Initialized the constant
  60.  
  61. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  62. bool keypad_Keypad3x4_C1_PIN_D8_rawData = 0;
  63. bool keypad_Keypad3x4_C2_PIN_D9_rawData = 0;
  64. bool keypad_Keypad3x4_C3_PIN_D10_rawData = 0;
  65.  
  66. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  67. float keypad_Keypad3x4_C1_PIN_D8_phyData = 0.0;
  68. float keypad_Keypad3x4_C2_PIN_D9_phyData = 0.0;
  69. float keypad_Keypad3x4_C3_PIN_D10_phyData = 0.0;
  70.  
  71. const uint8_t KEYPAD3X4_ROWS = 4; // four rows
  72. const uint8_t KEYPAD3X4_COLS = 3; // three columns
  73. char Keys_Keypad3x4[KEYPAD3X4_ROWS][KEYPAD3X4_COLS] = {
  74.     {'1', '2', '3'},
  75.     {'4', '5', '6'},
  76.     {'7', '8', '9'},
  77.     {'*', '0', '#'},
  78. };
  79.  
  80. /***** DEFINITION OF PIN CODE *****/
  81. const String CORRECT_PIN = "1234"; // Define the correct PIN code
  82.  
  83. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  84. // Create instances for servo control and keypad handler
  85. ServoControl doorServo(9); // Attach the servo to pin 9
  86. byte rowPins[KEYPAD3X4_ROWS] = {keypad_Keypad3x4_R1_PIN_D4, keypad_Keypad3x4_R2_PIN_D5, keypad_Keypad3x4_R3_PIN_D6, keypad_Keypad3x4_R4_PIN_D7};
  87. byte colPins[KEYPAD3X4_COLS] = {keypad_Keypad3x4_C1_PIN_D8, keypad_Keypad3x4_C2_PIN_D9, keypad_Keypad3x4_C3_PIN_D10};
  88. KeypadHandler keypad(Keys_Keypad3x4, rowPins, colPins); // Updated to use static arrays
  89.  
  90. void setup(void)
  91. {
  92.     // put your setup code here, to run once:
  93.     pinMode(keypad_Keypad3x4_R1_PIN_D4, INPUT_PULLUP);
  94.     pinMode(keypad_Keypad3x4_R2_PIN_D5, INPUT_PULLUP);
  95.     pinMode(keypad_Keypad3x4_R3_PIN_D6, INPUT_PULLUP);
  96.     pinMode(keypad_Keypad3x4_R4_PIN_D7, INPUT_PULLUP);
  97.     pinMode(pinButton_PushButton_PIN_D2, INPUT_PULLUP);
  98.  
  99.     pinMode(keypad_Keypad3x4_C1_PIN_D8, OUTPUT);
  100.     pinMode(keypad_Keypad3x4_C2_PIN_D9, OUTPUT);
  101.     pinMode(keypad_Keypad3x4_C3_PIN_D10, OUTPUT);
  102. }
  103.  
  104. void loop(void)
  105. {
  106.     // put your main code here, to run repeatedly:
  107.     updateOutputs(); // Refresh output data
  108.     checkKeypad();   // Check for keypad input
  109. }
  110.  
  111. void updateOutputs()
  112. {
  113.     digitalWrite(keypad_Keypad3x4_C1_PIN_D8, keypad_Keypad3x4_C1_PIN_D8_rawData);
  114.     digitalWrite(keypad_Keypad3x4_C2_PIN_D9, keypad_Keypad3x4_C2_PIN_D9_rawData);
  115.     digitalWrite(keypad_Keypad3x4_C3_PIN_D10, keypad_Keypad3x4_C3_PIN_D10_rawData);
  116. }
  117.  
  118. void checkKeypad() {
  119.     char key = keypad.getKey();
  120.     static String inputPIN = ""; // Store the input PIN
  121.  
  122.     if (key) {
  123.         // Append the pressed key to the input PIN
  124.         inputPIN += key;
  125.  
  126.         // Check if the input PIN is correct
  127.         if (inputPIN.length() == CORRECT_PIN.length()) {
  128.             if (inputPIN == CORRECT_PIN) {
  129.                 if (doorServo.isLocked()) {
  130.                     doorServo.unlock(); // Unlock the door
  131.                 } else {
  132.                     doorServo.lock(); // Lock the door
  133.                 }
  134.             }
  135.             inputPIN = ""; // Reset input after checking
  136.         }
  137.     }
  138. }
  139.  
  140. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement