Hi all,
I'm trying to learn some of the basics using an If,,, Else,,, read/print from/to the serial monitor and a void.
My little sketch does not function how I'd imagine, when I run the program & open the serial monitor, if I enter "0" "1" "A" or any letter/ number it returns
You have entered Numeric One
Here is my sketch...
int userInput;
void setup()
{
// initialize serial:
Serial.begin(9600);
Serial.println("Welcome");
Serial.println("");
Serial.println("Please enter 0 or 1");
Serial.println("");
}
void loop()
{
// if there's any serial available, read it:
while (Serial.available() > 0)
{
userInput = Serial.read();
if (userInput == 0 || 1)
{
valueEntered();
}
else
{
Serial.println("Input muck up, enter either 0 or 1");
}
}
}
void valueEntered()
{
if (userInput == 0)
{
Serial.println("Your have entered Numeric Zero");
}
else
{
Serial.println("Your have entered Numeric One");
}
}
I have tried changing int userInput; to char userInput; with better results but still not how I'd image it to behave.
char userInput;
void setup()
{
// initialize serial:
Serial.begin(9600);
Serial.println("Welcome");
Serial.println("");
Serial.println("Please enter 0 or 1");
Serial.println("");
}
void loop()
{
// if there's any serial available, read it:
while (Serial.available() > 0)
{
userInput = Serial.read();
if (userInput == '0' || '1')
{
valueEntered();
}
else
{
Serial.println("Input muck up, enter either 0 or 1");
}
}
}
void valueEntered()
{
if (userInput == '0')
{
Serial.println("Your have entered Numeric Zero");
}
else
{
Serial.println("Your have entered Numeric One");
}
}
This time when I run the program, if I enter 0 into the serial monitor I get the reply
Your have entered Numeric Zero
and if I enter 1 into the serial monitor I get the reply
Your have entered Numeric One
but if I enter 3 or A into the serial monitor I get the reply
Your have entered Numeric One
My program doesn't appear to ever reach my Else, regardless of what invalid input I enter.
My question is... Why doesn't an invalid input trigger the Else in my program?