Servo Control

Hi, I am building a simple pan-tilt setup.
I have found this code:

#include <Servo.h>
// This is the pin you need to attach the servo to
#define SERVO_X_PIN 2
#define SERVO_Y_PIN 3

Servo g_servoX;
Servo g_servoY;

void setup()
  Serial.println("Servo Control");

void loop()
  static int valX = 0;
  static int valY = 0;

  if (Serial.available())
    char chX =;
    char chY =;

      case '0'...'9':
        valX = valX * 10 + chX - '0';
      case 'x':
        Serial.print("Servo X is set to: ");
        Serial.print(valX, DEC);
        Serial.println(" degrees");
        valX = 0;

I have customized it a bit but i cant get any further.
My idea is that when I input 90x the X servo goes to 90 degrees and when I input 90y the Y servo goes to 90 degrees.
So I turned to the all-knowing Arduino Forum.
I would like some help,
Thanks in advance!

You have a couple of problems. First, you test that there is at least one character in the serial buffer. Then, you read two characters, storing them in chX and chY.

Your switch(chX) statement is not followed by any curly braces.

Your switch(chY) statement does not have a case for y.

You need to read one character, and have one switch statement, based on that characters. You need cases for x, y, and each number. There are no shortcuts in a switch statement. Since you have only 4 possibilities (x, y, >=0 && <=9, and everything else (which you would ignore)), a switch statement is probably not the base choice.

Just use if/else if/else.

But how can I tell the Arduino when val is followed by an x it must store the value in chX?
Sorry, I am just an beginner but I’m trying my best!

Where is the pan/tilt data coming from? If you have control of the sending code, you should send something like <x:nnn> or <y:nnn>. With the < and > markers, you know when a packet starts and ends. The character before the : then defines whether the number goes to the X servo or the Y servo. The value does not need to be stored in valX or valY, since the string will be passed to atoi and sent to the appropriate servo as soon as the end of packet marker arrives.