Skip to content

Instantly share code, notes, and snippets.

@arslnb
Created June 7, 2017 10:38
Show Gist options
  • Save arslnb/98760972afabb422f24c0f3a13a1bc8e to your computer and use it in GitHub Desktop.
Save arslnb/98760972afabb422f24c0f3a13a1bc8e to your computer and use it in GitHub Desktop.
16x2 LCD and 4X4 Keypad unlock for Arduino
#include <Wire.h>
#include <LiquidCrystal.h>
#include <SoftwareSerial.h>
#include <Keypad.h>
#define redLED 13
#define greenLED 10
#define relayPIN 12
String password = "2468";
String enteredString;
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {9, 8, 7, 6}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
LiquidCrystal lcd (A0, A1, A2, A3, A4, A5);
void setup(){
isLocked(false);
lcd.begin(16, 2);
lcd.setCursor(0, 0);
lcd.print(" Welcome");
lcd.setCursor(0, 1);
lcd.print(" Enter Password");
pinMode(redLED, OUTPUT);
pinMode(greenLED, OUTPUT);
Serial.begin(9600);
Serial.print("Hello World");
}
void loop(){
char key = keypad.getKey();
if (key){
enteredString = enteredString + key;
clearScreen();
lcd.print(" Enter Password ");
lcd.setCursor(0, 1);
lcd.print(" " + enteredString + " ");
if(enteredString.length() == 4){
if(enteredString == password){
enteredString = "";
clearScreen();
lcd.print(" Unlocked ");
isLocked(true);
} else if (enteredString.length() > 4) {
clearScreen();
lcd.print(" Incorrect ");
isLocked(false);
enteredString = "";
} else {
clearScreen();
lcd.print(" Incorrect ");
isLocked(false);
enteredString = "";
}
}
}
}
void isLocked(int locked){
if(locked){
digitalWrite(relayPIN, LOW);
digitalWrite(redLED, LOW);
digitalWrite(greenLED, HIGH);
}
else{
digitalWrite(relayPIN, HIGH);
digitalWrite(redLED, HIGH);
digitalWrite(greenLED, LOW);
}
}
void clearScreen(){
for (int i = 0; i < 16; ++i){
lcd.setCursor (i, 1);
lcd.write(' ');
lcd.setCursor (i, 2);
lcd.write(' ');
}
lcd.setCursor(0, 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment