Hello, I am trying to make a code, I have a keypad and an lcd, first you need to give the factory reset key ('123'), if it's matching then set a new key, the problem is that when I try to give the new key (also of 3 digits) , the last '3' counts as the first digit of the new password or sometimes the first and second.
How can I make it so that the last press of 3 will not be taken accounted as the first digit in the new key I want to give.
#include <Servo.h>
#include <Keypad.h>
#include <Wire.h>
#include <LiquidCrystal.h>
Servo servo1;
//rs, en, d4, d5, d6, d7
LiquidCrystal lcd(A0, A1, A2, A3, A4, A5); //pins in the arduino
const int ledOpen = A9;
const int ledClosed = A8;
int pos = 0;
int openDoor = false;
int i = 0;
int j = 0;
long givenKey[3];
int cont = 0;
long factoryPass[3] = {1,2,3};
long newPass[3];
int k = 0;
int key1 = false;
int key2= false;
int key3 = false;
int check = false;
const byte ROWS = 4;
const byte COLS = 4;
char hexaKeys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {13, 12, 11, 10}; //row pins in the arduino
byte colPins[COLS] = {9, 8, 7, 6}; //col pins in the arduino
Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
void setup() {
servo1.attach(5); //attach to pin 5
lcd.begin(20,4); // initialize lcd 20 x 4
Serial.begin(9600);
}
void loop() {
//while (cont == 1){
//factory reset pass = 123
char customKey = customKeypad.getKey(); //keypad key
lcd.setCursor(0,0); // cursor column 0, line 0
lcd.print("Enter Reset Key: ");
if ( i <= 2){ //if there are 3 digits press
if (customKey){
givenKey[i] = customKey - '0'; //converts key press (char) to int
lcd.setCursor(i,1); //writes down password in new column
lcd.print(customKey);
i++;
}
}
delay(100);
if(givenKey[j] == factoryPass[j]){ //if key1 is the same as the factory key1
if(givenKey[j+1] == factoryPass[j+1]){ //if key2 is the same as the factory key2
if(givenKey[j+2] == factoryPass[j+2]){ //if key3 is the same as the factory key3
lcd.setCursor(0,1);
lcd.print("Enter New Key: "); //if the 3 keys matched, give new password
if ( k <= 2){
if (customKey){
newPass[k] = customKey - '0'; //converts key press (char) to int
Serial.println(customKey);
Serial.print(",");
Serial.println(newPass[k]);
lcd.setCursor(k,2);
lcd.print(customKey);
k++;
}
if (k == 2){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Door Locked");
}
}
//}
//else{
//int key3 = false;
//check = true;
}
}
else{
//int key2 = false;
//check = true;
}
}
//else{
//int key1 = false;
//check = true;
//}
}
// if(check == true){
// delay(100);
//if( key1 == false or key2==false or key3 == false){
//lcd.print("Wrong Password");
//}
//}
//}
//}
/*
servo1.write(pos);
int pass = false;
if( pass = true) {
for (pos = 0; pos <= 90; pos += 10) { // servo opens door
servo1.write(pos);
delay(15);
}
}
// Escribimos el número de segundos trascurridos
//lcd.print(millis()/1000);
// lcd.print(" segundos");
*/