Hello everybody. So Ive browsed a lot on here already with problems regarding strings with the arduino, and I've viewed the intro to serial comms that people like to refer to here. My main question is, when I use the if statement to evaluate if two strings are the same (if data==username), it doesn't evaluate true, even though I have the serial monitor print out both strings right before and they are the same exact string!
const int GLED=11;
const int RLED=9;
String data; //holds incoming string
String username;
int i = 0;
void setup()
{
Serial.begin(9600); //Serial Port at 9600 baud
pinMode(GLED, OUTPUT);
pinMode(RLED, OUTPUT);
username = String("tmahaffey");
}
void loop()
{
if (i <1)
{
Serial.println("Please enter your username or ID");
i++;
}
if (Serial.available() > 0)
{
data = Serial.readString(); //read string
Serial.println("Seraial Read String is: " + data);
Serial.println("Username String is: "+ username);
if (data == username)
{
Serial.println("Hello " + username);
digitalWrite(GLED, HIGH);
delay(5000);
digitalWrite(GLED, LOW);
i = 0;
}
else if (data != username)
{
Serial.println("Invalid Username");
digitalWrite(RLED, HIGH);
delay(5000);
digitalWrite(RLED, LOW);
i =0;
}
}
}