0

I am trying to compare whether the user input matches a user ID or not. When I read the input from serial, the program is taking the last character which is newline char '\n' and moving it into the char data value to compare against the stored user ID and I don't understand why it's taking the '\n' value instead of the actual user input.

int Gled = 7;
int Rled = 8;
int wait = 5000;
char UserID = "12345";
char data;
char incoming;

void setup() {
  Serial.begin(9600);
  pinMode(Gled,OUTPUT);
  pinMode(Rled,OUTPUT);
  Serial.println("Please Enter your username or ID:");
}

void loop() {
  if(Serial.available()>0) {
    while(1) {
      data=Serial.read(); //read user input and move to data char
      if(data !='\n')
        incoming = data; //if data doesn't equal newline data = incoming
      if (data == '\n')
        break; // newline char detected break
      if (data == -1) //continue reading all of user input
        continue;
      Serial.print(data);
      Serial.print(incoming);
    }
    Serial.println(incoming);
    if(incoming == UserID) { //if user input matches run statement
      digitalWrite(Gled, HIGH);
      Serial.println();
      Serial.println("Access Granted");
      Serial.println("Hello User Name");
      delay(wait);
      Serial.println("Login time exceeded, you are now logged out.");
      Serial.println("Reenter your UserID to log back in.");
      digitalWrite(Gled, LOW);
      Serial.end();
      Serial.begin(9600);
    } else if(incoming != UserID){
      //user input doesn't match run statement
      digitalWrite(Rled, HIGH);
      Serial.println();
      Serial.println("Access Denied");
      Serial.end();
      delay(wait);
      Serial.begin(9600);
      digitalWrite(Rled, LOW);
      Serial.println("Please Enter a Valid ID to Continue.");
    }
  }
}
1
if(data !='n')

This should be

if(data !='\n')

NEXT.

data=Serial.read();

only reads ONE character. You should add to a buffer, not assign a single character.

  • Thats my mistake for leaving that in there I had already copied the text before fixing that typo but the program still has the same issue. – 1fastk Jan 10 '17 at 23:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.