Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Marquessa MacKenzie
- December 6th, 2019
- Escape Room Lockbox
- Sources:
- LCD I2C Info: https://dronebotworkshop.com/lcd-displays-arduino/
- Keypad Door Lock: https://www.electronoobs.com/eng_arduino_tut11.php
- */
- #include <Wire.h> // Including Libraries
- #include <LiquidCrystal_I2C.h>
- LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); //Configures the LCD so that the data can be read
- #define I2C_ADDR 0x27 // Defining the LCD pins
- #define BACKLIGHT_PIN 3
- #define En_pin 2
- #define Rw_pin 1
- #define Rs_pin 0
- #define D4_pin 4
- #define D5_pin 5
- #define D6_pin 6
- #define D7_pin 7
- #include <Servo.h> //Including More Libraries...
- #include <Keypad.h>
- //Variables
- int mot_min = 90; // Setting The Minimum Servo Angle (Locked)
- int mot_max = 180; //Setting The Maximum Servo Angle (Unlocked)
- int character = 0; // Setting The Characters To Start Position
- int activated = 0; // Activating The Starting Position
- // Setting the initial print out and the string positions that will be useful for later:
- char Str[16] = {' ', ' ', ' ', ' ', ' ', ' ', '-', '*', '*', '*', ' ', ' ', ' ', ' ', ' ', ' '};
- //Pins
- Servo myservo; // Naming Servo
- 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)
- int external = 12; //pin to inside open (same as above)
- int internal = 13; //pin to inside close (same as above)
- //Keypad configuration
- const byte ROWS = 4; //four rows
- const byte COLS = 4; //four columns
- //define the symbols on the buttons of the keypads
- char keymap[ROWS][COLS] = {
- {'(', '&', '@', '*'}, // Changing the printout on the screen to not match what was on the key pad
- {'$', '^', '?', '!'},
- {'%', ')', '+', '#'},
- {'A', 'B', 'C', 'D'}
- // {'1','4','7','*'}, // This was the original setting
- // {'2','5','8','0'},
- // {'3','6','9','#'},
- // {'A','B','C','D'}
- };
- byte rowPins[ROWS] = {5, 4, 3, 2}; // Connecting to the row pinouts of the keypad
- byte colPins[COLS] = {9, 8, 7, 6}; // Connecting to the column pinouts of the keypad
- // Naming and initializing the keypad
- Keypad customKeypad = Keypad( makeKeymap(keymap), rowPins, colPins, ROWS, COLS);
- void setup() {
- Serial.begin(9600); // Running serial to see print out
- myservo.attach(10); // attach the servo to pin D10
- // pinMode(buzzer,OUTPUT);
- // pinMode(external,INPUT);
- // pinMode(internal,INPUT);
- //Init the screen and print the first text
- lcd.begin(16, 2); //Starting position for LCD
- lcd.backlight(); // Turing on the backlight
- // lcd.clear();
- lcd.setCursor(4, 0); // Setting the cursor to the correct spot to print out:
- lcd.print("PASSWORD");
- lcd.setCursor(0, 1); // Setting the cursor on the next line to print out:
- lcd.print(" -*** ");
- myservo.write(mot_min); // Putting the servo in the closed position first
- }
- void loop() {
- char customKey = customKeypad.getKey(); // This function reads the pressed key
- if (customKey) {
- 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:
- {
- Serial.println(customKey); //Register the custom key
- Str[6] = customKey; // on string position 6
- lcd.clear(); // clear screen
- lcd.setCursor(4, 0); // set the cursor at position 4 on LCD
- lcd.print("PASSWORD"); // LCD reads Password
- lcd.setCursor(0, 1); // Set the cursor to the 2nd line
- lcd.print(Str); // print the defined string variable
- }
- if (character == 1) // Progressing onto 2 of 5 with the same pattern as above.
- {
- Serial.println(customKey);
- Str[7] = customKey; // on string position 7
- lcd.clear();
- lcd.setCursor(4, 0);
- lcd.print("PASSWORD");
- lcd.setCursor(0, 1);
- lcd.print(Str);
- }
- if (character == 2) // Now to 3 of 5
- {
- Serial.println(customKey);
- Str[8] = customKey; // string position 8
- lcd.clear();
- lcd.setCursor(4, 0);
- lcd.print("PASSWORD");
- lcd.setCursor(0, 1);
- lcd.print(Str);
- }
- if (character == 3) // 4 of 5
- {
- Serial.println(customKey);
- Str[9] = customKey; // This is string position 9. This is the last one of the preprogrammed characters that will show up on the LDC
- lcd.clear();
- lcd.setCursor(4, 0);
- lcd.print("PASSWORD");
- lcd.setCursor(0, 1);
- lcd.print(Str);
- }
- if (character == 4) // 5 of 5
- {
- Serial.println(customKey);
- Str[10] = customKey; // This is the unlocking button. If you do not press then the box will not unlock.
- activated = 1; // When the 10th string position is activated then attempt to run the unlocking code
- }
- 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
- }
- if (activated == 1) //If the unlocking key is activated at the 10th position on the string then run the possibility of opening the box.
- {
- 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
- {
- myservo.write(mot_max); // If the correct code is entered, rotate the servo to the maximum angle
- activated = 2; // If the unlocking code was right then run the accepted code:
- delay(500); // delay 500 ms
- lcd.clear(); // clear the LCD
- lcd.setCursor(4, 0); // set the cursor
- lcd.print("ACCEPTED"); // LCD print accepted
- // 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
- // delay(250);
- // analogWrite(buzzer,200);
- // delay(250);
- // analogWrite(buzzer,180);
- // delay(250);
- // analogWrite(buzzer,250);
- // delay(250);
- // analogWrite(buzzer,LOW);
- // delay(1000);
- lcd.clear(); // clear the LCD scree
- lcd.setCursor(2, 0); // set the cursor the position 2 on the top row
- lcd.print("YOUR PASSWORD"); // print out YOUR PASSWORD
- lcd.setCursor(3, 1); // Set cursor to position 3 on the 2nd row
- lcd.print("IS CORRECT"); // Print out IS CORRECT
- delay(1000); // delay 1 sec
- lcd.clear(); // clear the LCD screen
- lcd.setCursor(3, 0); // set cursor to position 3 on the top row
- lcd.print("PERMISSION"); // print out PERMISSION
- lcd.setCursor(3, 1); // set cursor to position 3 on the 2nd row
- lcd.print("IS GRANTED"); // print out IS GRANTED
- }
- else // If the password is not correct run this code:
- {
- lcd.clear(); // clear the LCD screen
- lcd.setCursor(2, 0); // set the cursor to position 2
- lcd.print("YOUR PASSWORD"); // print out YOUR PASSWORD
- lcd.setCursor(1, 1); // set the cursor to position 1 on the 2nd row
- lcd.print("IS NOT CORRECT"); // print out IS NOT CORRECT
- 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
- delay(3000);
- analogWrite(buzzer, LOW);
- character = 0; // Reset and rerun the beginning of the loop where the character = 0
- Str[6] = '-';
- Str[7] = '*';
- Str[8] = '*';
- Str[9] = '*';
- Str[10] = ' ';
- activated = 0; // Begin the loop over again
- lcd.clear();
- lcd.setCursor(4, 0);
- lcd.print("PASSWORD");
- lcd.setCursor(0, 1);
- lcd.print(Str);
- }
- }
- if (activated == 2) // alternatively if the password has been entered in correctly and the box is left unlocked:
- {
- if (customKey == 'B' ) // setting the custom key B to re-lock the box
- {
- myservo.write(mot_min); // If B is pressed then change the position of the servo to return to the most minimum position (aka: locked)
- activated = 0; //activate and run the main set up loop over again
- character = 0;
- Str[6] = '-';
- Str[7] = '*';
- Str[8] = '*';
- Str[9] = '*';
- Str[10] = ' ';
- lcd.clear();
- lcd.setCursor(4, 0);
- lcd.print("PASSWORD");
- lcd.setCursor(0, 1);
- lcd.print(Str);
- }
- }
- }
Add Comment
Please, Sign In to add comment