#include <Keypad.h>
#include <Servo.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
int position = 0;
char password[5] = "1234";
char input[5];
bool canType = true;
const byte rows = 4;
const byte cols = 4;
char keys[rows][cols] = {
{ '1', '2', '3', 'A' },
{ '4', '5', '6', 'B' },
{ '7', '8', '9', 'C' },
{ '*', '0', '#', 'D' }
};
byte rowPins[rows] = { 9, 8, 7, 6 };
byte colPins[cols] = { 5, 4, 3, 2 };
Keypad customKeypad = Keypad(makeKeymap(keys), rowPins, colPins, rows, cols);
void setup() {
Serial.begin(9600);
lcd.init();
lcd.clear();
lcd.backlight();
lcd.print("ENTER PASSWORD");
}
void loop() {
char key = customKeypad.getKey();
if (key && position < 4 && canType) {
if (key == '#') {
lcd.clear();
lcd.print("ENTER PASSWORD");
position = 0;
} else {
lcd.setCursor(position, 0);
lcd.print(key);
input[position] = key;
position++;
}
}
//Checking
if (position == 4) {
canType = false;
delay(1000);
lcd.clear();
position = 0;
if (strcmp(input, password) == 0) {
lcd.print("CORRECT");
} else {
lcd.print("WRONG");
}
delay(2000);
lcd.clear();
lcd.print("ENTER PASSWORD");
canType = true;
}
}
By using lcd.clear () when you accept the first character
I tried adding typed
variable that gets added by 1 before showing it to display unlike position
variable. So I checked if typed is equal to 1. If it is I cleared lcd, but it cleared the first character as well.
So, you put the code you didn't post in the wrong place.
You already have a variable that tells you if you've accepted a character or not
Why didn't you use that?
i think i commented on your code already in some other post
why not just do a lcd.clear when position == 0 in your code that recognizes a keypress, instead of checking for '#'.
Oh yeah. I cleared lcd after checking if position is equal to 1. Then cleared it and printed the first array element so it actually shows up after clearing.
but I need to clear lcd when i press #
my mistake. so add another case for position == 0
may make sense to have a sub-function the resets the display for a new password that is called in setup(), for '#' and after a password is entered, rather than have duplicate code in 3 places.
that sub-function can also reset position to zero
and why is there a delay preceding processing of the password? there's a delay afterwards
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.