0

I do apologize for the constant posts on here. However I ran into an issue and I cannot seem to find any solution to it. The goal is to enter a username, through the Serial.read function, and if the username matches it simply responds "Welcome Steven", and lights a green LED, when the wrong username is submitted it responds "Access Denied" and lights a red LED. So far the code works in response to access granted, and access denied, while also lighting the appropriate LEDs. However the Serial Monitor does not wait until there is a user input, it automatically starts repetitively "access denied" and lights the red LED. When the username is entered the red LED turns off, the green LED turns on, and Serial Monitor repetitively responds "Welcome Steven". I can do this multiple times, and each time it will change state. I have looked at many tutorials and read many posts on here concerning issues, but so far nothing has worked out. Even tried making changes NickGammon suggested in another post. Would like to get rid of the repetitive issue, and wait for user input before access denied or granted appears.

int led1 = 9;
int led2 = 10;
String user_name;

void setup()
{
  Serial.begin(9600);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  Serial.println("Enter Username");
}

void loop()
{
 if(Serial.available())
   user_name = Serial.readStringUntil('\n');
{

if(user_name == "Grantham1")
{
  Serial.println("Welcome Steven");
  digitalWrite(led1, HIGH);
  digitalWrite(led2, LOW);
}
else if(user_name != "Grantham1")
{
  Serial.println("Invalid Input");
  digitalWrite(led1, LOW);
  digitalWrite(led2, HIGH);
}
else
{
  Serial.println("Waiting Pateintly");
  digitalWrite(led1, LOW);
  digitalWrite(led2, LOW);
}
}
}
  • 2
    your code is badly formatted .... indentation is not what it should be .... that makes it very difficult to follow the program flow ..... please correct the formatting and the cause of the problem may become apparent at first glance – jsotola Oct 21 '18 at 0:30
  • @jsotola i edited the code, hope its better. Also for some reason I tried a separate code (for some reason would only accept 4 char lines for an array instead of the 9) but when i tried to re-upload this one, it quit working on me. Still repetitively saying "access denied" but when i enter "Grantham1" it will no longer read it, – Steven Oct 21 '18 at 0:49
  • i do not understand how you are able to run this code .... it has errors and does not compile as a result – jsotola Oct 21 '18 at 1:47
  • @jsotola That could be why it stopped working for me properly after I had uploaded a different code. Every time I compile a new code now, my error window will flash orange, but complete the compile. I managed to build a shorter and much more simpler code for this program, but still having the same issue with the Serial Monitor. Ill update the code through edit. – Steven Oct 21 '18 at 1:50
2

Change

if(Serial.available())
    user_name = Serial.readStringUntil('\n');
{

to

if(Serial.available()) {
    user_name = Serial.readStringUntil('\n');

Edit: The reason why it didn't work properly was because all subsequent ifs were evaluated repeatedly regardless of whether there was anything new in the serial buffer or not. The modification fixes that problem.

  • It did work thank you. I thought I had all the necessary {} in there. Im a little surprised it didnt give me a compiler error for lacking them while uploading. – Steven Oct 21 '18 at 4:34
  • @Steven The if (statement) command; is completely valid in C++ and the stand alone block { commands; } is also perfectly valid in C++ – KIIV Oct 21 '18 at 9:41
  • @Steven, accept the answer if it works now – Juraj Oct 21 '18 at 9:57
  • @Juraj Accepted the answer, I apologize for taking so long to do so. – Steven Oct 21 '18 at 13:39
  • @KIIV Im intermediate in C++ probably why im having so many issues with coding in the Arduino. Wish i could just write with Python that language made so much more sense to me. – Steven Oct 21 '18 at 13:41

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.