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.