Advertisement
pleasedontcode

Servo Lock rev_122

Oct 7th, 2024
63
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: Servo Lock
  13.     - Source Code NOT compiled for: Arduino Nano ESP32
  14.     - Source Code created on: 2024-10-07 22:43:22
  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.  
  37. /* START CODE */
  38.  
  39. /****** DEFINITION OF LIBRARIES *****/
  40. #include <Deneyap_Servo.h>  //https://github.com/deneyapkart/deneyap-servo-arduino-library
  41. #include <Keypad.h>         //https://github.com/Chris--A/Keypad
  42. #include "ServoControl.h"   // Include the ServoControl header
  43. #include "KeypadHandler.h"   // Include the KeypadHandler header
  44.  
  45. /****** FUNCTION PROTOTYPES *****/
  46. void setup(void);
  47. void loop(void);
  48. void updateOutputs(void);
  49.  
  50. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  51. const uint8_t keypad_Keypad3x4_R1_PIN_D4 = 4;
  52. const uint8_t keypad_Keypad3x4_R2_PIN_D5 = 5;
  53. const uint8_t keypad_Keypad3x4_R3_PIN_D6 = 6;
  54. const uint8_t keypad_Keypad3x4_R4_PIN_D7 = 7;
  55. const uint8_t pinButton_PushButton_PIN_D2 = 2;
  56.  
  57. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  58. const uint8_t keypad_Keypad3x4_C1_PIN_D8 = 8;
  59. const uint8_t keypad_Keypad3x4_C2_PIN_D9 = 9;
  60. const uint8_t keypad_Keypad3x4_C3_PIN_D10;
  61.  
  62. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  63. bool keypad_Keypad3x4_C1_PIN_D8_rawData = 0;
  64. bool keypad_Keypad3x4_C2_PIN_D9_rawData = 0;
  65. bool keypad_Keypad3x4_C3_PIN_D10_rawData = 0;
  66.  
  67. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  68. float keypad_Keypad3x4_C1_PIN_D8_phyData = 0.0;
  69. float keypad_Keypad3x4_C2_PIN_D9_phyData = 0.0;
  70. float keypad_Keypad3x4_C3_PIN_D10_phyData = 0.0;
  71.  
  72. const uint8_t KEYPAD3X4_ROWS = 4; // four rows
  73. const uint8_t KEYPAD3X4_COLS = 3; // three columns
  74. char Keys_Keypad3x4[KEYPAD3X4_ROWS][KEYPAD3X4_COLS] = {
  75.     {'1', '2', '3'},
  76.     {'4', '5', '6'},
  77.     {'7', '8', '9'},
  78.     {'*', '0', '#'},
  79. };
  80.  
  81. /***** DEFINITION OF PIN CODE *****/
  82. const String CORRECT_PIN = "1234"; // Define the correct PIN code
  83.  
  84. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  85. // Create instances for servo control and keypad handler
  86. ServoControl doorServo(9); // Attach the servo to pin 9
  87. KeypadHandler keypad(Keys_Keypad3x4, (byte[]){keypad_Keypad3x4_R1_PIN_D4, keypad_Keypad3x4_R2_PIN_D5, keypad_Keypad3x4_R3_PIN_D6, keypad_Keypad3x4_R4_PIN_D7}, (byte[]){keypad_Keypad3x4_C1_PIN_D8, keypad_Keypad3x4_C2_PIN_D9, keypad_Keypad3x4_C3_PIN_D10});
  88.  
  89. void setup(void)
  90. {
  91.     // put your setup code here, to run once:
  92.     pinMode(keypad_Keypad3x4_R1_PIN_D4, INPUT_PULLUP);
  93.     pinMode(keypad_Keypad3x4_R2_PIN_D5, INPUT_PULLUP);
  94.     pinMode(keypad_Keypad3x4_R3_PIN_D6, INPUT_PULLUP);
  95.     pinMode(keypad_Keypad3x4_R4_PIN_D7, INPUT_PULLUP);
  96.     pinMode(pinButton_PushButton_PIN_D2, INPUT_PULLUP);
  97.  
  98.     pinMode(keypad_Keypad3x4_C1_PIN_D8, OUTPUT);
  99.     pinMode(keypad_Keypad3x4_C2_PIN_D9, OUTPUT);
  100.     pinMode(keypad_Keypad3x4_C3_PIN_D10, OUTPUT);
  101. }
  102.  
  103. void loop(void)
  104. {
  105.     // put your main code here, to run repeatedly:
  106.     updateOutputs(); // Refresh output data
  107.     checkKeypad();   // Check for keypad input
  108. }
  109.  
  110. void updateOutputs()
  111. {
  112.     digitalWrite(keypad_Keypad3x4_C1_PIN_D8, keypad_Keypad3x4_C1_PIN_D8_rawData);
  113.     digitalWrite(keypad_Keypad3x4_C2_PIN_D9, keypad_Keypad3x4_C2_PIN_D9_rawData);
  114.     digitalWrite(keypad_Keypad3x4_C3_PIN_D10, keypad_Keypad3x4_C3_PIN_D10_rawData);
  115. }
  116.  
  117. void checkKeypad() {
  118.     char key = keypad.getKey();
  119.     static String inputPIN = ""; // Store the input PIN
  120.  
  121.     if (key) {
  122.         // Append the pressed key to the input PIN
  123.         inputPIN += key;
  124.  
  125.         // Check if the input PIN is correct
  126.         if (inputPIN.length() == CORRECT_PIN.length()) {
  127.             if (inputPIN == CORRECT_PIN) {
  128.                 if (doorServo.isLocked()) {
  129.                     doorServo.unlock(); // Unlock the door
  130.                 } else {
  131.                     doorServo.lock(); // Lock the door
  132.                 }
  133.             }
  134.             inputPIN = ""; // Reset input after checking
  135.         }
  136.     }
  137. }
  138.  
  139. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement