Reading wrong serial inputs?

I have a code for a serial input code but in the serial monitor it reads the wrong code. For example when i type in "s" it reads "w" and when I press "w" it still reads "w". This is the code:

int motorPin = 9;

void setup() {
// put your setup code here, to run once:
pinMode(motorPin, OUTPUT);
Serial.begin(9600);
while (! Serial);
Serial.println("w to move, s to stop");
}

void loop() {
// put your main code here, to run repeatedly:
{if (Serial.available())
{
int speed = Serial.parseInt();
{if (Serial.print("w"))
{
digitalWrite(motorPin, HIGH);
}
}
}
}

{if (Serial.available())
{
int speed = Serial.parseInt();
{if (Serial.print("s"))
{
digitalWrite(motorPin, LOW);
}
}
}
}

}

{if (Serial.available())
{
  int speed = Serial.parseInt();
  {if (Serial.print("w"))
  {
digitalWrite(motorPin, HIGH);
  }
  }

This part looks like complete nonsense. parseInt( ) is trying to read a number, like 123. 'w' and 's' are not numbers.

if ( Serial.available() >= 1 )
{
    char ch = Serial.read() ;
    if ( 'w' == ch )
    {
       // do  something
    }
    else if ( 's' == ch )
    {
       // do something else
    }
    else
    {
        Serial.println("Got char %c,  don't know what to do\n", ch );
     }
}

Welcome to the Forum. Please read the two posts by Nick Gammon at the top of this Forum. It contains guidelines for posting comments here, such as using code tags when posting code. The guidelines are there so you can help us help you.

if (Serial.print("w"))

What are you trying to test here ? The Serial.print will succeed and return true so the test will always return true.

So basically I have attached a regular DC motor to the arduino using the breadboard and built a circuit to keep the arduino from getting burnt out. To make the motor spin all I have to do is put the pin on HIGH and it will start running, king of like an LED. But I want to control the motor with the serial command so when I type in "w" and enter it will start and when I type in "s" it will stop. The problem is it is not reading the right inputs. So when I press "w" it might read it as an "s" or vice versa. There obviously is a bug in the program but I can't figure it out.

Basic single character pin on/off test code that might be of use.

//zoomkat single character pin on/off test code 6-20-14

int ledPin = 13;
char c = 0;
void setup()
{
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT); 
  Serial.println("serial on/off test 6-20-2014"); // so I can keep track
}
   
void loop()
{
  if (Serial.available() > 0) 
  {
    c = Serial.read();
    if (c == '1')     // assuming you send character '1', not a byte with value 1 
    {
       digitalWrite(ledPin, HIGH);
       Serial.println(c);
    }
    if (c == '2')
    {
       digitalWrite(ledPin, LOW);
       Serial.println(c);
    }
  }
}

zoomcat has given a good example of using the Serial object to make the Arduino do something. I made two very minor changes: 1) a simple message to tell the user the input that is expected, and 2) the use of an if-else, since there's no reason to perform the second if test if the first is logic True.

//zoomkat single character pin on/off test code 6-20-14

int ledPin = 13;
char c = 0;
void setup()
{
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
  Serial.println("serial on/off test 6-20-2014"); // so I can keep track
  Serial.println("1 = ON, 2 = OFF");              // Help the user get it right.
}

void loop()
{
  if (Serial.available() > 0)
  {
    c = Serial.read();        // You could subtract '0' from the byte and use the result as an int
    if (c == '1')             // assuming you send character '1', not a byte with value 1
    {
      digitalWrite(ledPin, HIGH);
      Serial.println(c);
    } else {                  // No sense checking for '2' if c equals '1'
      if (c == '2')
      {
        digitalWrite(ledPin, LOW);
        Serial.println(c);
      }
    }
  }
}

Right now you have to type in they key and press enter for it to run. What I am wondering is can I have it only run when they kwy is pressed.

blob123789:
Right now you have to type in they key and press enter for it to run. What I am wondering is can I have it only run when they kwy is pressed.

Not possible using the Serial monitor because nothing is sent until you press Enter. A terminal emulator such as puTTY can do it though.