Issue with Serial.read()

A few minutes ago, I tried assembling a security access point composing of just an Arduino Uno and an access card reader. Everything went just as planned until I wanted to add a password to ensure that the program was being used by the intended user. I can't get the program to return the Access Granted message and it would just hang after I've entered the password. If anyone could help me debug the code I'd be extremely grateful. :slight_smile:

#include <SPI.h>
#include <MFRC522.h>

#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN);

void setup()
{
Serial.begin(9600);
SPI.begin();
mfrc522.PCD_Init();
Serial.println("Please ID yourself: ");
Serial.println();
}
void loop()
{
if ( ! mfrc522.PICC_IsNewCardPresent())
{
return;
}
if ( ! mfrc522.PICC_ReadCardSerial())
{
return;
}
Serial.println("Card detected ");
Serial.print("Verifying ID ");
Serial.print(".");
delay(2000);
Serial.print(".");
delay(2000);
Serial.print(".");
Serial.print(".");
delay(2000);
Serial.print(".");
delay(2000);
Serial.println(".");
Serial.println("---------------------------------------------------------------------");
Serial.print("UID tag :");
String content= "";
byte letter;
for (byte i = 0; i < mfrc522.uid.size; i++)
{
Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(mfrc522.uid.uidByte[i], HEX);
content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
content.concat(String(mfrc522.uid.uidByte[i], HEX));
}
Serial.println();
content.toUpperCase();
if (content.substring(1) == "UID OF THE CARD")
{
Serial.println("User: Admin");
Serial.print("Please enter your password: ");
if (Serial.available())
{
if (Serial.read() == THE PASSWORD)
{
Serial.println("Access granted");
Serial.println();
}

  `else`
  `{`
    `Serial.println("Password incorrect");`
    `Serial.println();`
  `}`
`}`

}

else {
Serial.println(" Access denied");
Serial.println();
delay(3000);
}
}

Variable names cannot include spaces.

Please remember to use code tags when posting code

You are checking the serial input before the user has even seen the text prompting them to enter the password.

Yes, my apologies

I'm afraid I don't understand, could you perhaps highlight where I need to make the changes?

Serial is slooooow.
You print, but before that message has appeared, you check to see if you've received anything

Ohhhhhhhhhhhh, how do I fix this?

With patience.

Any other way? lol

Clearly not. :roll_eyes:

Do you get extra money for wasting the users time?

Thank you :smile:

I apologize, this is just a sketch. I may use a for loop or something like so in the future. Thanks for pointing it out :slight_smile:

And, you guys are getting paid?

Any complete code in the Arduino world is called a sketch.

Less nooby time wasting in an interactive sketch would still be senseless fakery.

Definitely not for pushing rubbish to other people.

To get ideas for serial communication, you can read Robin's Serial Input Basics - updated.

Alternatively, use readBytesUntil().
And if you insists on using String (capital S) you can use readStringUntil().

1 Like

I'll try that asap. Thanks for that! :smile:

Thank you soooooooooooo much! Using your references, I was able to sort the issue with serial by changing a section of the code:

while (Serial.available() == 0)
{

}

Password = Serial.parseInt();

I also declared the integer Password outside to make it publicly accessible.

Thank you all for your assists :slight_smile:

~Phoenix

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.