Anyone have code for entering multiple passwords with an ir remote to open a lock

I want to have a list of 20 or more preset passwords that would work to open a lock with an ir remote.

I have code to compare an entered password by ir remote to a single preset password, but I need multiple preset passwords that would work.

This is essentially what I want to do in the picture, but I want to remove the keypad altogether because I do not want it to set a password or to enter the password. I want to have preset passwords (like "1234567") I can set by modifying the code itself. I want to be able to enter the password with the ir remote. Code starts below.

#include <Keypad.h>

#include <LiquidCrystal.h>

#include <IRremote.h>

LiquidCrystal lcd(A5, A4, A3, A2, A1, A0); // set pin to LCD

IRrecv rCode(10); //set pin 10 for remote code
decode_results remoteCode; //var for write code

const byte ROWS = 4; //4 rows of keypad
const byte COLS = 4; //4 columns of keypad
char pressKey;
char Keys[ROWS][COLS] = { //symbol's of keypad
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
// * as open lock, # as set psw and close lock
};
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
Keypad cKeypad = Keypad( makeKeymap(Keys), rowPins, colPins, ROWS, COLS); // init keypad
int enderClose=12; //pin for ender switcher (when lock is closing)
int enderOpen=13; //pin for ender switcher (when lock is opening)
int motor=11; //pin for lock motor (closing/opening)
bool locked=false; // variable for lock satus
String code=""; // variable for entering unlock code
String codeLock=""; // variable for entering lock code

///////////////////////setup sketch//////////////////////////////////////////
void setup()
{
lcd.begin(16, 2); //set size of LCD (16 symb of 2 rows)

rCode.enableIRIn(); //enable IR
rCode.blink13(true); //i don't know this

pinMode(enderClose, INPUT); //pin ender switcher as input
pinMode(enderOpen, INPUT); //pin ender switcher as input
pinMode(motor, OUTPUT); //pin motor as output
digitalWrite(motor, LOW); //beginning satatus of motor
//Serial.begin(9600); //setting foe seral monitor
}
/////////////////// opening lock by motor /subprogramm///////////////////////////
void openLock()
{
digitalWrite(motor, HIGH); //ON motor
while (digitalRead(enderOpen)==HIGH) {}; // wait for ender swicher opening
digitalWrite(motor, LOW); //OFF motor
//delay(100);
}
/////////////////// closing lock by motor /subprogramm/////////////////////////////////
void closelock()
{
digitalWrite(motor, HIGH); //ON motor
while (digitalRead(enderClose)==HIGH) {}; // wait for ender swicher closing
digitalWrite(motor, LOW); //OFF motor
//delay(100);
}

////////////////// read unlock code by keypad /subprogramm////////////////////////
void readKeyPadCode()
{
String _code=""; //local variable for entring temp unlock code
int _i=0; //local variable for count of symbol

while (true) // repeat until force exit
{
pressKey = cKeypad.waitForKey(); //wait press key and write she to variable
/Serial.println("key pressed is- "+pressKey);
delay(30);
/
if ((pressKey=='')&&(_i==0)) //If press "" (enter code) and no one symbol
{
lcd.setCursor(0,1); //begin on second line
lcd.print("PSW is Empty "); //show on LCD password is empty
continue; //jump to begin of "while"
}
if ((pressKey=='')&&(_i!=0)) // if pressed "" (enter code) and have a symbol/simbols
{
code=_code; // assign unlock code from local temp code
/Serial.println("Enter code is- "+code);
delay(30);
/
pressKey=0; // reset value of press key variable
break; //force exit from "while"
}
_code=_code+pressKey; //add to temp unlock code last symbol
/Serial.println("Temp code is- "+_code);
delay(30);
/
lcd.setCursor(_i,1); //move cursor LCD to next position
lcd.print("#"); //show on LCD "#" ("#","##","###"...)
_i++; //increment _i
}
}

This could be removed and replaced with predefined passwords that would allow you to open the lock
///////////////////// setup password by keypad /subprogramm/////////////////////////
void setKeyPadCode()
{
String _ccode=""; //local variable for entring temp unlock code
int _j=0; //local variable for count of symbol

while (true) // repeat until force exit
{
pressKey = cKeypad.waitForKey(); //wait press key and write she to variable
if ((pressKey=='#')&&(_j==0)) //If press "#" (enter password) and no one symbol
{
lcd.setCursor(0,1); //begin on second line
lcd.print("PSW is Empty "); //show on LCD password is empty
continue; //jump to begin of "while"
}
if ((pressKey=='#')&&(_j!=0)) // if pressed "#" (enter password) and have a symbol/simbol
{
codeLock=_ccode; // assign lock code from local temp code
pressKey=0; // reset value of press key variable
break; //force exit from "while"
}
if (pressKey=='') continue ; //ignore symbol '' by entring code
lcd.setCursor(_j,1); //move cursor LCD to next position
lcd.print("#"); //show on LCD "#" ("#","##","###"...)
_ccode=_ccode+pressKey; //add to temp unlock code last symbol
/Serial.println("key pressed is- "+pressKey);
delay(30);
Serial.println("temp code is- "+_ccode);
delay(30);
/
_j++; //increment _j
}
/Serial.println("PSW setup is- "+codeLock);
delay(30);
/
locked=true; //status of lock
lcd.setCursor(0,1); //setup cursor to beginning first line
lcd.print("PSW is set "); //show on LCD password is set
}
//////////////////////// read unlock code by IR-reciver /subprogramm////////////////////
void readIRRCode()
{
String _str=""; //local variable for entring temp unlock code
int _i=0; //local variable for count of symbol
char _key=0; //local variable for symbol of IR decode
bool _isNum=false; //local variable for number status of code
unsigned long _code_value = 0; //local variable for IR decode

while (true) // repeat until force exit
{
if (rCode.decode(&remoteCode)) // do it if recive IR code
{
_code_value=remoteCode.value; //decode IR code
if ((_code_value==0xFD708F)&&(_i==0)) //If pressed "ST/REPT" (enter password) and no one symbol
{
lcd.setCursor(0,1); //setup cursor to beginning second line
lcd.print("PSW is Empty "); //show on LCD password is empty
rCode.resume(); //need for resume to recive IR code
continue; //jump to begin of "while"
}
if ((_code_value==0xFD708F)&&(_i!=0)&&(_key!=0))
{ // if pressed "ST/REPT" (enter password) and have a symbol/simbol
code=_str; // assign unlock code from local temp code
/Serial.println("recive IR ST/Rept");
delay(30);
Serial.println("recive code is- "+code);
delay(30);
/
rCode.resume(); //need for resume to recive IR code
break; //force exit from "while"
}
_key=0; // reset value of key code variable
switch (_code_value) //select of decode value of recive code
{
case 0xFD30CF: //if 0
_key='0'; // assign IR decode and zero
_isNum=true; // enter code is number
break ;
case 0xFD08F7: //1 (see here and next as 0)
_key='1';
_isNum=true;
break ;
case 0xFD8877: //2
_key='2';
_isNum=true;
break ;
case 0xFD48B7: //3
_key='3';
_isNum=true;
break ;
case 0xFD28D7: //4
_key='4';
_isNum=true;
break ;
case 0xFDA857: //5
_key='5';
_isNum=true;
break ;
case 0xFD6897: //6
_key='6';
_isNum=true;
break ;
case 0xFD18E7: //7
_key='7';
_isNum=true;
break ;
case 0xFD9867: //8
_key='8';
_isNum=true;
break ;
case 0xFD58A7: //9
_key='9';
_isNum=true;
break ;
default: //if not a number
_isNum=false; //variable status as no number
break;
}
if (_isNum==true) //do it if recive number
{
_str=_str+_key; //assign temp code and recive symbol
lcd.setCursor(_i,1); //move cursor LCD to next position
lcd.print("#"); //show on LCD "#" ("#","##","###"...)
/Serial.println("recive IR is- "+_key);
delay(30);
Serial.println("temp code is- "+_str);
delay(30);
/
_i++; //increment symbol count
}
rCode.resume(); //need for resume to recive IR code
} //end of first "if"
} //end of "while"
} //end of "void"
////////////////////// main programm body//////////////////////////
void loop()
{
if (locked==false) // do it if lock status is unlock ("open")
{
lcd.setCursor(0,0); //setup cursor to beginning first line
lcd.print("Lock is unlocked"); //show on LCD "Lock is unlocked"
lcd.setCursor(0,1); //setup cursor to beginning second line
lcd.print("Input Code & # "); //show on LCD "Input Code & #"
setKeyPadCode(); //if lock unlocked need set password. Calling subprogramm
closelock(); //lock after enter password. Calling subprogramm
lcd.setCursor(0,0); //setup cursor to beginning first line
lcd.print("Lock is locked "); //show on LCD "Lock is locked"
locked =true; // set lock status as "loked"
}
if (locked==true) //do it if lock status is lock ("close")
{
if (cKeypad.getKey()!=NO_KEY) //do it if enter code on keypad
{
readKeyPadCode(); //if lock is locked need enter code. Calling subprogram
if (code==codeLock) //do it if entring code equal with password
{
openLock(); //unlocking lock. calling subprogramm
lcd.setCursor(0,0); //setup cursor to beginning first line
lcd.print("Lock is unlocked"); //show on LCD "Lock is unlocked"
locked=false; // set lock status as "unloked"
codeLock=""; // reset value of password
} else //do it if entring code not equal with password
{
lcd.setCursor(0,1); //setup cursor to beginning second line
lcd.print("PSW is invalid "); //show on LCD "password is invalid"
}
}

if (rCode.decode(&remoteCode)) //do it if enetr code on IR-remote control
{
readIRRCode(); //if lock is locked need enter code. Calling subprogramm
if (code==codeLock) //do it if entring code equal with password
{
openLock(); //unlocking lock. calling subprogramm
lcd.setCursor(0,0); //setup cursor to beginning first line
lcd.print("Lock is unlocked"); //show on LCD "Lock is unlocked"
locked=false; // set lock status as "unloked"
codeLock=""; // reset value of password
} else
{
lcd.setCursor(0,1); //setup cursor to beginning second line
lcd.print("PSW is invalid "); //show on LCD "password is invalid"
}
}
}
}
///////////////////////end sketch ////////////////////////////////

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

When a valid code is received use a for loop to iterate through the array of preloaded passwords looking for an equality.  If a match is found activate the lock opening code.

Thank you for the help with getting this posted correctly.

Here is my code:

#include <Keypad.h>

#include <LiquidCrystal.h>

#include <IRremote.h>

LiquidCrystal lcd(A5, A4, A3, A2, A1, A0); // set pin to LCD

IRrecv rCode(10); //set pin 10 for remote code
decode_results remoteCode; //var for write code

const byte ROWS = 4; //4 rows of keypad
const byte COLS = 4; //4 columns of keypad
char pressKey;
char Keys[ROWS][COLS] = { //symbol's of keypad
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
// * as open lock, # as set psw and close lock
};
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
Keypad cKeypad = Keypad( makeKeymap(Keys), rowPins, colPins, ROWS, COLS); // init keypad
int enderClose=12; //pin for ender switcher (when lock is closing)
int enderOpen=13; //pin for ender switcher (when lock is opening)
int motor=11; //pin for lock motor (closing/opening)
bool locked=false; // variable for lock satus
String code=""; // variable for entering unlock code
String codeLock=""; // variable for entering lock code

///////////////////////setup sketch//////////////////////////////////////////
void setup()
{
lcd.begin(16, 2); //set size of LCD (16 symb of 2 rows)

rCode.enableIRIn(); //enable IR
rCode.blink13(true); //i don't know this

pinMode(enderClose, INPUT); //pin ender switcher as input
pinMode(enderOpen, INPUT); //pin ender switcher as input
pinMode(motor, OUTPUT); //pin motor as output
digitalWrite(motor, LOW); //beginning satatus of motor
//Serial.begin(9600); //setting foe seral monitor
}
/////////////////// opening lock by motor /subprogramm///////////////////////////
void openLock()
{
digitalWrite(motor, HIGH); //ON motor
while (digitalRead(enderOpen)==HIGH) {}; // wait for ender swicher opening
digitalWrite(motor, LOW); //OFF motor
//delay(100);
}
/////////////////// closing lock by motor /subprogramm/////////////////////////////////
void closelock()
{
digitalWrite(motor, HIGH); //ON motor
while (digitalRead(enderClose)==HIGH) {}; // wait for ender swicher closing
digitalWrite(motor, LOW); //OFF motor
//delay(100);
}

////////////////// read unlock code by keypad /subprogramm////////////////////////
void readKeyPadCode()
{
String _code=""; //local variable for entring temp unlock code
int _i=0; //local variable for count of symbol

while (true) // repeat until force exit
{
pressKey = cKeypad.waitForKey(); //wait press key and write she to variable
/Serial.println("key pressed is- "+pressKey);
delay(30);/
if ((pressKey=='')&&(_i==0)) //If press "" (enter code) and no one symbol
{
lcd.setCursor(0,1); //begin on second line
lcd.print("PSW is Empty "); //show on LCD password is empty
continue; //jump to begin of "while"
}
if ((pressKey=='')&&(_i!=0)) // if pressed "" (enter code) and have a symbol/simbols
{
code=_code; // assign unlock code from local temp code
/Serial.println("Enter code is- "+code);
delay(30);/
pressKey=0; // reset value of press key variable
break; //force exit from "while"
}
_code=_code+pressKey; //add to temp unlock code last symbol
/Serial.println("Temp code is- "+_code);
delay(30);/
lcd.setCursor(_i,1); //move cursor LCD to next position
lcd.print("#"); //show on LCD "#" ("#","##","###"...)
_i++; //increment _i
}
}

This could be removed and replaced with predefined passwords that would allow you to open the lock
///////////////////// setup password by keypad /subprogramm/////////////////////////
void setKeyPadCode()
{
String _ccode=""; //local variable for entring temp unlock code
int _j=0; //local variable for count of symbol

while (true) // repeat until force exit
{
pressKey = cKeypad.waitForKey(); //wait press key and write she to variable
if ((pressKey=='#')&&(_j==0)) //If press "#" (enter password) and no one symbol
{
lcd.setCursor(0,1); //begin on second line
lcd.print("PSW is Empty "); //show on LCD password is empty
continue; //jump to begin of "while"
}
if ((pressKey=='#')&&(_j!=0)) // if pressed "#" (enter password) and have a symbol/simbol
{
codeLock=_ccode; // assign lock code from local temp code
pressKey=0; // reset value of press key variable
break; //force exit from "while"
}
if (pressKey=='') continue ; //ignore symbol '' by entring code
lcd.setCursor(_j,1); //move cursor LCD to next position
lcd.print("#"); //show on LCD "#" ("#","##","###"...)
_ccode=_ccode+pressKey; //add to temp unlock code last symbol
/Serial.println("key pressed is- "+pressKey);
delay(30);
Serial.println("temp code is- "+_ccode);
delay(30);/
_j++; //increment _j
}
/Serial.println("PSW setup is- "+codeLock);
delay(30);/
locked=true; //status of lock
lcd.setCursor(0,1); //setup cursor to beginning first line
lcd.print("PSW is set "); //show on LCD password is set
}
//////////////////////// read unlock code by IR-reciver /subprogramm////////////////////
void readIRRCode()
{
String _str=""; //local variable for entring temp unlock code
int _i=0; //local variable for count of symbol
char _key=0; //local variable for symbol of IR decode
bool _isNum=false; //local variable for number status of code
unsigned long _code_value = 0; //local variable for IR decode

while (true) // repeat until force exit
{
if (rCode.decode(&remoteCode)) // do it if recive IR code
{
_code_value=remoteCode.value; //decode IR code
if ((_code_value==0xFD708F)&&(_i==0)) //If pressed "ST/REPT" (enter password) and no one symbol
{
lcd.setCursor(0,1); //setup cursor to beginning second line
lcd.print("PSW is Empty "); //show on LCD password is empty
rCode.resume(); //need for resume to recive IR code
continue; //jump to begin of "while"
}
if ((_code_value==0xFD708F)&&(_i!=0)&&(_key!=0))
{ // if pressed "ST/REPT" (enter password) and have a symbol/simbol
code=_str; // assign unlock code from local temp code
/Serial.println("recive IR ST/Rept");
delay(30);
Serial.println("recive code is- "+code);
delay(30);/
rCode.resume(); //need for resume to recive IR code
break; //force exit from "while"
}
_key=0; // reset value of key code variable
switch (_code_value) //select of decode value of recive code
{
case 0xFD30CF: //if 0
_key='0'; // assign IR decode and zero
_isNum=true; // enter code is number
break ;
case 0xFD08F7: //1 (see here and next as 0)
_key='1';
_isNum=true;
break ;
case 0xFD8877: //2
_key='2';
_isNum=true;
break ;
case 0xFD48B7: //3
_key='3';
_isNum=true;
break ;
case 0xFD28D7: //4
_key='4';
_isNum=true;
break ;
case 0xFDA857: //5
_key='5';
_isNum=true;
break ;
case 0xFD6897: //6
_key='6';
_isNum=true;
break ;
case 0xFD18E7: //7
_key='7';
_isNum=true;
break ;
case 0xFD9867: //8
_key='8';
_isNum=true;
break ;
case 0xFD58A7: //9
_key='9';
_isNum=true;
break ;
default: //if not a number
_isNum=false; //variable status as no number
break;
}
if (_isNum==true) //do it if recive number
{
_str=_str+_key; //assign temp code and recive symbol
lcd.setCursor(_i,1); //move cursor LCD to next position
lcd.print("#"); //show on LCD "#" ("#","##","###"...)
/Serial.println("recive IR is- "+_key);
delay(30);
Serial.println("temp code is- "+_str);
delay(30);/
_i++; //increment symbol count
}
rCode.resume(); //need for resume to recive IR code
} //end of first "if"
} //end of "while"
} //end of "void"
////////////////////// main programm body//////////////////////////
void loop()
{
if (locked==false) // do it if lock status is unlock ("open")
{
lcd.setCursor(0,0); //setup cursor to beginning first line
lcd.print("Lock is unlocked"); //show on LCD "Lock is unlocked"
lcd.setCursor(0,1); //setup cursor to beginning second line
lcd.print("Input Code & # "); //show on LCD "Input Code & #"
setKeyPadCode(); //if lock unlocked need set password. Calling subprogramm
closelock(); //lock after enter password. Calling subprogramm
lcd.setCursor(0,0); //setup cursor to beginning first line
lcd.print("Lock is locked "); //show on LCD "Lock is locked"
locked =true; // set lock status as "loked"
}
if (locked==true) //do it if lock status is lock ("close")
{
if (cKeypad.getKey()!=NO_KEY) //do it if enter code on keypad
{
readKeyPadCode(); //if lock is locked need enter code. Calling subprogram
if (code==codeLock) //do it if entring code equal with password
{
openLock(); //unlocking lock. calling subprogramm
lcd.setCursor(0,0); //setup cursor to beginning first line
lcd.print("Lock is unlocked"); //show on LCD "Lock is unlocked"
locked=false; // set lock status as "unloked"
codeLock=""; // reset value of password
} else //do it if entring code not equal with password
{
lcd.setCursor(0,1); //setup cursor to beginning second line
lcd.print("PSW is invalid "); //show on LCD "password is invalid"
}
}

if (rCode.decode(&remoteCode)) //do it if enetr code on IR-remote control
{
readIRRCode(); //if lock is locked need enter code. Calling subprogramm
if (code==codeLock) //do it if entring code equal with password
{
openLock(); //unlocking lock. calling subprogramm
lcd.setCursor(0,0); //setup cursor to beginning first line
lcd.print("Lock is unlocked"); //show on LCD "Lock is unlocked"
locked=false; // set lock status as "unloked"
codeLock=""; // reset value of password
} else
{
lcd.setCursor(0,1); //setup cursor to beginning second line
lcd.print("PSW is invalid "); //show on LCD "password is invalid"
}
}
}
}
///////////////////////end sketch ////////////////////////////////

Thank you for adding the code tags. They do make it easier to deal with code posted in the forum

You should edit your first posting to have the code in the frst posting as code-section too. Simply click on the pencil-button to activate the EDIT-mode.
Then mark all the code and press Ctrl-X for ciut out
click the </>-Button then press Ctrl-C to insert the code again

best regards Stefan

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.