I am working on a simple section of code that requires the serial input of a password to continue. Whenever I run this however, it keeps saying incorrect. What am I missing?
Thank you for any help!
Sincerely,
-Englishscone
String iAccess = "";
String accessCode = "1234";
void setup() {
Serial.begin(19200);
while (!Serial) {
; //waiting for port to connect
}
}
void loop() {
iAccess = Serial.readString();
Serial.println(iAccess);
if (iAccess.equals(accessCode)){
Serial.println("You typed the correct password");
delay(10000);
}
else{
Serial.println("You typed the incorrect password");
delay(10000);
}
}
Why don't you print out the string to see what you are actually comparing? I am guessing that you receive it in fragments and are not waiting for the while thing to arrive.
readString() reads characters from a stream into a String. The function terminates if it times out
The standard timeout is 1 second.
The problem may be that the 'string' includes the (invisible) newline termination at the end of your Serial input. You can test this by turning off line endings in the serial monitor and just typing 1234 and waiting for the timeout. You would also see the ending's effect if you printed it as
marco_c:
From the library referenceThe standard timeout is 1 second.
The problem may be that the 'string' includes the (invisble) newline termination at the end of your Serial input. You can test this by turning off line endings in the serial monitor and just typing 1234 and waiting for the timeout. You would also see the ending's effect if you printed it as
Serial.print("|");
Serial.print(iAccess);
Serial.println("|");
because the second '|' would be on the next line.
Thank you! This helped. I added an \n to the accessCode. Then it read correctly. It seems to work fine, let me update my code to stop if the input is incorrect and see if it works.
It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. This can happen after the program has been running perfectly for some time. Just use cstrings - char arrays terminated with '\0' (NULL).
When using Cstrings you must use strcmp() to compare values rather than ==
Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.
If String1 equals String2, continue program
You also need to say what should happen if the strings are not the same as that may affect how to go about the test.
Robin2:
It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. This can happen after the program has been running perfectly for some time. Just use cstrings - char arrays terminated with '\0' (NULL).
When using Cstrings you must use strcmp() to compare values rather than ==
Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.
You also need to say what should happen if the strings are not the same as that may affect how to go about the test.
...R
Thank you for all the advice! My code right now is saving incoming strings to EEPROM. Would this saving method still risk corruption? As a noobie, I thought it was fairly safe.
jremington:
Why? The number of writes to EEPROM is limited.
EEPROM is a fine place to store data that is not often changed.
I need these saved strings in EEPROM because I am creating a key ring password manager, it won't always be plugged in. The code works fine now, but my created encryption code to scramble the strings is making thing complicated. Haha.