/* Arduino Security System with the Keypad and LCD
Simple one-digit password
*/
#include <LiquidCrystal.h> //include LCD library
#include <Keypad.h> //include keypad library
#define redLED 11 //define the LED pins
#define greenLED 12
const byte rows = 4; //number of the keypad rows
const byte cols = 4; // number of the keypad columns
char keyMap [rows] [cols] = { //define the symbols on the buttons of the keypad
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
char password ='6'; //create a password
byte rowPins [rows] = {1, 2, 3, 4}; // digital pins related to the row pins of the keypad
byte colPins [cols] = {5, 6, 7, 8}; // digital pins related to the column pins of the keypad
Keypad myKeypad = Keypad( makeKeymap(keyMap), rowPins, colPins, rows, cols);
LiquidCrystal lcd (A0, A1, A2, A3, A4, A5); // pins of the LCD. (RS, E, D4, D5, D6, D7)
void setup(){
lcd.begin(16, 2);
pinMode(redLED, OUTPUT); //set the LED as an output
pinMode(greenLED, OUTPUT);
setLocked (true); //state of the password
}
void loop(){
// char whichKey = myKeypad.getKey(); //define which key is pressed with getKey
lcd.setCursor(0, 0);
lcd.print(" Welcome");
lcd.setCursor(0, 1);
lcd.print(" Enter Password");
char whichKey = myKeypad.waitForKey(); //define which key is pressed with getKey
if((whichKey == '*') || (whichKey == '#') || (whichKey == 'A') || (whichKey == 'B') || (whichKey == 'C') ||( whichKey == 'D'))
{
// location=0;
setLocked (true);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" Invalid Key! ");
delay(1000);
lcd.clear();
}
if(whichKey == password )
{
setLocked (false);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" Accepted ");
delay(1000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.clear();
setLocked (true);
}
else
{
setLocked (true);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" Try again ");
delay(1000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.clear();
}
delay(200);
}
void setLocked(int locked){
if(locked){
digitalWrite(redLED, HIGH);
digitalWrite(greenLED, LOW);
}
else{
digitalWrite(redLED, LOW);
digitalWrite(greenLED, HIGH);
}}