Advertisement
pleasedontcode

**Servo Lock** rev_47

Oct 5th, 2024
49
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-05 07:04:45
  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. - add a button that enable introduction of pin into keypad.
  34. ********* User code review feedback **********/
  35.  
  36. /****** DEFINITION OF LIBRARIES *****/
  37. #include <Deneyap_Servo.h>  // https://github.com/deneyapkart/deneyap-servo-arduino-library
  38. #include <Keypad.h>         // https://github.com/Chris--A/Keypad
  39.  
  40. /****** FUNCTION PROTOTYPES *****/
  41. void setup(void);
  42. void loop(void);
  43. void updateOutputs(void);
  44. void checkPin(void);
  45.  
  46. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  47. const uint8_t keypad_Keypad3x4_R1_PIN_D4 = 4;
  48. const uint8_t keypad_Keypad3x4_R2_PIN_D5 = 5;
  49. const uint8_t keypad_Keypad3x4_R3_PIN_D6 = 6;
  50. const uint8_t keypad_Keypad3x4_R4_PIN_D7 = 7;
  51. const uint8_t button_PIN_D2 = 2; // Button for PIN entry
  52.  
  53. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  54. const uint8_t keypad_Keypad3x4_C1_PIN_D8 = 8;
  55. const uint8_t keypad_Keypad3x4_C2_PIN_D9 = 9;
  56. const uint8_t keypad_Keypad3x4_C3_PIN_D10 = 10;
  57.  
  58. /***** DEFINITION OF PWM OUTPUT PINS *****/
  59. const uint8_t servo_Servomotor_PWMSignal_PIN_D3 = 3;
  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. uint8_t servo_Servomotor_PWMSignal_PIN_D3_rawData = 0;
  66.  
  67. const uint8_t KEYPAD3X4_ROWS = 4; // four rows
  68. const uint8_t KEYPAD3X4_COLS = 3; // three columns
  69. char Keys_Keypad3x4[KEYPAD3X4_ROWS][KEYPAD3X4_COLS] = {
  70.     {'1', '2', '3'},
  71.     {'4', '5', '6'},
  72.     {'7', '8', '9'},
  73.     {'*', '0', '#'},
  74. };
  75.  
  76. // Define the correct PIN
  77. const char correctPin[] = "1234"; // Example PIN
  78. char enteredPin[5]; // Buffer to store entered PIN
  79. int pinIndex = 0; // Index for entered PIN
  80.  
  81. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  82. // Servo instance
  83. Servo myServo;
  84.  
  85. void setup(void)
  86. {
  87.     // put your setup code here, to run once:
  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(button_PIN_D2, INPUT_PULLUP); // Initialize button pin
  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.     pinMode(servo_Servomotor_PWMSignal_PIN_D3, OUTPUT);
  98.  
  99.     myServo.attach(servo_Servomotor_PWMSignal_PIN_D3); // Attach servo to pin
  100. }
  101.  
  102. void loop(void)
  103. {
  104.     // put your main code here, to run repeatedly:
  105.     updateOutputs(); // Refresh output data
  106.  
  107.     // Check if the button is pressed
  108.     if (digitalRead(button_PIN_D2) == LOW) {
  109.         checkPin(); // Check entered PIN
  110.     }
  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.     analogWrite(servo_Servomotor_PWMSignal_PIN_D3, servo_Servomotor_PWMSignal_PIN_D3_rawData);
  119. }
  120.  
  121. void checkPin()
  122. {
  123.     // Simulate entering a PIN
  124.     char key = getKeypadKey(); // Function to get the key from keypad
  125.     if (key != '\0') {
  126.         if (key == '#') {
  127.             // Check if the entered PIN is correct
  128.             if (strcmp(enteredPin, correctPin) == 0) {
  129.                 // Toggle servo lock/unlock
  130.                 servo_Servomotor_PWMSignal_PIN_D3_rawData = (servo_Servomotor_PWMSignal_PIN_D3_rawData == 0) ? 180 : 0;
  131.                 myServo.write(servo_Servomotor_PWMSignal_PIN_D3_rawData); // Move servo
  132.             }
  133.             // Reset entered PIN
  134.             memset(enteredPin, 0, sizeof(enteredPin));
  135.             pinIndex = 0;
  136.         } else if (pinIndex < 4) {
  137.             enteredPin[pinIndex++] = key; // Store entered key
  138.         }
  139.     }
  140. }
  141.  
  142. char getKeypadKey() {
  143.     // Logic to read from the keypad
  144.     // This is a placeholder for the actual keypad reading logic
  145.     // You would need to implement this based on your keypad library
  146.     return '\0'; // Return the key pressed
  147. }
  148.  
  149. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement