If String1 equals String2, continue program

Hello everyone,

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);
    }
}

Reading_string_password.ino (467 Bytes)

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.

I tried to update it to this with no avail. Am I doing something redundantly?

Thank you!

String iAccess = "";
String accessCode = "1234"; 

void setup() {
  Serial.begin(19200);
  while (!Serial) {
    ; //waiting for port to connect
  }
}

void loop() {
   if (Serial.available() > 0){
    iAccess = Serial.readString();
    Serial.println(iAccess);
    delay(500);
    if (iAccess.equals(accessCode)){
      Serial.println("You typed the correct password");
      delay(10000);
    }
    else{
      Serial.println("You typed the incorrect password");
      delay(10000);
    }
   }
}

I tried to update it to this with no avail.

What does that mean? What was printed for your password?

marco_c:
What does that mean? What was printed for your password?

It printed out the correct password after I typed it in, "1234", but after it still said incorrect password.

From the library reference

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

Serial.print("|");
Serial.print(iAccess);
Serial.println("|");

because the second '|' would be on the next line.

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.

...R

Helo EnglishScone

Try :

String iAccess = "";
String accessCode = "1234";

void setup() {
  Serial.begin(115200);
  while (!Serial) {
    ; //waiting for port to connect
  }
}

void loop() {
 if (Serial.available() > 0) {
    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);
    }
 }   
 else {
  Serial.println(" password ?");
 }
}

But avoid Strings !!

Regards,
bidouilleelec

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.

Sincerely,

-Englishscone

My code right now is saving incoming strings to EEPROM.

Why? The number of writes to EEPROM is limited.

EEPROM is a fine place to store data that is not often changed.

Englishscone:
Would this saving method still risk corruption?

Using the String class represents a risk. You won't know there is a problem until there is a problem. You might be lucky.

...R

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.

Thank you!

Sincerely,

Gabriel Peck