Simple If... Else... and Void with Serial input

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?

    if (userInput == '0' || '1')

This is valid C/C++ but it isn't what you meant. It will never execute the else clause because '1' is non-zero.
Try:

    if (userInput == '0' || userInput == '1')

read/print from/to the serial monitor and a void

There's no such thing as "a void" in C/C++. There are functions which are declared to return nothing in which case the function name is preceded by the type "void".
So,

void loop() {

is a function named "loop". It doesn't return anything which is why the return type is specified as "void".

Pete

I'm trying to learn some of the basics using an If,,, Else,,, read/print from/to the serial monitor and a void.

If Ritchie had used the word "empty" instead of the unfortunate "void", your sentence would have read:

I'm trying to learn some of the basics using an If,,, Else,,, read/print from/to the serial monitor and an empty.

Now my little program works, thank-you Pete for your help and explanation. I couldn't think what else to call the "void", in Visual basics I believe it would have been called a "Sub Routine"

Many thanks,

Skippy.

"Sub Routine" is far closer to "function" than "void".

-jim lee