I am a kinda new to arduino this is my second project, I got a code from the forum and made some heavy modification, it's working but I am not sure if I did everything ok, if someone with free time could take a look and help with some improvements, thanks in advance.
This code is for a keypad with LCD that uses two passwords to open two doors.
//Code by Reyfus
//Original Code by: ironicDeveloper 12/18/2011
//Help & parts of code from arduino forum
#include <Keypad.h> //Includes keypad library
#include <LiquidCrystal.h> //Includes LCD Library
LiquidCrystal lcd(12, 11, 17, 16, 15, 14); //Set LCD Pins
const byte ROWS = 4; //Set Leypad Pins
const byte COLS = 3;
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte rowPins[ROWS] = {5, 4, 3, 2};
byte colPins[COLS] = {8, 7, 6};
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS ); //Creates the keypad
char inputArray[4]; //Creates the input array
char Main[4] = {'4','6','2','7'}; //creates arra for correct password first door
char Guest[4] = {'1','2','3','4'}; //creates arra for correct Password Second door
#define registerPin 10 //First door open pin
#define secondPin 9 //Second door open pin
int i = 0; //variable to count number of key pressed
long tempo = 0; //variable that counts inactive time for display reset
void setup()
{
lcd.begin(16, 2); //start the lcd
pinMode(registerPin, OUTPUT); //set First door open pin
pinMode(secondPin, OUTPUT); //set Second door open pin
lcd.print("Enter password"); //Print Message
lcd.setCursor(0, 1); //put cursor on second line
}
void loop()
{
char key = kpd.getKey(); //read ley press
if(key)
{
inputArray[i] = key; //record key press in input array
tempo=0; // zero inactive time variable
i++; // increase key pressed variable
lcd.print(key); //print key pressed to LCD
if (i == 4) //If 4 keys have been pressed
{
if (inputArray[0] == Main[0] && //compare input password to door 1 password
inputArray[1] == Main[1] && //compare input password to door 1 password
inputArray[2] == Main[2] && //compare input password to door 1 password
inputArray[3] == Main[3]) //compare input password to door 1 password
{
digitalWrite(registerPin, HIGH); //Activate door 1 relay
delay(500); // wait half a second
digitalWrite(registerPin, LOW); //deactivate relay
lcd.clear(); //clears lcd
lcd.print("Correct Password"); // write message
delay(1500); // Wat 1.5 sec
memset(inputArray,'\0',4); //clears the input array
lcd.clear(); //clear lcd
lcd.print("Enter password"); //write message
lcd.setCursor(0, 1); //put cursor on second line
}
else
{
if (inputArray[0] == Guest[0] && //compare input password to door 2 password
inputArray[1] == Guest[1] && //compare input password to door 2 password
inputArray[2] == Guest[2] && //compare input password to door 2 password
inputArray[3] == Guest[3]) //compare input password to door 2 password
{
digitalWrite(secondPin, HIGH); //Activate door 2 relay
delay(500); // wait half a second
digitalWrite(secondPin, LOW); //deactivate relay
lcd.clear(); //clears lcd
lcd.print("Correct Password"); // write message
delay(1500); // Wat 1.5 sec
memset(inputArray,'\0',4); //clears the input array
lcd.clear(); //clear lcd
lcd.print("Enter password"); //write message
lcd.setCursor(0, 1); //put cursor on second line
}
else // in case of wrong password
{
memset(inputArray,'\0',4); //clears the input array
delay(1000); //wait 1 sec
lcd.clear(); //clear lcd
lcd.print("Wrong Password"); // write message
delay(1500); // Wat 1.5 sec
lcd.clear(); //clear lcd
lcd.print("Enter password"); //write message
lcd.setCursor(0, 1); //put cursor on second line
}
}
i=0; //Zero number of keys pressed
}
}
tempo++; //increase inactive time varieable
if (tempo > 4500000) //if inactive time variable reaches near 45sec
{
i=0; //zero key presses
memset(inputArray,'\0',4); //clears the input array
lcd.clear(); //clear lcd
lcd.print("Enter password:"); //write message
lcd.setCursor(0, 1); //put cursor on second line
tempo=0; // zero inactive time variable
}
};