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.");
}
}
}
-
Your whole data reading and user ID storage and comparison is completely wrong.Majenko– Majenko2017年01月11日 00:01:05 +00:00Commented Jan 11, 2017 at 0:01
-
Read this: hackingmajenkoblog.wordpress.com/2016/02/01/…Majenko– Majenko2017年01月11日 00:02:09 +00:00Commented Jan 11, 2017 at 0:02
-
Some parts of this can help too: hackingmajenkoblog.wordpress.com/2016/02/04/…Majenko– Majenko2017年01月11日 00:02:51 +00:00Commented Jan 11, 2017 at 0:02
-
Thanks for those links, I read them and figured out what I was doing wrong.1fastk– 1fastk2017年01月11日 05:07:49 +00:00Commented Jan 11, 2017 at 5:07
1 Answer 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– 1fastk2017年01月10日 23:52:15 +00:00Commented Jan 10, 2017 at 23:52