Understanding Serial Communication

I'm quite new to the whole arduino/electronics thing in general so please excuse my ignorance but here's my issue:

I'm simply trying to write something that will let me control a continuous servo by entering the values "0", "90" or "180" into the serial monitor, respective to the values of a continuous servo.

This is my code:

void loop() {
  if (Serial.available() > 0) {
    char x[4] = {Serial.read()};
    if (x == "0" || x == "90" || x == "180") {
      myservo.write(x);
    }
    else Serial.print("invalid");
  } 
}

I was debugging by printing out what I was putting into the serial monitor, but while doing this I encountered the issue that as soon as I tried to check the value of "x", it wouldn't output properly.

I kind of assumed it was something to do with the way serial communication works, thus me posting here but I'm not sure what to do, any help would be appreciated.

ethan_fraser: but while doing this I encountered the issue that as soon as I tried to check the value of "x", it wouldn't output properly.

So what happened? http://forum.arduino.cc/index.php?topic=396450.0

It would print "invalid" one more times than there were characters in the text I inputed For example if I put "0" into the serial monitor, it would output

invalid invalid

or if I put "180" it would output

invalid invalid invalid invalid

etc.

When you enter 180 in the Serial Monitor it sends the three characters '1' '8' and '0' but your program is only reading one character.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

...R

    char x[4] = {Serial.read()};

The array is allocated and initialized with ONE character (plus the terminating NULL).

    if (x == "0" || x == "90" || x == "180") {

The address of the array is not likely to be "0" or "90" or "180". So that code will ALWAYS evaluate to false.

PaulS:     char x[4] = {Serial.read()};

The array is allocated and initialized with ONE character (plus the terminating NULL).

    if (x == "0" || x == "90" || x == "180") {

The address of the array is not likely to be "0" or "90" or "180". So that code will ALWAYS evaluate to false.

I think it would help to explain that the compiler will assume that when you use x you want the address of the start of the array whereas when you use x[0] it knows that you want the contents of the the first element.

...R