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.");
 }
 }
}
dda
1,5951 gold badge12 silver badges17 bronze badges
asked Jan 10, 2017 at 23:29
4
  • Your whole data reading and user ID storage and comparison is completely wrong. Commented Jan 11, 2017 at 0:01
  • Read this: hackingmajenkoblog.wordpress.com/2016/02/01/… Commented Jan 11, 2017 at 0:02
  • Some parts of this can help too: hackingmajenkoblog.wordpress.com/2016/02/04/… Commented Jan 11, 2017 at 0:02
  • Thanks for those links, I read them and figured out what I was doing wrong. Commented Jan 11, 2017 at 5:07

1 Answer 1

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.

answered Jan 10, 2017 at 23:50
1
  • 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. Commented Jan 10, 2017 at 23:52

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.