Something Wrong with Generated Password

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!!

you again

gorgonopsis:
It seems that the last two digit from previous password is read by the program;

all previous digits stay in char password[5];
clear it

Juraj:
you again

This is my first actual program, and I've been teaching myself how to code for these past days, and this forum really helped me a lot, I'm really sorry for asking too many questions.

Juraj:
all previous digits stay in char password[5];
clear it

I'll give it a shot later after school is done, thank you for the reply!

alright I managed to get it right by adding additional condition to check if the password is right or wrong

now my only problem is when I press ‘#’ again it generate another password;

Is there a keypad function to disable a specific button if press one time??

here’s the code now

#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&&i==5) //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));
}

Thank you!

Is there a keypad function to disable a specific button if press one time??

No, but your program could ignore it.

create a new global boolean variable and set it to false

read the key
do what you want with the key value if the boolean is false
set a boolean variable to true to stop the program reacting to it

UKHeliBob:
No, but your program could ignore it.

create a new global boolean variable and set it to false

Thank you again for helping me!!
I still don’t understand how to use boolean data type and how it works (I mean i know how it function but I dont know where to put it on my code)

In the end I had to set a default value for my data in EEPROM to reset and somehow it did work;
Now I can loop my system fine THANKSS

here’s the final 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=1234;
int checker=1234;
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 == '#' && checker==data)
{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&&i==5) //print if condition is met
  {
    digitalWrite(led,HIGH);
    Serial.print(theint);
    Serial.print("PASSWORD MATCHED");
    delay(400);
    digitalWrite(led,LOW);
    i=0;
    data=1234;
    loop();}}}
    
   
   
  
 
  
  void generate() //generatecode
  { 
x = random(1111,9999);
EEPROM.put(3,x);
Serial.println("YOUR PASSWORD");
Serial.println(EEPROM.get(3,data));
}

THANK YOU yay!! :slight_smile: