marquessamackenzie

Lockbox for Escape Room

Dec 4th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.36 KB | None | 0 0
  1. /*
  2.    Marquessa MacKenzie
  3.    December 6th, 2019
  4.    Escape Room Lockbox
  5.    Sources:
  6.    LCD I2C Info: https://dronebotworkshop.com/lcd-displays-arduino/
  7.    Keypad Door Lock: https://www.electronoobs.com/eng_arduino_tut11.php
  8. */
  9.  
  10.  
  11. #include <Wire.h> // Including Libraries
  12. #include <LiquidCrystal_I2C.h>
  13. LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); //Configures the LCD so that the data can be read
  14. #define I2C_ADDR 0x27 // Defining the LCD pins
  15. #define BACKLIGHT_PIN 3
  16. #define En_pin 2
  17. #define Rw_pin 1
  18. #define Rs_pin 0
  19. #define D4_pin 4
  20. #define D5_pin 5
  21. #define D6_pin 6
  22. #define D7_pin 7
  23. #include <Servo.h> //Including More Libraries...
  24. #include <Keypad.h>
  25.  
  26. //Variables
  27. int mot_min = 90; // Setting The Minimum Servo Angle (Locked)
  28. int mot_max = 180; //Setting The Maximum Servo Angle (Unlocked)
  29. int character = 0; // Setting The Characters To Start Position
  30. int activated = 0; // Activating The Starting Position
  31. // Setting the initial print out and the string positions that will be useful for later:
  32. char Str[16] = {' ', ' ', ' ', ' ', ' ', ' ', '-', '*', '*', '*', ' ', ' ', ' ', ' ', ' ', ' '};
  33. //Pins
  34. Servo myservo; // Naming Servo
  35. int buzzer = 11;   //pin for the buzzer beep (this was not necessary but every time that I removed it there was something wrong with the code)
  36. int external = 12; //pin to inside open (same as above)
  37. int internal = 13; //pin to inside close (same as above)
  38.  
  39. //Keypad configuration
  40. const byte ROWS = 4; //four rows
  41. const byte COLS = 4; //four columns
  42. //define the symbols on the buttons of the keypads
  43. char keymap[ROWS][COLS] = {
  44.   {'(', '&', '@', '*'}, // Changing the printout on the screen to not match what was on the key pad
  45.   {'$', '^', '?', '!'},
  46.   {'%', ')', '+', '#'},
  47.   {'A', 'B', 'C', 'D'}
  48.   //  {'1','4','7','*'}, // This was the original setting
  49.   //  {'2','5','8','0'},
  50.   //  {'3','6','9','#'},
  51.   //  {'A','B','C','D'}
  52. };
  53. byte rowPins[ROWS] = {5, 4, 3, 2}; // Connecting to the row pinouts of the keypad
  54. byte colPins[COLS] = {9, 8, 7, 6}; // Connecting to the column pinouts of the keypad
  55.  
  56. // Naming and initializing the keypad
  57. Keypad customKeypad = Keypad( makeKeymap(keymap), rowPins, colPins, ROWS, COLS);
  58.  
  59. void setup() {
  60.   Serial.begin(9600); // Running serial to see print out
  61.   myservo.attach(10); // attach the servo to pin D10
  62.   //  pinMode(buzzer,OUTPUT);
  63.   //  pinMode(external,INPUT);
  64.   //  pinMode(internal,INPUT);
  65.   //Init the screen and print the first text
  66.   lcd.begin(16, 2); //Starting position for LCD
  67.   lcd.backlight(); // Turing on the backlight
  68.   //  lcd.clear();
  69.   lcd.setCursor(4, 0); // Setting the cursor to the correct spot to print out:
  70.   lcd.print("PASSWORD");
  71.   lcd.setCursor(0, 1); // Setting the cursor on the next line to print out:
  72.   lcd.print("      -***     ");
  73.   myservo.write(mot_min);  // Putting the servo in the closed position first
  74.  
  75. }
  76.  
  77. void loop() {
  78.   char customKey = customKeypad.getKey(); // This function reads the pressed key
  79.  
  80.   if (customKey) {
  81.  
  82.     if (character == 0) // Setting 1 of 5 custom keys to print out what was entered on the keypad based off the string variable from earlier. If the character is equal to 0:
  83.     {
  84.       Serial.println(customKey); //Register the custom key
  85.       Str[6] = customKey;  // on string position 6
  86.       lcd.clear(); // clear screen
  87.       lcd.setCursor(4, 0); // set the cursor at position 4 on LCD
  88.       lcd.print("PASSWORD"); // LCD reads Password
  89.       lcd.setCursor(0, 1); // Set the cursor to the 2nd line
  90.       lcd.print(Str); // print the defined string variable
  91.  
  92.     }
  93.  
  94.     if (character == 1) // Progressing onto 2 of 5 with the same pattern as above.
  95.     {
  96.       Serial.println(customKey);
  97.       Str[7] = customKey; // on string position 7
  98.       lcd.clear();
  99.       lcd.setCursor(4, 0);
  100.       lcd.print("PASSWORD");
  101.       lcd.setCursor(0, 1);
  102.       lcd.print(Str);
  103.  
  104.     }
  105.  
  106.     if (character == 2) // Now to 3 of 5
  107.     {
  108.       Serial.println(customKey);
  109.       Str[8] = customKey; // string position 8
  110.       lcd.clear();
  111.       lcd.setCursor(4, 0);
  112.       lcd.print("PASSWORD");
  113.       lcd.setCursor(0, 1);
  114.       lcd.print(Str);
  115.  
  116.     }
  117.  
  118.     if (character == 3) // 4 of 5
  119.     {
  120.       Serial.println(customKey);
  121.       Str[9] = customKey; // This is string position 9. This is the last one of the preprogrammed characters that will show up on the LDC
  122.       lcd.clear();
  123.       lcd.setCursor(4, 0);
  124.       lcd.print("PASSWORD");
  125.       lcd.setCursor(0, 1);
  126.       lcd.print(Str);
  127.  
  128.     }
  129.  
  130.     if (character == 4) // 5 of 5
  131.     {
  132.       Serial.println(customKey);
  133.       Str[10] = customKey; // This is the unlocking button. If you do not press then the box will not unlock.
  134.       activated = 1; // When the 10th string position is activated then attempt to run the unlocking code
  135.  
  136.     }
  137.     character = character + 1; // Once one character from the keypad is entered, move onto the next position on the LCD screen and register a new spot on the string
  138.   }
  139.  
  140.   if (activated == 1) //If the unlocking key is activated at the 10th position on the string then run the possibility of opening the box.
  141.   {
  142.     if (Str[10] = 'A' && character == 5 && Str[6] == '$' && Str[7] == '!' && Str[8] == '@' && Str[9] == '%' ) //This is the code that will unlock the box. It is possible to change as needed
  143.     {
  144.       myservo.write(mot_max); // If the correct code is entered, rotate the servo to the maximum angle
  145.       activated = 2; // If the unlocking code was right then run the accepted code:
  146.       delay(500);  // delay 500 ms
  147.       lcd.clear(); // clear the LCD
  148.       lcd.setCursor(4, 0); // set the cursor
  149.       lcd.print("ACCEPTED"); // LCD print accepted
  150.       //      analogWrite(buzzer,240); // I did not use a buzzer but whenever I tried to remove this code the whole system would not work. So I am leaving it in for the sake of sanity
  151.       //      delay(250);
  152.       //      analogWrite(buzzer,200);
  153.       //      delay(250);
  154.       //      analogWrite(buzzer,180);
  155.       //      delay(250);
  156.       //      analogWrite(buzzer,250);
  157.       //      delay(250);
  158.       //      analogWrite(buzzer,LOW);
  159.       //      delay(1000);
  160.  
  161.       lcd.clear(); // clear the LCD scree
  162.       lcd.setCursor(2, 0); // set the cursor the position 2 on the top row
  163.       lcd.print("YOUR PASSWORD"); // print out YOUR PASSWORD
  164.       lcd.setCursor(3, 1); // Set cursor to position 3 on the 2nd row
  165.       lcd.print("IS CORRECT"); // Print out IS CORRECT
  166.       delay(1000); // delay 1 sec
  167.  
  168.       lcd.clear(); // clear the LCD screen
  169.       lcd.setCursor(3, 0); // set cursor to position 3 on the top row
  170.       lcd.print("PERMISSION"); // print out PERMISSION
  171.       lcd.setCursor(3, 1); // set cursor to position 3 on the 2nd row
  172.       lcd.print("IS GRANTED"); // print out IS GRANTED
  173.  
  174.     }
  175.     else // If the password is not correct run this code:
  176.     {
  177.       lcd.clear(); // clear the LCD screen
  178.       lcd.setCursor(2, 0); // set the cursor to position 2
  179.       lcd.print("YOUR PASSWORD"); // print out YOUR PASSWORD
  180.       lcd.setCursor(1, 1); // set the cursor to position 1 on the 2nd row
  181.       lcd.print("IS NOT CORRECT"); // print out IS NOT CORRECT
  182.       analogWrite(buzzer, 150); //If I commented out this portion of the code, the whole code would not work as well. So I have just left it in to run even tho there was no buzzer
  183.       delay(3000);
  184.       analogWrite(buzzer, LOW);
  185.       character = 0; // Reset and rerun the beginning of the loop where the character = 0
  186.       Str[6] = '-';
  187.       Str[7] = '*';
  188.       Str[8] = '*';
  189.       Str[9] = '*';
  190.       Str[10] = ' ';
  191.       activated = 0; // Begin the loop over again
  192.       lcd.clear();
  193.       lcd.setCursor(4, 0);
  194.       lcd.print("PASSWORD");
  195.       lcd.setCursor(0, 1);
  196.       lcd.print(Str);
  197.     }
  198.   }
  199.   if (activated == 2) // alternatively if the password has been entered in correctly and the box is left unlocked:
  200.   {
  201.     if (customKey == 'B' ) // setting the custom key B to re-lock the box
  202.     {
  203.       myservo.write(mot_min); // If B is pressed then change the position of the servo to return to the most minimum position (aka: locked)
  204.       activated = 0; //activate and run the main set up loop over again
  205.       character = 0;
  206.       Str[6] = '-';
  207.       Str[7] = '*';
  208.       Str[8] = '*';
  209.       Str[9] = '*';
  210.       Str[10] = ' ';
  211.       lcd.clear();
  212.       lcd.setCursor(4, 0);
  213.       lcd.print("PASSWORD");
  214.       lcd.setCursor(0, 1);
  215.       lcd.print(Str);
  216.  
  217.     }
  218.   }
  219. }
Add Comment
Please, Sign In to add comment