Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Brandan Tyler Lasley @Brandantl 2014 */
- /* Last Modifed: Thursday Augest 28 00:32:00
- Pin Specifications:
- ===== LCD =====
- SCL -> ANALOG 5
- SDA -> ANALOG 4
- VCC -> 5V OUTPUT
- GND -> 5V GROUND
- ==== Matrix Keypad ===
- With the keypad face up, the wire nearest to the star key is the first wire then (from left to right) second, third, forth etc.
- The first wire goes in Digital 9, then the second in 8 and all the way to the 2nd digital pin.
- So there should be 8 toal wires connected.
- Keypad Pin Connects to Arduino Pin...
- 1 D9
- 2 D8
- 3 D7
- 4 D6
- 5 D5
- 6 D4
- 7 D3
- 8 D2
- Now I'm going to explain what each wire repersents so if you wish to free up some digital pins by remving some colums.
- Wires 1-4 are the rows and 5-8 are the columns. So I'm guessing if you remove Wire #4 and wire #8
- then it should disabled A-D and * - D. Haven't tested that though.
- The current code below represents a key lock system where a specific password needed to continue.
- Almost every key is used, with the exception of ABC.
- STAR = System Clear
- Pound = Enter
- D = LCD backlight on/off
- Modify the code to suit your .need.
- The libaries required DO NOT come with intels Arudino IDE. You'll need to download Keypad.h and LiquidCrytal-I2C seperatly.
- LiquidCrystal_I2C for Sainsmart LCD 2004
- http://www.dfrobot.com/wiki/index.php?title=I2C/TWI_LCD1602_Module_(SKU:_DFR0063)
- http://playground.arduino.cc/code/Keypad
- To import these libaries dont unzip them,simpily go to Sketch -> Import Libary and select the zip.
- Also remember arrays start on 0.
- */
- #include <Keypad.h>
- #include <Wire.h>
- #include <LiquidCrystal_I2C.h>
- // Variables
- const int R_Keypad1 = 9; // Row
- const int R_Keypad2 = 8; // Row
- const int R_Keypad3 = 7; // Row
- const int R_Keypad4 = 6; // Row
- const int C_Keypad5 = 5; // Col
- const int C_Keypad6 = 4; // Col
- const int C_Keypad7 = 3; // Col
- const int C_Keypad8 = 2; // Col
- int LCD_Address = 0x3F;
- const byte numRows= 4; //number of rows on the keypad
- const byte numCols= 4; //number of columns on the keypad
- //keymap defines the key pressed according to the row and columns just as appears on the keypad
- char keymap[numRows][numCols]=
- {
- {
- '1', '2', '3', 'A' }
- ,
- {
- '4', '5', '6', 'B' }
- ,
- {
- '7', '8', '9', 'C' }
- ,
- {
- '*', '0', '#', 'D' }
- };
- LiquidCrystal_I2C lcd(LCD_Address,20,4); // set the LCD address to 0x27 for a 16 chars and 2 line display
- //LiquidCrystal_I2C lcd(i2c_addr, en, rw, rs, d4, d5, d6, d7, BACKLIGHT_PIN, POSITIVE); // Set the LCD I2C address
- //Code that shows the the keypad connections to the arduino terminals
- byte rowPins[numRows] = {
- R_Keypad1,R_Keypad2,R_Keypad3,R_Keypad4}; //Rows 0 to 3
- byte colPins[numCols]= {
- C_Keypad5,C_Keypad6,C_Keypad7,C_Keypad8}; //Columns 0 to 3
- // Notice, if you're going to remove a wire, remove it from here too and change the numRows and numCols to the appropiate setting.
- // You may also want to edit the key map too, test it first though.
- //initializes an instance of the Keypad class
- Keypad myKeypad= Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);
- String password = "";
- boolean syslock = false; // Lets us know if the password accecpted/deny screen is active.
- boolean lighton = true; // Lets us know the state of our LCD screen backlight on/off. by default program load its on.
- void setup()
- {
- lcd.init(); // initialize the lcd
- lcd.init();
- lcd.backlight(); // turns on LCD backlight on program boot
- lcd.setCursor(0,1); // Moves cursor to position 0 row 1.
- lcd.print ("Pass: "); // Prints out "Pass: " on position 0, row 1.
- }
- void loop() // You can also do this a more efficent way using event handlers.
- {
- char keypressed = myKeypad.getKey(); // Dunno how this works, assume its magic.
- if (keypressed == '*') { // If key * is pressed execute the reset system function.
- reset_system();
- }
- else if (keypressed == '#') { // If # is pressed then execute the pound key function.
- pound_key();
- }
- else if (keypressed == 'D') { // If D turn off/on the LCD backlight.
- if (lighton) {
- lcd.noBacklight(); // Off
- lighton = false; // Off
- }
- else {
- lighton = true; // On
- lcd.backlight(); // On
- }
- }
- else if (keypressed != NO_KEY) { // If neither of the keys above are pressed, and there is a key pressed.
- if (!syslock) { // Checks if there is an active system lock enganged.
- if (password.length() < 14) { // Makes sure the password is less than 14 chars (LCD screen is small)
- lcd.print('*'); // Print *'s instead of of whats entered.
- password += keypressed; // add the key to the password string.
- }
- else {
- pound_key();// if password exceeds 14 chars then execute the pound key function, which checks and validates the password.
- }
- }
- }
- }
- void pound_key() {
- if (check_password(password)) {
- lcd.clear(); // Clear LCD screen
- lcd.setCursor(0, 1); // Set cursor to position 0, line 1
- lcd.print("Password Accecpted!");// Print Text to line 1, position 0.
- lcd.setCursor(0, 2); // Set cursor to line 2, position 0.
- lcd.print("Press * to reset."); // Print Text to line 2, position 0.
- syslock = true; // engage system lock, this prevents text from being entered on accept/deny scren.
- }
- else {
- lcd.clear(); // Clear LCD screen
- lcd.setCursor(0, 1); // Set Cursor to line 1 (starts at 0)
- lcd.print("Access Denied!"); // Print text at position 0, line 1.
- lcd.setCursor(0, 2); // Set Cursor to line 2
- lcd.print("Press * to reset."); // Print text to line 2 position 0
- syslock = true; // engage system lock, this prevents text from being entered on accept/deny scren.
- }
- }
- void reset_system() {
- lcd.setCursor(0,1); // Set Cursor to line 1 (starts at 0)
- lcd.clear(); // Clear LCD screen
- lcd.print ("Pass: "); // Print "Pass: " with space starting at position 0 line 1.
- password = ""; // Clear the passwored entered.
- syslock = false; // Clear Syslock, this prevents text from being entered on the password accept/deny screen.
- }
- boolean check_password(String password) {
- if (password == "6666") { // check password
- return true; // If password matches return true.
- }
- else {
- return false; // else if it doesn't return false.
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement