issues with Serial.println for unrecognized commands

I'm unfortintly very very new to programming so of course i have 9000 syntax errors but this issue has me completely puzzled.

What i want it to do.
input of 1 (over Serial.read) flips the motor on for 1 second forward sending a Serial.println of "moving forward" and "stopped moving" after 1 second and the Hbridge is shut off.

input of 2 (Serial.read again) flips the motor on for 1 second reverse sending the same information and same time interval.

input of anything else causes a Serial.println of "huh?"

What is happening...

on an input of 1 the motor turns on and off just fine but the Serial.println of
"moving forward
stopped moving
huh?"

i have absolutely no clue why its giving me "huh?" at the end... heres the code (i removed asimov's laws from the // hopefully all of you know them already...)

int reversePin1A =2;
int reversePin2A =3;
int forwardPin1A =4;
int forwardPin2A =5;

void setup()
{
  Serial.begin(9600);
  pinMode(forwardPin1A, OUTPUT);
  pinMode(forwardPin2A, OUTPUT);
  pinMode(reversePin1A, OUTPUT);
  pinMode(reversePin2A, OUTPUT);
}

void loop()
{
  while (Serial.available() == 0);
  int val =  Serial.read() - '0';
  if (val == 1)//input of 1 makes it move forward 1 second
  {
    Serial.println("moving forward");
    digitalWrite(forwardPin1A, HIGH);
    digitalWrite(forwardPin2A, HIGH);
    delay(1000);
    digitalWrite(forwardPin1A, LOW);
    digitalWrite(forwardPin2A, LOW);
    Serial.println("stopped moving");
  }
  if (val == 2)//input of 2 makes it move backwards for 1 second
  {
    Serial.println("moving reverse");
    digitalWrite(reversePin1A, HIGH);
    digitalWrite(reversePin2A, HIGH);
    delay(1000);
    digitalWrite(reversePin1A, LOW);
    digitalWrite(reversePin2A, LOW);
    Serial.println("stopped moving");
  }
  else
  {
    Serial.println("huh?");// unrecognized command confusses it
  }
}

thanks in advance for helping a newb

Your serial monitor is probably configured to send a carriage return and/or new line at the end and that is what it is receiving. You could always print a better debug message than "huh?" to tell you what value it is seeing.

I wonder if it works better if you say:

else if (val == 2)//input of 2 makes it move backwards for 1 second

-br

billroy:
I wonder if it works better if you say:

else if (val == 2)//input of 2 makes it move backwards for 1 second

-br

that fixed it... so i have to ask... why? i can always dig around and figure it out but if you want to be a kind soul and take pity on me it would be appreciated :slight_smile:

This is a broken-chain-of-if-then-else bug.

Without the else, your code is falling into the check for val=='2' even if it has already established that val=='1'. This is the case for the bug you asked about.

Unfortunately, since we know val is not '2' (when it's '1'), the else clause after the if '2' check is firing in this case, printing "Huh". Which is the behavior you didn't want.

So, putting an else on the if val==2 properly chains the character checks, preventing the final else clause from firing in the wrong case.

Does that help?

-br

you know... when you say it like that its sounds pretty dang simple.

thank you.