Keypad with Password
Last updated
Was this helpful?
Last updated
Was this helpful?
- 1 Arduino Uno
- 1 4 by 4 keypad
- Assorted Wire
#include <Keypad.h> //library to simplify accepting input from the keypad
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {5, 4, 3, 2}; //connect to the column pinouts of the keypad
#define Password_Length 7
char Data[Password_Length]; // 6 is the number of chars it can hold + the null char = 7
char o_master[Password_Length] = "123456";
byte data_count = 0, master_count = 0;
bool Pass_is_good;
//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
void setup() {
Serial.begin(9600);
Serial.println("Please enter the password");
}
// the main loop of the program
void loop() {
char customKey = customKeypad.getKey();
//only runs when it receives data from the kaypad
if (customKey) {
if (customKey == '#') {
//compare the array to the password
if (!strcmp(Data, o_master)) {
Serial.println("");
Serial.println("Correct, you may enter");
clear_data();
delay(500);
Serial.println("Please enter the password");
}
//if the password doesn't match, ask the user to try again.
else {
Serial.println("");
Serial.println("Incorect. Please try again");
clear_data();
delay(500);
Serial.println("Please enter the open password");
}
}
else if(customKey == '*'){
clear_data();
Serial.println("");
Serial.println("Please enter the password");
}
else{
Serial.print(customKey); //print the current key entry to the serial monitor
Data[data_count] = customKey; //place the key entry in the data array
data_count ++; // move to the next spot in the array
}
}
}
//clears the list of numbers in the keypad array
void clear_data() {
while (data_count != 0) {
Data[data_count--] = 0;
}
return;
}