So let me start by saying this is my first post here but it will certainly not be my last! I should also say that i am a student and this is for school.
I am having an issue with a program that takes a value 1-7 from the serial monitor and displays the day of the week
that corresponds to that number. We are required to use a switch statement for the days of the week and we get extra credit
for using if statements for the weekends and for invalid inputs. So this is the problem, it works great except that it always prints my error message for invalid inputs after it prints the correct output. I have rearranged the code more than once to try to stop this but its almost like the serial monitor is sending 2 values every time. Is this a coding issue? Below is my code and the output from the serial monitor.
Thanks in advance!
CODE
int d;
// setup program parameters
void setup(){
Serial.begin(9600); // start serial communication
Serial.println("you will be prompted for numbers between 1 and 7");
Serial.println(" that represents the day of the week"); // give user instructions
Serial.println("Please enter a number between 1 and 7.");
}
// loop that calculates the day
void loop(){
while (Serial.available() == 0){
Serial.setTimeout(100);
}
if (Serial.available() > 0){
d = Serial.parseInt();
if (d<1||d>7){
Serial.println("This is not an allowable day number");
}
else if (d==1||d==7){
Serial.println("It's the weekend. Enjoy!");
}
switch (d){
case 2:
Serial.println("Monday");
break;
case 3:
Serial.println("Tuesday");
break;
case 4:
Serial.println("Wednesday");
break;
case 5:
Serial.println("Thursday");
break;
case 6:
Serial.println("Friday");
break;
}
}
}
OUTPUT
Monday
This is not an allowable day number
Tuesday
This is not an allowable day number
Wednesday
This is not an allowable day number
Thursday
This is not an allowable day number
Friday
This is not an allowable day number
It's the weekend. Enjoy!
This is not an allowable day number

