Advertisement
pleasedontcode

Strongbox rev_02

Sep 17th, 2023
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 6.62 KB | Software | 0 0
  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: Strongbox
  13.     - Source Code compiled for: Arduino Mega
  14.     - Source Code created on: 2023-09-17 20:12:25
  15.     - Source Code generated by: Tom
  16.  
  17. ********* Pleasedontcode.com **********/
  18.  
  19. /********* User code review feedback **********
  20. #### Feedback 1 ####
  21. - complete the code.
  22. ********* User code review feedback **********/
  23.  
  24. /****** DEFINITION OF LIBRARIES *****/
  25. #include <Arduino.h>
  26. #include <Wire.h>
  27. #include <LiquidCrystal_I2C.h>
  28. #include <Keypad.h>
  29. #include <Servo.h>
  30. #include <EEPROM.h>
  31.  
  32. /****** SYSTEM REQUIREMENT 1 *****/
  33. // This is a project to create a strongbox with a keypad (to enter the code), a display (to show the state of strongbox) and a servo (to lock or unlock the door).
  34.  
  35. /****** SYSTEM REQUIREMENT 2 *****/
  36. // A code number has to be stored in EEPROM (the value is 3478) and used for comparison with the code provided by user.
  37.  
  38. /****** SYSTEM REQUIREMENT 3 *****/
  39. // At startup, Arduino has to initialize the display showing "Press digit code of 4 numbers:".
  40.  
  41. /****** SYSTEM REQUIREMENT 4 *****/
  42. // Periodically the system has to wait the code of 4 numbers from keypad. After each number is introduced, add a '*' character on the second row. When the 4 digits are introduced, check if the code is equal to the value saved in EEPROM.
  43.  
  44. /****** SYSTEM REQUIREMENT 5 *****/
  45. // Save on EEPROM also the state of machine which is composed of 2 states: "DOOR LOCKED" and "DOOR UNLOCKED".
  46.  
  47. /****** SYSTEM REQUIREMENT 6 *****/
  48. // If the code is equal to EEPROM value and the state is "DOOR LOCKED", unlock the door: drive the servo to 180 degrees, show on display 'DOOR UNLOCKED' for 2 seconds, and change the state to 'DOOR UNLOCKED'.
  49.  
  50. /****** SYSTEM REQUIREMENT 7 *****/
  51. // If the state is 'DOOR UNLOCKED', write on the display 'Enter 4 digits to lock the door', then wait for 4 digits on the keypad. And at each digit pressed, display a '*' character on the second row of the display.
  52.  
  53. /****** SYSTEM REQUIREMENT 8 *****/
  54. // After the 4 digits are entered, change the state from 'DOOR UNLOCKED' to 'DOOR LOCKED', save it in EEPROM, drive the servo to 0 degrees, display 'DOOR LOCKED' for 3 seconds, and save the code in EEPROM.
  55.  
  56. /****** FUNCTION PROTOTYPES *****/
  57. void setup(void);
  58. void loop(void);
  59.  
  60. /***** DEFINITION OF PWM OUTPUT PINS *****/
  61. const uint8_t servo_PIN_D2 = 2;
  62.  
  63. /***** DEFINITION OF I2C PINS *****/
  64. const uint8_t display_LCD1602I2C_I2C_PIN_SDA_D20 = 20;
  65. const uint8_t display_LCD1602I2C_I2C_PIN_SCL_D21 = 21;
  66. const uint8_t display_LCD1602I2C_I2C_SLAVE_ADDRESS = 39;
  67.  
  68. // Define the keypad layout
  69. const byte ROWS = 4; //four rows
  70. const byte COLS = 4; //four columns
  71. char keys[ROWS][COLS] = {
  72.   {'1','2','3','A'},
  73.   {'4','5','6','B'},
  74.   {'7','8','9','C'},
  75.   {'*','0','#','D'}
  76. };
  77. byte rowPins[ROWS] = {30, 31, 32, 33}; //connect to the row pinouts of the keypad
  78. byte colPins[COLS] = {34, 35, 36, 37}; //connect to the column pinouts of the keypad
  79.  
  80. // Create an instance of Keypad
  81. Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
  82.  
  83. // Create an instance of Servo
  84. Servo doorServo;
  85.  
  86. // Define the state of the door
  87. enum DoorState {
  88.   DOOR_LOCKED,
  89.   DOOR_UNLOCKED
  90. };
  91. DoorState doorState = DOOR_LOCKED;
  92.  
  93. // Define the code length
  94. const int CODE_LENGTH = 4;
  95.  
  96. // Define the code
  97. const int code[CODE_LENGTH] = {3, 4, 7, 8};
  98.  
  99. // Define the EEPROM address to store the code
  100. const int CODE_ADDRESS = 0;
  101.  
  102. // Define the EEPROM address to store the door state
  103. const int STATE_ADDRESS = CODE_ADDRESS + CODE_LENGTH;
  104.  
  105. // Define the LCD display
  106. LiquidCrystal_I2C lcd(display_LCD1602I2C_I2C_SLAVE_ADDRESS, 16, 2);
  107.  
  108. void setup(void)
  109. {
  110.   // Initialize the servo pin as output
  111.   pinMode(servo_PIN_D2, OUTPUT);
  112.  
  113.   // Attach the servo to the pin
  114.   doorServo.attach(servo_PIN_D2);
  115.  
  116.   // Initialize the display
  117.   lcd.begin(16, 2);
  118.  
  119.   // Display the initial message
  120.   lcd.print("Press digit code");
  121.   lcd.setCursor(0, 1);
  122.   lcd.print("of 4 numbers:");
  123.  
  124.   // Read the door state from EEPROM
  125.   int storedState = EEPROM.read(STATE_ADDRESS);
  126.   if (storedState == DOOR_UNLOCKED) {
  127.     doorState = DOOR_UNLOCKED;
  128.     lcd.clear();
  129.     lcd.print("DOOR UNLOCKED");
  130.     delay(2000);
  131.   }
  132. }
  133.  
  134. void loop(void)
  135. {
  136.   // Check if the door is locked
  137.   if (doorState == DOOR_LOCKED) {
  138.     // Wait for the code to be entered
  139.     displayCodeEntry();
  140.     int enteredCode[CODE_LENGTH];
  141.     for (int i = 0; i < CODE_LENGTH; i++) {
  142.       char key = keypad.waitForKey();
  143.       lcd.setCursor(i, 1);
  144.       lcd.print("*");
  145.       enteredCode[i] = key - '0';
  146.     }
  147.  
  148.     // Check if the entered code matches the stored code
  149.     bool codeMatch = true;
  150.     for (int i = 0; i < CODE_LENGTH; i++) {
  151.       if (enteredCode[i] != code[i]) {
  152.         codeMatch = false;
  153.         break;
  154.       }
  155.     }
  156.  
  157.     // If the code matches, unlock the door
  158.     if (codeMatch) {
  159.       unlockDoor();
  160.     }
  161.   } else if (doorState == DOOR_UNLOCKED) {
  162.     // Wait for the code to lock the door
  163.     displayCodeConfirmation();
  164.     int enteredCode[CODE_LENGTH];
  165.     for (int i = 0; i < CODE_LENGTH; i++) {
  166.       char key = keypad.waitForKey();
  167.       lcd.setCursor(i, 1);
  168.       lcd.print("*");
  169.       enteredCode[i] = key - '0';
  170.     }
  171.  
  172.     // Lock the door and save the code
  173.     lockDoor(enteredCode);
  174.   }
  175. }
  176.  
  177. // Function to display the code entry prompt on the LCD
  178. static void displayCodeEntry()
  179. {
  180.   lcd.clear();
  181.   lcd.print("Enter 4 digits");
  182.   lcd.setCursor(0, 1);
  183.   lcd.print("to unlock door:");
  184. }
  185.  
  186. // Function to display the code confirmation prompt on the LCD
  187. static void displayCodeConfirmation()
  188. {
  189.   lcd.clear();
  190.   lcd.print("Enter 4 digits");
  191.   lcd.setCursor(0, 1);
  192.   lcd.print("to lock door:");
  193. }
  194.  
  195. // Function to unlock the door
  196. static void unlockDoor()
  197. {
  198.   doorServo.write(180);
  199.   lcd.clear();
  200.   lcd.print("DOOR UNLOCKED");
  201.   delay(2000);
  202.   doorState = DOOR_UNLOCKED;
  203.   EEPROM.write(STATE_ADDRESS, doorState);
  204. }
  205.  
  206. // Function to lock the door
  207. static void lockDoor(int enteredCode[])
  208. {
  209.   doorServo.write(0);
  210.   lcd.clear();
  211.   lcd.print("DOOR LOCKED");
  212.   delay(3000);
  213.   doorState = DOOR_LOCKED;
  214.   EEPROM.write(STATE_ADDRESS, doorState);
  215.   for (int i = 0; i < CODE_LENGTH; i++) {
  216.     EEPROM.write(CODE_ADDRESS + i, enteredCode[i]);
  217.   }
  218. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement