Components and supplies
Tegg 3x4 Keypad
Arduino UNO
KEYESTUDIO 1602 LCD with I2C backpack
Project description
Code
PasswordLogicProgram.ino
arduino
Program code. Modify to your needs. Have fun with it!
1// Program written by Dan Hostler, August 2020. 2// Feel free to use as you need to. 3// PayPal donations appreciated at danhostler1985@yahoo.com 4 5 6 7 8// LIBRARIES. (PLEASE INSTALL THESE LIBRARIES IF YOU HAVE NOT YET) 9#include <Wire.h> // deals with I2C connections 10#include <LiquidCrystal_I2C.h> // activates the LCD I2C library 11#include <Keypad.h> // activates the Keypad library 12 13 14 15 16// VARIABLES FOR THE 8-NUMBER PASSWORD INPUT DATA 17#define Password_Length 9 // Amount of digits plus one null character (8 + 1 = 9) defines password length 18char userInput[Password_Length]; // This variable will store the user input in a string 19char Master[Password_Length] = "12345678"; // This variable holds the correct password that the user string must match, change as needed 20char customKey; // This variable holds key input of every key pressed 21byte pressCount = 0; // This variable holds a counter for the amount of times the keys were pressed 22 23 24 25 26// KEYPAD CONFIGURATION (PLEASE MODIFY THIS SECTION IF YOUR KEYPAD WIRING IS DIFFERENT FROM BELOW) 27const byte ROWS = 4; // Constants for row and 28const byte COLS = 3; // column size of your keypad. 29 30char twelveKeys[ROWS][COLS] = // Physical layout of the keypad being used in this program 31{ 32 {'1', '2', '3'}, 33 {'4', '5', '6'}, 34 {'7', '8', '9'}, 35 {'C', '0', 'E'} // C = CLEAR, E = ENTER 36}; 37 38byte rowPins[ROWS] = {9, 8, 7, 6}; // Defines how rowPins are connected on the Arduino to the keypad: 9 = R1, 8 = R2, 7 = R3, 6 = R4 39byte colPins[COLS] = {5, 4, 3}; // Defines how colPins are connected on the Arduino to the keypad: 5 = C1, 4 = C2, 3 = C3, 2 = C2 (if you have a 4x4 keypad) 40 41 42 43 44// OBJECT CONFIGURATIONS 45Keypad customKeypad = Keypad(makeKeymap(twelveKeys), rowPins, colPins, ROWS, COLS); // Creates keypad "object" 46LiquidCrystal_I2C lcd(0x27, 16, 2); // Creates LCD "object" (I2C address, rows, columns) 47 48 49 50 51// PROGRAM SETUP 52void setup() { 53 54 // LCD Initialization 55 lcd.backlight(); 56 lcd.init(); 57 lcd.clear(); 58 59} 60 61 62 63 64// MAIN PROGRAM LOOP 65void loop() { 66 67 lcd.setCursor(0,0); 68 lcd.print(" Type Password:"); // One space is offset to center the text on the LCD 69 customKey = customKeypad.waitForKey(); // Program will halt here until a key is pushed 70 71 if (customKey != NO_KEY && customKey != 'C' && customKey != 'E') // If the user presses the number keys, the data is entered 72 { 73 userInput[pressCount] = customKey; // Password string is being collected by the userInput variable 74 lcd.setCursor(pressCount + 4, 1); // Four is added to pressCount to center the input on the LCD 75 lcd.print("*"); // Asterisks are printed for confidentiality 76 pressCount++; // Key press count will roll over until 8 digits are entered 77 } 78 79 else if (customKey == 'C') // C is to clear the entire password input from any point and start over again as 80 { // it calls up the clearData function. 81 lcd.clear(); 82 clearData(); 83 } 84 85 else if (customKey == 'E') // If less than 8 digits are entered, the input will be invalid and 86 { // an error message will flash, then call the clearData function to 87 lcd.clear(); // start the entering process all over again. 88 lcd.setCursor(0,0); 89 lcd.print("INVALID ENTRY"); 90 delay(800); 91 clearData(); 92 } 93 94 95 if (pressCount == 8) // Once 8 digits are entered, the function to check the password string is called up and 96 { // both the character and string data are sent to the function. 97 lcd.clear(); 98 waitHere(); 99 } 100 101} 102 103 104// THIS FUNCTION CHECKS THE PASSWORD STRING 105void waitHere(){ 106 107 lcd.setCursor(0,0); 108 lcd.print(" Type Password:"); 109 lcd.setCursor(0,1); 110 lcd.print(" ********"); // Password data is hidden as asterisks for confidentiality purposes and 111 // is offset by 4 spaces to center the input on the LCD 112 113 customKey = customKeypad.waitForKey(); // Program will halt here until a key is pushed 114 115 if (customKey != NO_KEY && customKey == 'E') // The ENTER button is pushed and the password goes through the routine. 116 { 117 lcd.clear(); 118 lcd.setCursor(0,0); 119 if (!strcmp(userInput, Master)) // The user input string is matched up with the Master password string and matches up 120 { // running the routine to grant access before returning back to the beginning. 121 lcd.setCursor(0,0); 122 lcd.print("ACCESS GRANTED."); 123 lcd.setCursor(0,1); 124 lcd.print("WELCOME!!"); 125 delay(5000); 126 clearData(); 127 } 128 else if (strcmp(userInput, Master)) // The user input string is matched up with the Master password string and does not match up 129 { // running the routine to deny access before returning back to the beginning. 130 lcd.setCursor(0,0); 131 lcd.print("ACCESS DENIED."); 132 delay(2000); 133 clearData(); 134 } 135 } 136 137 if (customKey != NO_KEY && customKey == 'C') // If the CLEAR button is pushed, the data is cleared and the program starts over. 138 { 139 lcd.clear(); 140 clearData(); 141 } 142 143 if (customKey != NO_KEY && customKey == '0') // This numberical button has no purpose and does nothing when pressed. 144 { 145 waitHere(); 146 } 147 148 if (customKey != NO_KEY && customKey == '1') // This numberical button has no purpose and does nothing when pressed. 149 { 150 waitHere(); 151 } 152 153 if (customKey != NO_KEY && customKey == '2') // This numberical button has no purpose and does nothing when pressed. 154 { 155 waitHere(); 156 } 157 158 if (customKey != NO_KEY && customKey == '3') // This numberical button has no purpose and does nothing when pressed. 159 { 160 waitHere(); 161 } 162 163 if (customKey != NO_KEY && customKey == '4') // This numberical button has no purpose and does nothing when pressed. 164 { 165 waitHere(); 166 } 167 168 if (customKey != NO_KEY && customKey == '5') // This numberical button has no purpose and does nothing when pressed. 169 { 170 waitHere(); 171 } 172 173 if (customKey != NO_KEY && customKey == '6') // This numberical button has no purpose and does nothing when pressed. 174 { 175 waitHere(); 176 } 177 178 if (customKey != NO_KEY && customKey == '7') // This numberical button has no purpose and does nothing when pressed. 179 { 180 waitHere(); 181 } 182 183 if (customKey != NO_KEY && customKey == '8') // This numberical button has no purpose and does nothing when pressed. 184 { 185 waitHere(); 186 } 187 188 if (customKey != NO_KEY && customKey == '9') // This numberical button has no purpose and does nothing when pressed. 189 { 190 waitHere(); 191 } 192 193} 194 195 196 197 198// OPTIONAL FUNCTIONS TO USE IN CASE YOU WISH TO EXPAND OUT YOUR PROGRAM 199//void accessGranted(){ 200// 201//} 202// 203//void accessDenied(){ 204// 205//} 206 207 208 209 210// CLEARS THE ARRAY DATA, STARTS PROGRAM ALL OVER AGAIN 211void clearData() { 212 while (pressCount != 0) 213 { 214 userInput[pressCount--] = 0; // Clears out the user input data, both digit and string data are reset to zero. 215 } 216 setup(); // Returns program back to the beginning 217} 218
PasswordLogicProgram.ino
arduino
Program code. Modify to your needs. Have fun with it!
1// Program written by Dan Hostler, August 2020. 2// Feel free to use as you need to. 3// PayPal donations appreciated at danhostler1985@yahoo.com 4 5 6 7 8// LIBRARIES. (PLEASE INSTALL THESE LIBRARIES IF YOU HAVE NOT YET) 9#include <Wire.h> // deals with I2C connections 10#include <LiquidCrystal_I2C.h> // activates the LCD I2C library 11#include <Keypad.h> // activates the Keypad library 12 13 14 15 16// VARIABLES FOR THE 8-NUMBER PASSWORD INPUT DATA 17#define Password_Length 9 // Amount of digits plus one null character (8 + 1 = 9) defines password length 18char userInput[Password_Length]; // This variable will store the user input in a string 19char Master[Password_Length] = "12345678"; // This variable holds the correct password that the user string must match, change as needed 20char customKey; // This variable holds key input of every key pressed 21byte pressCount = 0; // This variable holds a counter for the amount of times the keys were pressed 22 23 24 25 26// KEYPAD CONFIGURATION (PLEASE MODIFY THIS SECTION IF YOUR KEYPAD WIRING IS DIFFERENT FROM BELOW) 27const byte ROWS = 4; // Constants for row and 28const byte COLS = 3; // column size of your keypad. 29 30char twelveKeys[ROWS][COLS] = // Physical layout of the keypad being used in this program 31{ 32 {'1', '2', '3'}, 33 {'4', '5', '6'}, 34 {'7', '8', '9'}, 35 {'C', '0', 'E'} // C = CLEAR, E = ENTER 36}; 37 38byte rowPins[ROWS] = {9, 8, 7, 6}; // Defines how rowPins are connected on the Arduino to the keypad: 9 = R1, 8 = R2, 7 = R3, 6 = R4 39byte colPins[COLS] = {5, 4, 3}; // Defines how colPins are connected on the Arduino to the keypad: 5 = C1, 4 = C2, 3 = C3, 2 = C2 (if you have a 4x4 keypad) 40 41 42 43 44// OBJECT CONFIGURATIONS 45Keypad customKeypad = Keypad(makeKeymap(twelveKeys), rowPins, colPins, ROWS, COLS); // Creates keypad "object" 46LiquidCrystal_I2C lcd(0x27, 16, 2); // Creates LCD "object" (I2C address, rows, columns) 47 48 49 50 51// PROGRAM SETUP 52void setup() { 53 54 // LCD Initialization 55 lcd.backlight(); 56 lcd.init(); 57 lcd.clear(); 58 59} 60 61 62 63 64// MAIN PROGRAM LOOP 65void loop() { 66 67 lcd.setCursor(0,0); 68 lcd.print(" Type Password:"); // One space is offset to center the text on the LCD 69 customKey = customKeypad.waitForKey(); // Program will halt here until a key is pushed 70 71 if (customKey != NO_KEY && customKey != 'C' && customKey != 'E') // If the user presses the number keys, the data is entered 72 { 73 userInput[pressCount] = customKey; // Password string is being collected by the userInput variable 74 lcd.setCursor(pressCount + 4, 1); // Four is added to pressCount to center the input on the LCD 75 lcd.print("*"); // Asterisks are printed for confidentiality 76 pressCount++; // Key press count will roll over until 8 digits are entered 77 } 78 79 else if (customKey == 'C') // C is to clear the entire password input from any point and start over again as 80 { // it calls up the clearData function. 81 lcd.clear(); 82 clearData(); 83 } 84 85 else if (customKey == 'E') // If less than 8 digits are entered, the input will be invalid and 86 { // an error message will flash, then call the clearData function to 87 lcd.clear(); // start the entering process all over again. 88 lcd.setCursor(0,0); 89 lcd.print("INVALID ENTRY"); 90 delay(800); 91 clearData(); 92 } 93 94 95 if (pressCount == 8) // Once 8 digits are entered, the function to check the password string is called up and 96 { // both the character and string data are sent to the function. 97 lcd.clear(); 98 waitHere(); 99 } 100 101} 102 103 104// THIS FUNCTION CHECKS THE PASSWORD STRING 105void waitHere(){ 106 107 lcd.setCursor(0,0); 108 lcd.print(" Type Password:"); 109 lcd.setCursor(0,1); 110 lcd.print(" ********"); // Password data is hidden as asterisks for confidentiality purposes and 111 // is offset by 4 spaces to center the input on the LCD 112 113 customKey = customKeypad.waitForKey(); // Program will halt here until a key is pushed 114 115 if (customKey != NO_KEY && customKey == 'E') // The ENTER button is pushed and the password goes through the routine. 116 { 117 lcd.clear(); 118 lcd.setCursor(0,0); 119 if (!strcmp(userInput, Master)) // The user input string is matched up with the Master password string and matches up 120 { // running the routine to grant access before returning back to the beginning. 121 lcd.setCursor(0,0); 122 lcd.print("ACCESS GRANTED."); 123 lcd.setCursor(0,1); 124 lcd.print("WELCOME!!"); 125 delay(5000); 126 clearData(); 127 } 128 else if (strcmp(userInput, Master)) // The user input string is matched up with the Master password string and does not match up 129 { // running the routine to deny access before returning back to the beginning. 130 lcd.setCursor(0,0); 131 lcd.print("ACCESS DENIED."); 132 delay(2000); 133 clearData(); 134 } 135 } 136 137 if (customKey != NO_KEY && customKey == 'C') // If the CLEAR button is pushed, the data is cleared and the program starts over. 138 { 139 lcd.clear(); 140 clearData(); 141 } 142 143 if (customKey != NO_KEY && customKey == '0') // This numberical button has no purpose and does nothing when pressed. 144 { 145 waitHere(); 146 } 147 148 if (customKey != NO_KEY && customKey == '1') // This numberical button has no purpose and does nothing when pressed. 149 { 150 waitHere(); 151 } 152 153 if (customKey != NO_KEY && customKey == '2') // This numberical button has no purpose and does nothing when pressed. 154 { 155 waitHere(); 156 } 157 158 if (customKey != NO_KEY && customKey == '3') // This numberical button has no purpose and does nothing when pressed. 159 { 160 waitHere(); 161 } 162 163 if (customKey != NO_KEY && customKey == '4') // This numberical button has no purpose and does nothing when pressed. 164 { 165 waitHere(); 166 } 167 168 if (customKey != NO_KEY && customKey == '5') // This numberical button has no purpose and does nothing when pressed. 169 { 170 waitHere(); 171 } 172 173 if (customKey != NO_KEY && customKey == '6') // This numberical button has no purpose and does nothing when pressed. 174 { 175 waitHere(); 176 } 177 178 if (customKey != NO_KEY && customKey == '7') // This numberical button has no purpose and does nothing when pressed. 179 { 180 waitHere(); 181 } 182 183 if (customKey != NO_KEY && customKey == '8') // This numberical button has no purpose and does nothing when pressed. 184 { 185 waitHere(); 186 } 187 188 if (customKey != NO_KEY && customKey == '9') // This numberical button has no purpose and does nothing when pressed. 189 { 190 waitHere(); 191 } 192 193} 194 195 196 197 198// OPTIONAL FUNCTIONS TO USE IN CASE YOU WISH TO EXPAND OUT YOUR PROGRAM 199//void accessGranted(){ 200// 201//} 202// 203//void accessDenied(){ 204// 205//} 206 207 208 209 210// CLEARS THE ARRAY DATA, STARTS PROGRAM ALL OVER AGAIN 211void clearData() { 212 while (pressCount != 0) 213 { 214 userInput[pressCount--] = 0; // Clears out the user input data, both digit and string data are reset to zero. 215 } 216 setup(); // Returns program back to the beginning 217} 218
Downloadable files
Schematic for project setup
This schematic utilizes a 4x4 keypad but you can easily use a 4x3 keypad by negating pin 2.
Schematic for project setup
Comments
Only logged in users can leave comments
danhostler1985
0 Followers
•0 Projects
Table of contents
Intro
5
0