/* Keypad for password test
by miliohm.com */
/*
#include <Keypad.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // set the LCD address to 0x27 for a 16 chars and 2 line display
String pad;
const byte numRows = 4;
const byte numCols = 4;
String password = "4321";
char keypressed;
char keymap[numRows][numCols] =
{
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
//------------------------------------------------------------
byte rowPins[numRows] = {A15, A14, A13, A12};
byte colPins[numCols] = {A11, A10, A9, A8};
Keypad myKeypad = Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols); //mapping keypad
const byte LED1 = 11;
const byte IN22 = 22;
boolean readValue22;
void setup() {
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Keypad");
lcd.setCursor(0, 1);
lcd.print("Test");
delay(400);
lcd.clear();
pinMode(LED1, OUTPUT);
digitalWrite(LED1, LOW);
pinMode(22, INPUT_PULLUP);
}
void loop() {
// put your main code here, to run repeatedly:
readKeypad();
if (keypressed == '#')
{
if (pad == password)
{
lcd.setCursor(0, 1);
lcd.print("Access Granted");
delay(1000);
door();
//char key = myKeypad.getKey();
lcd.clear();
while(1);
}
else
{
lcd.setCursor(0, 1);
lcd.print("Access Denied");
}
}
if (keypressed == '*')
{
pad = "";
lcd.clear();
}
lcd.setCursor(0, 0);
lcd.print(pad);
delay(100);
if (keypressed == '#')
{
if(password=='4321')
{
if(!digitalRead(22))
{
digitalWrite(LED1,HIGH);
}
}
}
}
void readKeypad() {
keypressed = myKeypad.getKey(); //deteksi penekanan keypad
if (keypressed != '#') {
String konv = String(keypressed);
pad += konv;
}
}
void door()
{
char key = myKeypad.getKey();
if(key)
{
}
}
*/
/*
#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
#include <TM1637Display.h>
#define CLK 3
#define DIO 2
TM1637Display display = TM1637Display(CLK, DIO);
const int ROW_NUM = 4; //four rows
const int COLUMN_NUM = 4; //four columns
char keys[ROW_NUM][COLUMN_NUM] = {
{'1','2','3', 'A'},
{'4','5','6', 'B'},
{'7','8','9', 'C'},
{'*','0','#', 'D'}
};
byte pin_rows[ROW_NUM] = {A15, A14, A13, A12}; //connect to the row pinouts of the keypad
byte pin_column[COLUMN_NUM] = {A11, A10, A9, A8}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );
const String password_1 = "1234";
String input_password;
const byte LED1 = 11;
const byte GRND = 13;
const byte IN22 = 22;
boolean readValue22;
void setup()
{
lcd.init();
lcd.backlight();
Serial.begin(9600);
Serial.println("Begin..");
pinMode(LED1, OUTPUT);
digitalWrite(LED1, LOW);
pinMode(GRND, OUTPUT);
digitalWrite(GRND, HIGH);
pinMode(22, INPUT_PULLUP);
}
void loop()
{
display.setBrightness(3);
char key = keypad.getKey();
if (key)
{
if(key == '*')
{
input_password = "";
lcd.clear(); // YE LINE KO DISABLE KIYA GAYA HAI
}
else if (key == 'B') // FOR BACKSPACE
{
if (input_password.length() > 0)
{
lcd.setCursor(input_password.length(), 1); // move cursor to the last added character
lcd.print(' '); // Replace it with a blank
input_password.remove(input_password.length() - 1);
}
}
else if(key == '#') // FOR ENTER BUTTON AFTER SELECTING PASSWORD
{
lcd.setCursor(0,1); // YE LINE KO INTRODUCE KIYA GAYA HAI
lcd.print(" ");
if (input_password == password_1) // 001 PATCH SELECTOR
{
//DO SOMETHING HERE LIKE LED ON/OFF
lcd.print("ALL GOOD");
delay(1000);
lcd.clear();
//if(!digitalRead(22))
//{
digitalWrite(GRND,LOW);
//}
while(1);
}
else
{
lcd.setCursor(1, 1);
lcd.print("ACCESS DENIED!");
delay(400);
lcd.clear(); // YE LINE KO DISABLE KIYA GAYA HAI
}
input_password = ""; // reset the input password
} // else if of '#' ends here !!!!!!!
else // after else if of all the pass 'keys' & Backspace key
{
if(input_password.length() < 4)
{
input_password += key; // append new character to input password string
lcd.setCursor(input_password.length(), 1); // move cursor to new position
lcd.print(key); // print key showing the character
//lcd.print('*');
}
//if (input_password)
//{
//}
} // ELSE AFTER PASS KEYS ENDS HERE
} // END OF if(key)
if(!digitalRead(22))
{
digitalWrite(LED1,HIGH);
}
} //END OF void loop()
*/
//*
#include<Keypad.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
#include<EEPROM.h>
char password[4];
//String password;
String keylimit;
char pass[4],pass1[4];
int i=0;
char customKey=0;
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char hexaKeys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {A15, A14, A13, A12}; //row pinouts of the keypad
byte colPins[COLS] = {A11, A10, A9, A8}; //column pinouts of the keypad
//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
int trigreenLed = 7;
int blueLed = 9;
int redLed = 10;
int greenLed = 11;
int triblueLed = 13;
int OrangeLed = 47;
int PurpleLed = 48;
const byte IN22 = 22;
const byte IN23 = 23;
boolean readValue22;
boolean readValue23;
bool running = false; //added externally
byte data_count = 0; //added externally
#define Password_Length 8
char Data[Password_Length];
void setup()
{
lcd.init();
lcd.backlight();
pinMode(22, INPUT_PULLUP);
pinMode(23, INPUT_PULLUP);
pinMode(blueLed, OUTPUT);
pinMode(redLed, OUTPUT);
pinMode(greenLed, OUTPUT);
pinMode(triblueLed, OUTPUT);
digitalWrite(triblueLed, HIGH);
pinMode(trigreenLed, OUTPUT);
digitalWrite(trigreenLed, HIGH);
pinMode(OrangeLed, OUTPUT);
digitalWrite(OrangeLed, LOW);
pinMode(PurpleLed, OUTPUT);
digitalWrite(PurpleLed, LOW);
lcd.print(" WELCOME... ");
lcd.setCursor(0,1);
lcd.print(" Keypad Lock ");
delay(1000);
lcd.clear();
lcd.print("Enter password:");
lcd.setCursor(0,1);
for(int j=0;j<4;j++)
EEPROM.write(j, j+49);
for(int j=0;j<4;j++)
pass[j]=EEPROM.read(j);
}
void change()
{
int j=0;
lcd.clear();
lcd.print("Enter Current Key");
lcd.setCursor(0,1);
while(j<4)
{
char key=customKeypad.getKey();
if(key)
{
pass1[j++]=key;
lcd.print(key);
}
key=0;
}
delay(500);
if((strncmp(pass1, pass, 4)))
{
lcd.clear();
lcd.print("Wrong Passkey...");
lcd.setCursor(0,1);
lcd.print("Enter Right Key");
delay(1000);
}
else
{
j=0;
lcd.clear();
lcd.print("Enter New Key:");
lcd.setCursor(0,1);
while(j<4)
{
char key=customKeypad.getKey();
if(key)
{
pass[j]=key;
lcd.print(key);
EEPROM.write(j,key);
j++;
}
}
lcd.print(" Done......");
delay(1000);
}
lcd.clear();
lcd.print("Enter your key:");
lcd.setCursor(0,1);
customKey=0;
}
void reset()
{
digitalWrite(triblueLed, HIGH);
running=false;
/*
char key=customKeypad.getKey();
if(key)
{
int j=0;
pass[j]=key;
lcd.print(key);
EEPROM.write(j,key);
j++;
}
*/
lcd.clear();
delay(100);
lcd.print("Enter password:");
lcd.setCursor(0,1);
for(int j=0;j<4;j++)
EEPROM.write(j, j+49);
for(int j=0;j<4;j++)
pass[j]=EEPROM.read(j);
lcd.setCursor(0,1);
customKey=0;
}
void loop()
{
//digitalWrite(11, HIGH);
customKey = customKeypad.getKey();
if(customKey=='#')
{
change();
if(customKey=="*")
{
customKey="";
}
}
if(customKey=='*')
{
reset();
//if(customKey=="*")
//{
//customKey="";
//}
}
if (customKey)
{
password[i++]=customKey;
customKey!='*';
lcd.print(customKey);
}
if (customKey == 'B')
{
//if (password > 0)
//{
//customKey=customKey-1;///10;
//data_count = Password_Length - 1;
//data_count = password[i--];
password[i--];// = customKey;
lcd.setCursor(0,1);
lcd.print(" "); // Replace it with UnderScore
lcd.setCursor(0,1);
customKey=0;
//}
}
else if(i==4)
{
delay(200);
for(int j=0;j<4;j++)
pass[j]=EEPROM.read(j);
if(!(strncmp(password, pass,4)))
{
customKey!='*';
digitalWrite(greenLed, HIGH);
lcd.clear();
lcd.print("Passkey Accepted");
delay(500);
lcd.clear();
/*
lcd.setCursor(0,1);
lcd.print("PRESS # TO CHANGE PASSWORD.");
for(int PositionCount=0;PositionCount<80; PositionCount++)//loop for scrolling the LCD text
{
lcd.scrollDisplayLeft();//builtin command to scroll left the text
delay(150);// delay of 150 msec
}
delay(3000);
lcd.clear();
*/
//lcd.print("Enter Passkey:");
//lcd.setCursor(0,1);
lcd.setCursor(0,0);
lcd.print("Welcome to this:");
lcd.setCursor(1,1);
lcd.print("Epic World of");
delay(1000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("* I B M P 444 *");
i=0;
running=true;
delay(1);
//if(running)
//{
digitalWrite(triblueLed, LOW);
//delay(5000);
//digitalWrite(triblueLed, HIGH);
//}
//if(digitalRead(22)==LOW || digitalRead(23)==LOW)
//{
//digitalWrite(triblueLed, LOW);
//delay(5000);
//digitalWrite(triblueLed, HIGH);
//}
//}
//while(1);
}
else //if(i=4)
{
digitalWrite(redLed, HIGH);
lcd.clear();
lcd.print("Access Denied...");
lcd.setCursor(0,1);
lcd.print("#.Change Passkey");
delay(1000);
lcd.clear();
lcd.print("Enter right key:");
lcd.setCursor(0,1);
i=0;
digitalWrite(redLed, LOW);
}
}
//*
//if(!(strncmp(password, pass,4)))
//{
//if(digitalRead(22)==LOW)// = !running;
//{
//digitalWrite(trigreenLed, LOW);
//}
// else
// {
// digitalWrite(trigreenLed, HIGH);
//}
//delay(10000);
/*
if(digitalRead(23)==LOW)
//{
//for(int o=0; o<5; o++)
{
digitalWrite(OrangeLed, HIGH);
delay(400);
}
//delay(1000);
//digitalWrite(OrangeLed, LOW);
//delay(400);
else
{
digitalWrite(OrangeLed, LOW);
}
*/
if(running)
{
if(digitalRead(22)==LOW)
{
digitalWrite(PurpleLed, HIGH);
}
else
{
digitalWrite(PurpleLed, LOW);
}
}
if(digitalRead(23)==LOW)
{
//digitalWrite(OrangeLed, HIGH);
//running=false;
}
//else
//{
//digitalWrite(OrangeLed, LOW);
//}
//else
//{
//digitalWrite(triblueLed, HIGH);
//delay(5000);
//}
//}
//*/
//}
} // END OF void loop()
//*/
//*/
/*
#include <Keypad.h>
#include <EEPROM.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display
#include <Servo.h>
Servo myservo;
#define LEDindicator A3 //Door Open/close LED indicator
#define BypassSwitch A2 //Push Button for emergency bypass
int pos = 0; // variable to store the servo position
const byte numRows= 4; //number of rows on the keypad
const byte numCols= 4; //number of columns on the keypad
char keymap[numRows][numCols]=
{
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
char keypressed;
char code[]= {'1','2','3','4'}; //The default password
char code_buff1[sizeof(code)]; //Where the new password is stored
char code_buff2[sizeof(code)]; //Where the new password is stored again
short a=0,i=0,s=0,j=0; //Variables
byte rowPins[numRows] = {A15, A14, A13, A12}; //Rows 0 to 3
byte colPins[numCols]= {A11, A10, A9, A8}; //Columns 0 to 3
Keypad myKeypad= Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);
void setup()
{
Serial.begin(9600);
lcd.init(); // initialize the lcd
// Print a message to the LCD.
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("ACCESS PROTECTED");
lcd.setCursor(0,1);
lcd.print("ENTER *(PW)0 ");
myservo.attach(7); // attaches the servo on pin 9 to the servo object
pinMode(LEDindicator,OUTPUT);
pinMode(BypassSwitch,INPUT);
for(i=0 ; i<sizeof(code);i++){ // uncomment
EEPROM.get(i, code[i]); // uncomment
}
}
void loop()
{
keypressed = myKeypad.getKey(); //4x4 key to be pressed
if(keypressed == '*'){ // * to open the lock
lcd.clear();
lcd.setCursor(0,0);
lcd.print("ENTER (PW)0 ");
GetCode(); //Getting code
if(a==sizeof(code)) //The size of the code array
OpenDoor();
else{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("WRONG PASSWORD ");
lcd.setCursor(0,1);
lcd.print("TRY AGAIN ");
}
delay(2000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("ACCESS PROTECTED");
lcd.setCursor(0,1);
lcd.print("ENTER *(PW)0 ");
}
if(keypressed == '#'){ //To change the code it calls the Password Change function
PWChange();
// lcd.clear();
// lcd.print("ENTER PASSWORD ");
}
if(digitalRead(BypassSwitch)==HIGH){ //Opening door
digitalWrite(LEDindicator,HIGH);
lcd.setCursor(0,0);
lcd.print("BYPASS ACTIVATED");
lcd.setCursor(0,1);
lcd.print("DOOR OPENING....");
for (pos = 90; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15 ms for the servo to reach the position
}
delay(3000);
for (pos = 180; pos >= 90; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15 ms for the servo to reach the position
}
// delay(3000); //Opens for 3s you can change
digitalWrite(LEDindicator,LOW);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("ACCESS PROTECTED");
lcd.setCursor(0,1);
lcd.print("ENTER *(PW)0 ");
}
}
void GetCode(){ //Getting code sequence
i=0; //All variables set to 0
a=0;
j=0;
while(keypressed != '0'){ //The user press 0 to confirm the code otherwise he can keep typing
keypressed = myKeypad.getKey();
if(keypressed != NO_KEY && keypressed != '0' ){ //If the char typed isn't 0 and neither "nothing"
lcd.setCursor(j,1); //This to write "*" on the LCD whenever a key is pressed it's position is controlled by j
lcd.print("*");
j++;
if(keypressed == code[i]&& i<sizeof(code)){ //if the char typed is correct a and i increments to verify the next caracter
a++; //Now I think maybe I should have use only a or i ... too lazy to test it -_-'
i++;
}
else
a--; //if the character typed is wrong a decrements and cannot equal the size of code []
}
}
keypressed = NO_KEY;
}
void PWChange(){ //Change code sequence
lcd.clear();
lcd.print("PASSWORD CHANGE ");
delay(1000);
lcd.clear();
lcd.print("ENTER (old PW)0 ");
GetCode(); //verify the old code first so you can change it
if(a==sizeof(code)){ //again verifying the a value
lcd.clear();
lcd.print("PASSWORD CHANGE ");
PWChangeCode(); //Get the new code
PWChangeconfirm(); //Get the new code again to confirm it
s=0;
for(i=0 ; i<sizeof(code) ; i++){ //Compare codes in array 1 and array 2 from two previous functions
if(code_buff1[i]==code_buff2[i])
s++; //again this how we verifiy, increment s whenever codes are matching
}
if(s==sizeof(code)){ //Correct is always the size of the array
for(i=0 ; i<sizeof(code) ; i++){
code[i]=code_buff2[i]; //the code array now receives the new code
EEPROM.put(i, code[i]); //And stores it in the EEPROM
//Serial.print(EEPROM.get(i, code[i])); //password
}
lcd.clear();
lcd.print("PASSWORD CHANGED");
delay(2000);
}
else{ //In case the new codes aren't matching
lcd.clear();
lcd.print("NEW PASSWORD ARE");
lcd.setCursor(0,1);
lcd.print("NOT MATCHING !! ");
delay(2000);
lcd.setCursor(0,0);
lcd.print("WRONG PASSWORD ");
lcd.setCursor(0,1);
lcd.print("TRY AGAIN ");
}
}
delay(2000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("ACCESS PROTECTED");
lcd.setCursor(0,1);
lcd.print("ENTER *(PW)0 ");
}
void PWChangeCode(){
i=0;
j=0;
lcd.clear();
lcd.print("CHANGE TO NEW PW");
lcd.setCursor(0,1);
lcd.print("ENTER (new PW)0 ");
delay(2000);
lcd.clear();
lcd.setCursor(0,1);
lcd.print("ENTER (new PW)0 ");
while(keypressed != '0'){ //0 to confirm and quits the loop
keypressed = myKeypad.getKey();
if(keypressed != NO_KEY && keypressed != '0' ){
lcd.setCursor(j,0);
lcd.print("*"); //On the new code you can show * as I did or change it to keypressed to show the keys
code_buff1[i]=keypressed; //Store caracters in the array
i++;
j++;
}
}
keypressed = NO_KEY;
}
void PWChangeconfirm(){ //This is exactly like the ChangeCode function but this time the code is stored in another array
i=0;
j=0;
lcd.clear();
lcd.print("CONFIRM PASSWORD");
lcd.setCursor(0,1);
lcd.print("ENTER (new PW)0 ");
delay(3000);
lcd.clear();
lcd.setCursor(0,1);
lcd.print("ENTER (new PW)0 ");
while(keypressed != '0'){
keypressed = myKeypad.getKey();
if(keypressed != NO_KEY && keypressed != '0' ){
lcd.setCursor(j,0);
lcd.print("*");
code_buff2[i]=keypressed;
i++;
j++;
}
}
keypressed = NO_KEY;
}
void OpenDoor(){ //Door opening function open for 3s
lcd.clear();
lcd.setCursor(0,0);
lcd.print("ACCESS GRANTED ");
lcd.setCursor(0,1);
lcd.print("DOOR OPENING....");
digitalWrite(LEDindicator,HIGH);
for (pos = 90; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15 ms for the servo to reach the position
}
delay(3000);
for (pos = 180; pos >= 90; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15 ms for the servo to reach the position
}
digitalWrite(LEDindicator,LOW);
}
*/