Advertisement
pleasedontcode

Keypad Lock rev_95

Oct 6th, 2024
84
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 NOT compiled for: Arduino Nano ESP32
  14.     - Source Code created on: 2024-10-06 11:07:18
  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. /****** DEFINITION OF LIBRARIES *****/
  31. #include <Deneyap_Servo.h>  //https://github.com/deneyapkart/deneyap-servo-arduino-library
  32. #include <Keypad.h> //https://github.com/Chris--A/Keypad
  33. #include <EasyButton.h> //https://github.com/evert-arias/EasyButton
  34.  
  35. /****** FUNCTION PROTOTYPES *****/
  36. void setup(void);
  37. void loop(void);
  38. void updateOutputs();
  39. void handleWebRequests();
  40. void unlockDoor();
  41. void lockDoor();
  42. bool checkPIN(char* enteredPIN);
  43.  
  44. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  45. const uint8_t keypad_Keypad3x4_R1_PIN_D4        = 4;
  46. const uint8_t keypad_Keypad3x4_R2_PIN_D5        = 5;
  47. const uint8_t keypad_Keypad3x4_R3_PIN_D6        = 6;
  48. const uint8_t keypad_Keypad3x4_R4_PIN_D7        = 7;
  49. const uint8_t pinButton_PushButton_PIN_D2       = 2;
  50.  
  51. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  52. const uint8_t keypad_Keypad3x4_C1_PIN_D8        = 8;
  53. const uint8_t keypad_Keypad3x4_C2_PIN_D9        = 9;
  54. const uint8_t keypad_Keypad3x4_C3_PIN_D10       = 10;
  55.  
  56. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  57. bool keypad_Keypad3x4_C1_PIN_D8_rawData = 0;
  58. bool keypad_Keypad3x4_C2_PIN_D9_rawData = 0;
  59. bool keypad_Keypad3x4_C3_PIN_D10_rawData = 0;
  60.  
  61. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  62. float keypad_Keypad3x4_C1_PIN_D8_phyData = 0.0;
  63. float keypad_Keypad3x4_C2_PIN_D9_phyData = 0.0;
  64. float keypad_Keypad3x4_C3_PIN_D10_phyData = 0.0;
  65.  
  66. const uint8_t KEYPAD3X4_ROWS = 4; //four rows
  67. const uint8_t KEYPAD3X4_COLS = 3; //three columns
  68. char Keys_Keypad3x4[KEYPAD3X4_ROWS][KEYPAD3X4_COLS] = {
  69.     {'1','2','3'},
  70.     {'4','5','6'},
  71.     {'7','8','9'},
  72.     {'*','0','#'},
  73. };
  74.  
  75. // Servo object
  76. Deneyap_Servo servoMotor;
  77.  
  78. // Web server state
  79. bool doorLocked = true; // Initial state is locked
  80. const char correctPIN[] = "1234"; // Example correct PIN
  81.  
  82. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  83.  
  84. void setup(void)
  85. {
  86.     // put your setup code here, to run once:
  87.  
  88.     pinMode(keypad_Keypad3x4_R1_PIN_D4, INPUT_PULLUP);
  89.     pinMode(keypad_Keypad3x4_R2_PIN_D5, INPUT_PULLUP);
  90.     pinMode(keypad_Keypad3x4_R3_PIN_D6, INPUT_PULLUP);
  91.     pinMode(keypad_Keypad3x4_R4_PIN_D7, INPUT_PULLUP);
  92.     pinMode(pinButton_PushButton_PIN_D2, INPUT_PULLUP);
  93.  
  94.     pinMode(keypad_Keypad3x4_C1_PIN_D8, OUTPUT);
  95.     pinMode(keypad_Keypad3x4_C2_PIN_D9, OUTPUT);
  96.     pinMode(keypad_Keypad3x4_C3_PIN_D10, OUTPUT);
  97.  
  98.     // Attach the servo to a pin
  99.     servoMotor.attach(3); // Assuming the servo is connected to pin 3
  100.     servoMotor.write(0); // Start in locked position
  101.  
  102.     // Initialize web server
  103.     // (Web server setup code would go here)
  104. }
  105.  
  106. void loop(void)
  107. {
  108.     // put your main code here, to run repeatedly:
  109.     updateOutputs(); // Refresh output data
  110.     handleWebRequests(); // Handle incoming web requests
  111. }
  112.  
  113. void updateOutputs()
  114. {
  115.     digitalWrite(keypad_Keypad3x4_C1_PIN_D8, keypad_Keypad3x4_C1_PIN_D8_rawData);
  116.     digitalWrite(keypad_Keypad3x4_C2_PIN_D9, keypad_Keypad3x4_C2_PIN_D9_rawData);
  117.     digitalWrite(keypad_Keypad3x4_C3_PIN_D10, keypad_Keypad3x4_C3_PIN_D10_rawData);
  118. }
  119.  
  120. void handleWebRequests()
  121. {
  122.     // Handle web requests to check lock state
  123.     // (Web server request handling code would go here)
  124.     // Example: If a PIN is entered, check it
  125.     char enteredPIN[5]; // Buffer for entered PIN
  126.     if (/* condition to check if a PIN is entered */) {
  127.         if (checkPIN(enteredPIN)) {
  128.             if (doorLocked) {
  129.                 unlockDoor();
  130.             } else {
  131.                 lockDoor();
  132.             }
  133.         }
  134.     }
  135. }
  136.  
  137. bool checkPIN(char* enteredPIN) {
  138.     // Compare entered PIN with the correct PIN
  139.     return strcmp(enteredPIN, correctPIN) == 0;
  140. }
  141.  
  142. void unlockDoor()
  143. {
  144.     if (doorLocked) {
  145.         servoMotor.write(90); // Unlock position
  146.         doorLocked = false; // Update state
  147.     }
  148. }
  149.  
  150. void lockDoor()
  151. {
  152.     if (!doorLocked) {
  153.         servoMotor.write(0); // Lock position
  154.         doorLocked = true; // Update state
  155.     }
  156. }
  157.  
  158. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement