Final else statement printing after each user input even if other conditions are met

Hi all, I'm stuck on a bug in my code that causes the final else statement to print through serial monitor, even if the else if statements preceding it return true.

//a program that reads a user input and responds with a value assigned to the input.
int rxusr;
int GP0_9[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int i;
void setup() {
  for (i = 0; i<=9; i++) {
    pinMode(GP0_9[i], OUTPUT);
  }
  
  Serial.begin(9600);
  while (!Serial) {
    ;
  }
  Serial.print("Serial Input of User-Defined Value \n\nPlease input an integer value between 1 and 3");

}

void loop() {
  if (Serial.available() > 0) {
   rxusr = Serial.parseInt();
    
   if (rxusr == 1) {
    Serial.print("\nThe student ID value corresponding to the integer 1 = 7 and will be displayed on output GP9 - GP3.\nPlease input the next integer value:");
    for (i = 3; i <=9; i++) {
      digitalWrite(GP0_9[i], HIGH);
      }
     }
   else if (rxusr == 2) {
    Serial.print("\nThe student ID value corresponding to the integer 2 = 8 and will be displayed on ouputs GP9-GP2\nPlease input the next integer value:");
    for (i = 2; i <= 9; i++) {
      digitalWrite(GP0_9[i], HIGH); 
      }
    }
   else if (rxusr == 3) {
   Serial.print("\nThe student ID value corresponding to the integer 3 = 3 and will be displayed on outputs GP9-GP7\nPlease input the next integer value");
    for (i = 7; i <= 9; i++) {
      digitalWrite(GP0_9[i], HIGH);
      }
    } 
   else {
    Serial.print("\nNot within scope - try again.");
    for (i = 0; i <= 9; i++) {
      digitalWrite(GP0_9[i], LOW);
      }
    }
  } 
}  

I've done a very similar thing with a different piece of code but with push buttons instead of the user input from serial monitor and everything worked fine. This time though, I get the following:

Serial Input of User-Defined Value 

Please input an integer value between 1 and 3
The student ID value corresponding to the integer 1 = 7 and will be displayed on output GP9 - GP3.
Please input the next integer value:
Not within scope - try again. 
The student ID value corresponding to the integer 2 = 8 and will be displayed on ouputs GP9-GP2
Please input the next integer value:
Not within scope - try again.
The student ID value corresponding to the integer 3 = 3
Please input the next integer value
Not within scope - try again.
//these final two are what happens when you enter an unexpected value
Not within scope - try again.
Not within scope - try again.

As you can see, everything else works perfectly, I'm just getting that extra "Not within scope" message (after approx. 1 second delay) each time I enter a value, regardless of what it is, almost like it's checking the for the integer twice, which I thought Serial.parseInt should stop from happening.

Sorry if there's a really obvious answer to this or it's been solved before, I'm still kind of at beginner level but not a complete noob and thought I'd got my else/if statements down to a T, but I can't seem to get the last one to work here, plus I have done a fair amount of trawling the internet but couldn't find anything; from what I'd looked at online plus what I've already done before I was sure my code should've worked so now am at a loss.

Thanks in advance.

What have you got the Line ending set to in the Serial monitor ?

Anything except "No Line Ending" will mean that the line ending character will be interpreted as an input, will not match what you test for and the code of the final else will execute

1 Like

What Arduino are you using? You may not be able to use pins 0 and 1 and serial at the same time.

I'm using a Pi Pico, as far as I'm aware I shouldn't have a problem here but could be wrong!

That could be what I'm missing! I've not got anything like that in my code. I'll take a look at how to implement that & see if it solves my issue, thanks!

It's easy to test. Change the line-ending.

After that you can consider of you want / need to change your code or not.

1 Like

I see I misunderstood what you meant, I changed the setting in the serial window and it solved that issue, though it did create another where my LEDs on the output don't change or switch off but I'm fairly certain I know how to sort that. Thanks again for the help!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.