Lcd Password Problem

Hello!!! I have an arduino mega 2560 and a lcd .I have a password (to unlock something) and I type some characters in serial monitor to find the password and I always get “ACCESS DENIED” message,before type all the 6 characters that the password has.
For example ,the password “53A#10” and then I type in serial monitor the number 5 and I push enter.
Then ,the number 3 and push enter and when I push the letter “A” ,I get “ACCESS DENIED” message in lcd.
Here is the code:

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);


String system_password = "53A#10";
char given_password = "";
int num_of_digits = 0;
boolean password_Is_Not_Correct = true;
String lista[6];
int mistake_in_given_code = 0;
boolean end_code_system = false;

void setup()
{
  Serial.begin(9600);
  lcd.begin();
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.print("UNLOCK SYSTEM:");
  lcd.setCursor(0, 1);
  lcd.print("PASSWORD>  ");
  while (Serial.available() > 0)
  {
    given_password = Serial.read();
    Serial.print(given_password);
  }
}


void loop()
{
  if (!end_code_system)
  {
    while (Serial.available() > 0)
    {
      given_password = Serial.read();
      Serial.print(given_password);

      if (given_password == system_password[num_of_digits])
      {
        lista[num_of_digits] = given_password;
        num_of_digits++;
      }
      if (given_password != system_password[num_of_digits])
      {
        mistake_in_given_code++;
        num_of_digits++;
      }
      if (num_of_digits == 6 && mistake_in_given_code > 0)
      {
        lcd.clear();
        delay(10);
        lcd.setCursor(0, 0);
        lcd.print("ACCESS DENIED");
        lcd.setCursor(0, 1);
        lcd.print("PASSWORD>  ");
        mistake_in_given_code = 0;
        num_of_digits = 0;
      }
      if (num_of_digits == 6 && mistake_in_given_code == 0)
      {
        lcd.clear();
        delay(10);
        lcd.print("ACCESS ACCEPTED");
        end_code_system = true;
      }
    }
  }
}

Thank you…

This is because each time you press enter you send a non-printng character (known as carriage return and normally shown in text as ). You’re looking for six characters and you send ‘5’ ‘’ ‘3’ ‘’ ‘A’ ‘’ which fulfils your six character requirement but does not match your password string. Either enter all the characters at one go or filter out the carriage return characters before counting and trying to match with the stored password.

Ian

I typed all the characters and pressed enter , but the same thing happens.
As about the second one you said ,how can I do it?

Thanks...

Any advice please.
I stack

arduiNICK:
I typed all the characters and pressed enter , but the same thing happens.
As about the second one you said ,how can I do it?

Thanks…

That is because you’ve sent 7 characters (password + ) which is unlikely to match the seventh character in your stored 6 character password.

If you want to filter out carriage returns then do your Serial.read() and only do the subsequent tests if the character does not equal (decimal 13, hex 0x0D).

Ian

My problem is that I can’t enter the complete code and I recieve the message “ACCESS DENIED”…

What changes should I make in my code?(I do not understand what is…)

Thanks again

Any advice please…

I think the problem here may be that you are unaware of the differences between a String object and an ordinary C string which is just an array of characters.

This code:

system_password[num_of_digits]

won’t work because system_password is a String, not a character array, so you can’t access a particular character position like that. You won’t get an error message at compile time or run time, it will just do something wierd and unexpected, which you have seen. To access a character position in a String you must use

system_password.charAt(num_of_digits)

However, instead of that, I would recommend using character array instead of String:

char system_password[] = "53A#10";

arduiNICK:
Any advice please...

My advice would be do not bump. It is against forum rules. If you wait 2 days with no response, then it's ok to bump.

The same thing happens...

arduiNICK:
The same thing happens...

Not a very useful update.

Post your updated code and what you see on serial monitor. Also, each time your code writes to the LCD, write the same text to serial, so we can see that as well.

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);


String system_password = "8924";
char given_password = 0;
int num_of_digits = 0;
char lista[4];
int mistake_in_given_code = 0;

void setup()
{
  Serial.begin(9600);
  lcd.begin();
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.print("UNLOCK SYSTEM:");
  lcd.setCursor(0, 1);
  lcd.print("PASSWORD>  ");
}


void loop()
{
  while (Serial.available() > 0)
  {
    given_password = Serial.read();
    Serial.print(given_password);

    if (given_password == system_password.charAt(num_of_digits))
    {
      lista[num_of_digits] = given_password;
      num_of_digits++;
    }
    if (given_password != system_password.charAt(num_of_digits))
    {
      mistake_in_given_code++;
      num_of_digits++;
    }
    if (num_of_digits == 3 && mistake_in_given_code > 0)
    {
      lcd.clear();
      delay(10);
      lcd.setCursor(2, 0);
      lcd.print("ACCESS DENIED");
      lcd.setCursor(0, 1);
      lcd.print("PASSWORD>  ");
      mistake_in_given_code = 0;
      num_of_digits = 0;
    }
    if (num_of_digits == 3 && mistake_in_given_code == 0)
    {
      lcd.clear();
      delay(10);
      lcd.print("ACCESS ACCEPTED");
    }
  }
}

When I push number “8”(at first time) ,I get “ACCESS DENIED” message .I do not know what happening here

Can you please help with my problem …The same thing happens every time

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);

char system_password[] = "8924";
char given_password = "";
char total_password = "";
int num_of_digits = 0;
int i=0;

void setup()
{
  Serial.begin(9600);
  lcd.begin();
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.print("UNLOCK SYSTEM: ");
  lcd.setCursor(0, 1);
  lcd.print("PASSWORD>  ");
}


void loop()
{
  if (Serial.available() > 0)
  {
    given_password = Serial.read();
    Serial.print(given_password);
    i++;

    if (given_password == system_password[num_of_digits])
    {
      total_password += given_password;
      num_of_digits++;
    }
    if (given_password != system_password[num_of_digits])
    {
      total_password += given_password;
      num_of_digits++;
    }

    if (total_password == system_password && i==4)
    {
      lcd.clear();
      delay(10);
      lcd.print("ACCESS ACCEPTED");
    }

    if (total_password != system_password && i==4)
    {
      lcd.clear();
      delay(10);
      lcd.setCursor(2, 0);
      lcd.print("ACCESS DENIED");
      lcd.setCursor(0, 1);
      lcd.print("PASSWORD>  ");
      num_of_digits = 0;
    }
  }
}

PaulRB:
...and what you see on serial monitor. Also, each time your code writes to the LCD, write the same text to serial, so we can see that as well.

?

total_password += given_password;

That won’t work if you are not using String. With char array, do this:

char total_password[] = "----";
...
total_password[num_of_digits] = given_password;

But that is not causing your problem. Need to see that serial monitor output.

In serial monitor when I push number 5 for example ,it shows me number 5 and when I push one more number ,in lcd it shows me "ACCESS DENIED"...

total_password == system_password

They will not work with char arrays either. Use:

strcmp(total_password, system_password) == 0

No,again.Always the same…
Can you check all the program?(because I can not find any mistake)

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);

char system_password[] = "8924";
char given_password = "";
char total_password = "";
int num_of_digits = 0;
int i=0;

void setup()
{
  Serial.begin(9600);
  lcd.begin();
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.print("UNLOCK SYSTEM: ");
  lcd.setCursor(0, 1);
  lcd.print("PASSWORD>  ");
}


void loop()
{
  if (Serial.available() > 0)
  {
    given_password = Serial.read();
    Serial.print(given_password);
    i++;

    if (given_password == system_password[num_of_digits])
    {
      total_password += given_password;
      num_of_digits++;
    }
    if (given_password != system_password[num_of_digits])
    {
      total_password += given_password;
      num_of_digits++;
    }

    if (strcmp(total_password, system_password) == 0 && i==4)
    {
      lcd.clear();
      delay(10);
      lcd.print("ACCESS ACCEPTED");
    }

    if (strcmp(total_password, system_password) != 0 && i==4)
    {
      lcd.clear();
      delay(10);
      lcd.setCursor(2, 0);
      lcd.print("ACCESS DENIED");
      lcd.setCursor(0, 1);
      lcd.print("PASSWORD>  ");
      num_of_digits = 0;
    }
  }
}

When I push 8 then enter ,then 9 ,I get “ACCESS DENIED”… :frowning:

You haven't made the changes I suggested. Did you not understand them?

I made them as you said.
I had String system_password = “8924” and i made it char system_password = “8924”
Second , I had given_password == system_password to strcmp(total_password, system_password) == 0

I think I did not forget anything…
Please advice me…