Advertisement
pleasedontcode

Servo Lockdown rev_97

Oct 6th, 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: Servo Lockdown
  13.     - Source Code NOT compiled for: Arduino Nano ESP32
  14.     - Source Code created on: 2024-10-06 11:15:42
  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 checkKeypadInput();
  40.  
  41. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  42. const uint8_t keypad_Keypad3x4_R1_PIN_D4        = 4;
  43. const uint8_t keypad_Keypad3x4_R2_PIN_D5        = 5;
  44. const uint8_t keypad_Keypad3x4_R3_PIN_D6        = 6;
  45. const uint8_t keypad_Keypad3x4_R4_PIN_D7        = 7;
  46. const uint8_t pinButton_PushButton_PIN_D2       = 2;
  47.  
  48. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  49. const uint8_t keypad_Keypad3x4_C1_PIN_D8        = 8;
  50. const uint8_t keypad_Keypad3x4_C2_PIN_D9        = 9;
  51. const uint8_t keypad_Keypad3x4_C3_PIN_D10       = 10;
  52.  
  53. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  54. bool keypad_Keypad3x4_C1_PIN_D8_rawData = 0;
  55. bool keypad_Keypad3x4_C2_PIN_D9_rawData = 0;
  56. bool keypad_Keypad3x4_C3_PIN_D10_rawData = 0;
  57.  
  58. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  59. float keypad_Keypad3x4_C1_PIN_D8_phyData = 0.0;
  60. float keypad_Keypad3x4_C2_PIN_D9_phyData = 0.0;
  61. float keypad_Keypad3x4_C3_PIN_D10_phyData = 0.0;
  62.  
  63. const uint8_t KEYPAD3X4_ROWS = 4; //four rows
  64. const uint8_t KEYPAD3X4_COLS = 3; //three columns
  65. char Keys_Keypad3x4[KEYPAD3X4_ROWS][KEYPAD3X4_COLS] = {
  66.     {'1','2','3'},
  67.     {'4','5','6'},
  68.     {'7','8','9'},
  69.     {'*','0','#'},
  70. };
  71.  
  72. // Instantiate the servo object
  73. Deneyap_Servo myServo;
  74.  
  75. // Define the correct PIN code
  76. const String correctPin = "1234"; // Example PIN code
  77. String enteredPin = ""; // Store entered PIN
  78.  
  79. void setup(void)
  80. {
  81.     // Initialize pin modes
  82.     pinMode(keypad_Keypad3x4_R1_PIN_D4, INPUT_PULLUP);
  83.     pinMode(keypad_Keypad3x4_R2_PIN_D5, INPUT_PULLUP);
  84.     pinMode(keypad_Keypad3x4_R3_PIN_D6, INPUT_PULLUP);
  85.     pinMode(keypad_Keypad3x4_R4_PIN_D7, INPUT_PULLUP);
  86.     pinMode(pinButton_PushButton_PIN_D2, INPUT_PULLUP);
  87.  
  88.     pinMode(keypad_Keypad3x4_C1_PIN_D8, OUTPUT);
  89.     pinMode(keypad_Keypad3x4_C2_PIN_D9, OUTPUT);
  90.     pinMode(keypad_Keypad3x4_C3_PIN_D10, OUTPUT);
  91.  
  92.     // Attach the servo to a pin (e.g., pin 3)
  93.     myServo.attach(3);
  94.     myServo.write(0); // Start with the door locked
  95. }
  96.  
  97. void loop(void)
  98. {
  99.     // Check keypad input
  100.     checkKeypadInput();
  101.     updateOutputs(); // Refresh output data
  102. }
  103.  
  104. void checkKeypadInput()
  105. {
  106.     char key = getKey(); // Function to get the pressed key from the keypad
  107.  
  108.     if (key != NO_KEY) {
  109.         if (key == '#') { // If '#' is pressed, check the entered PIN
  110.             if (enteredPin == correctPin) {
  111.                 // Toggle the servo state
  112.                 if (myServo.read() == 0) {
  113.                     myServo.write(90); // Unlock the door
  114.                 } else {
  115.                     myServo.write(0); // Lock the door
  116.                 }
  117.             }
  118.             enteredPin = ""; // Reset the entered PIN after checking
  119.         } else {
  120.             enteredPin += key; // Append the pressed key to the entered PIN
  121.         }
  122.     }
  123. }
  124.  
  125. void updateOutputs()
  126. {
  127.     digitalWrite(keypad_Keypad3x4_C1_PIN_D8, keypad_Keypad3x4_C1_PIN_D8_rawData);
  128.     digitalWrite(keypad_Keypad3x4_C2_PIN_D9, keypad_Keypad3x4_C2_PIN_D9_rawData);
  129.     digitalWrite(keypad_Keypad3x4_C3_PIN_D10, keypad_Keypad3x4_C3_PIN_D10_rawData);
  130. }
  131.  
  132. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement