Hi!
I made a program where when a specific button from keypad is pressed it will generate a password;
I have no problem with generating and comparing with the password but when a certain condition is met
even if the password requires four(4) integer and when I put only 2 or 3 it my system will accept the password.
This is the condition
if my generated password is “1234”
and I input a password like “1434”
when my system asks to input another but when I put “12”
the system accept even tho it requires four(4) int.
another condition is this
password = “1234”
input = “2234”
the system will accept the password if I input “1”
It seems that the last two digit from previous password is read by the program;
I’ve been trying to find where is the error but I just can’t locate it
here’s my code
#include <Keypad.h>
#include<EEPROM.h>
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
int led = 13;
char customkey = 0;
char password[5];
int i=0;
int x = 0;
int data;
char hexaKeys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {8, 7, 6, 5}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {9, 10, 11, 12}; //connect to the column pinouts of the keypad
//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
void setup() {
Serial.begin(9600);
pinMode(led,OUTPUT);
randomSeed(analogRead(A0));
Serial.println("WELCOME TO PPCS");
}
void loop()
{
customkey = customKeypad.getKey();
if(customkey)
{
password[i++]=customkey;
int theint = 0;
theint = (10* theint + password[1] - '0');
theint = (10* theint + password[2] - '0');
theint = (10* theint + password[3] - '0');
theint = (10* theint + password[4] - '0');// convert char from keypad to int
if(password[0] != '#')
{
i=0;
}
if (customkey == '#')
{i=i++;
Serial.print(i);
generate();
digitalWrite(led,HIGH);
}
Serial.println(customkey);
if(i==5 && theint != data) //print if wrong password
{i=1;
delay(500);
Serial.print(theint);
Serial.println("WRONG PASSWORD");
}
else
if( theint==data) //print if condition is met
{
digitalWrite(led,HIGH);
Serial.print(theint);
Serial.print("PASSWORD MATCHED");
delay(400);
digitalWrite(led,LOW);
i=0;
loop();}}}
void generate() //generatecode
{
x = random(1111,9999);
EEPROM.put(3,x);
Serial.println("YOUR PASSWORD");
Serial.println(EEPROM.get(3,data));
}
P.S I attached an image to show what’s going on in the serial monitor
Any help is greatly appreciated THANK YOU!!