Let me go ahead to post the full code but I thought I'd leave that out so the context of my issue would be gotten right.
For the waitForKey() function you mentioned, would it not mean that I can't use the getKey in the same loop at the same time?
I want to be able to short press key D for one action and long press same key for a different action.
But, here's the full code.
#include <LiquidCrystal.h>
#include <Keypad.h>
#include <EEPROM.h>
int buzzer = 13;
int relay = 12;
int stats = 1; // this is to monitor the status of the system so that the text message containing password can only act when the system is off
int xpx = 0;//this is used to ensure that the system will not lock if unmatching codes are set while setting codes.
int count;//this is to restrict the maximum number of trials of pin to three(3)
int xppl = 0;
//long timer = 0;//not yet implemented but this attempts to keep track of how long the system has been down
String inputString;
long inputInt;
// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = A0, en = A1, d4 = A2, d5 = A3, d6 = 1, d7 = 0;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
const byte numRows = 4; //number of rows on the keypad
const byte numCols = 4; //number of columns on the keypad
//The keymap for the keypad
char keymap[numRows][numCols] =
{
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
char keypressed;
char waitKey;
char code[4];//change to 5 to compensate for a null terminator if char array has, not too sure at this point
//char code[] = {'1', '2', '1', '2'}; //The default code, you can change it or make it a 'n' digits one
char master[4]; //= {'1', '1', '1', '1'}; //This is the master pin to open the system if the set pin is forgotten
char mastercompare[sizeof(master)];
char master_buff1[sizeof(master)]; //Where the new key is stored
char master_buff2[sizeof(master)]; //Where the new key is stored again so it's compared to the previous one
char code_buff1[sizeof(code)]; //Where the new key is stored
char code_buff2[sizeof(code)]; //Where the new key is stored again so it's compared to the previous one
short a = 0, i = 0, s = 0, j = 0, v = 0, q = 0; //Variables used later
byte rowPins[numRows] = {9, 8, 7, 6}; //Rows 0 to 3 //if you modify your pins you should modify this too
byte colPins[numCols] = {5, 4, 3, 2}; //Columns 0 to 3
Keypad myKeypad = Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);
char holdkey;
unsigned long t_hold;
void setup() {
pinMode(13, OUTPUT);//set buzzer to output
pinMode(12, OUTPUT);//set relay to output
lcd.begin(20, 4);
//EEPROM retrieval of the stored master pin
EEPROM.get(30, master[0]);
EEPROM.get(31, master[1]);
EEPROM.get(32, master[2]);
EEPROM.get(33, master[3]);
lcd.home();
lcd.print("System Startup..."); //What's written on the LCD you can change
delay(1000);
}
void loop() {
//keypressed = myKeypad.getKey(); //Constantly waiting for a key to be pressed
waitKey = myKeypad.waitForKey();
if (myKeypad.getState() == HOLD) {
if(keypressed == 'D'){
if ((millis() - t_hold) > 500) {
lcd.clear();
lcd.print("Change Master Pin?");
while (1) {
}
}
}
if (keypressed == 'B') {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Change Master Pin?");
delay(1500);
lcd.clear();
lcd.print("Enter Old Pin:");
lcd.setCursor(0, 2);
lcd.print("Press A to verify");
changemaster();
}
}
}
void changemaster() {
v = 0;
q = 0;
while (keypressed != 'A') { //A to confirm and quits the loop
keypressed = myKeypad.getKey();
if (keypressed != NO_KEY && keypressed != 'A') {
lcd.setCursor(q, 1);
lcd.print(keypressed); //shows the old keys
//buzzonce();
mastercompare[v] = keypressed; //Store caracters in the array 'mastercompare'. we'll use this to cpmpare against the existing master before a change can be effected
v++;
q++;
}
}
s = 0;
for (i = 0; i < sizeof(master); i++) {
if (mastercompare[i] == master[i])
s++; //if s is then incremented up till 4, we know that each character was correct and it can proceed
}
if (s == sizeof(master)) {
lcd.clear();
lcd.print("Old Pin Confirmed");
s = 0;
delay(1200);
keypressed = NO_KEY;
GetNewMaster1(); //Get the new master pin
GetNewMaster2(); //Get the new master pin again to confirm it
s = 0;
for (i = 0 ; i < sizeof(master) ; i++) { //Compare codes in array 1 and array 2 from two previous functions
if (master_buff1[i] == master_buff2[i])
s++; //again this how we verifiy, increment s whenever codes are matching
}
if (s == sizeof(master)) { //Correct is always the size of the array
j = 0;
for (i = 0 ; i < sizeof(master) ; i++) {
j = i + 30;
master[i] = master_buff2[i]; //the master array now receives the new code
EEPROM.put(j, master[i]); //And stores it in the EEPROM
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Master Pin Changed");
lcd.setCursor(5, 1);
lcd.print("successfully...");
//buzzonce();
xpx = 0;
delay(2000);
}
else { //In case the new codes aren't matching
lcd.clear();
lcd.print("Numbers are not");
lcd.setCursor(9, 1);
lcd.print("matching!!");
//buzzthrice();
xpx = 1;
delay(2000);
}
}
else {
lcd.clear();
lcd.print("Not accepted");
s = 0;
}
}
void GetNewMaster1() {
i = 0;
j = 0;
lcd.clear();
lcd.print("Enter new Master pin"); //tell the user to enter the new code and press A
lcd.setCursor(0, 1);
lcd.print("Press A to confirm");
delay(1800);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter new pin: ");
lcd.setCursor(0, 2);
lcd.print("Press A to confirm"); //Press A keep showing while the top row print ***
while (keypressed != 'A') { //A to confirm and quits the loop
keypressed = myKeypad.getKey();
if (keypressed != NO_KEY && keypressed != 'A' ) {
lcd.setCursor(j, 1);
lcd.print("*"); //On the new code you can show * as I did or change it to keypressed to show the keys
//buzzonce();
master_buff1[i] = keypressed; //Store caracters in the array
i++;
j++;
}
}
keypressed = NO_KEY;
}
void GetNewMaster2() { //This is exactly like the GetNewCode1 function but this time the code is stored in another array
i = 0;
j = 0;
lcd.clear();
lcd.print("Confirm your pin");
lcd.setCursor(7, 1);
lcd.print("and press A");
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter pin: ");
lcd.setCursor(0, 2);
lcd.print("Press A to confirm");
while (keypressed != 'A') {
keypressed = myKeypad.getKey();
if (keypressed != NO_KEY && keypressed != 'A' ) {
lcd.setCursor(j, 1);
lcd.print("*");
//buzzonce();
master_buff2[i] = keypressed;
i++;
j++;
}
}
keypressed = NO_KEY;
}
/*if (myKeypad.getState() == HOLD) {
if(keypressed == 'D') {
if ((millis() - t_hold) > 500) {
lcd.clear();
lcd.print("Change Master Pin?");
while (1) {
}
}
}
}*/