Setting different variables from serial input

Hey there, I was wondering if there was a way to set different variables from the serial input, depending on the first character

For example if I have two variables called 'myString' and 'myBrightness' if the first input is "S2" (Just an example) which would put the char '2' into 'myString' and for example "B150" would put '150' into the variable 'myBrightness'

How would I accomplish this?

thanks

How would I accomplish this?

First, your examples leave a lot to be desired. A letter followed by a number is a problem, because you have no idea when the number ends. 2S or 152B means that you know when the number ends, but that makes parsing more difficult.

S2; or B152; also delimits the number, and keeps the identifier up front.

Once you have a complete packet, such as "B152", you can look at the first letter to see where store the value. Then, replace the first letter with a space, and call atoi() on the result, to get an int. Now, you know where to store the int.

Below is some code for servo control. The data packet sent from the serial monitor first has a numeric command value, followed by the applicable servo for the command, then ended with a comma , to mark the end of the data packet.

//zoomkat 11-22-12 simple delimited ',' string parse 
//from serial port input (via serial monitor)
//and print result out serial port
//multi servos added 

String readString;
#include <Servo.h> 
Servo myservoa, myservob, myservoc, myservod;  // create servo object to control a servo 

void setup() {
  Serial.begin(9600);

  //myservoa.writeMicroseconds(1500); //set initial servo position if desired

  myservoa.attach(6);  //the pin for the servoa control
  myservob.attach(7);  //the pin for the servob control
  myservoc.attach(8);  //the pin for the servoc control
  myservod.attach(9);  //the pin for the servod control 
  Serial.println("multi-servo-delimit-test-dual-input-11-22-12"); // so I can keep track of what is loaded
}

void loop() {

  //expect single strings like 700a, or 1500c, or 2000d,
  //or like 30c, or 90a, or 180d,
  //or combined like 30c,180b,70a,120d,

  if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == ',') {
      if (readString.length() >1) {
        Serial.println(readString); //prints string to serial port out

        int n = readString.toInt();  //convert readString into a number

        // auto select appropriate value, copied from someone elses code.
        if(n >= 500)
        {
          Serial.print("writing Microseconds: ");
          Serial.println(n);
          if(readString.indexOf('a') >0) myservoa.writeMicroseconds(n);
          if(readString.indexOf('b') >0) myservob.writeMicroseconds(n);
          if(readString.indexOf('c') >0) myservoc.writeMicroseconds(n);
          if(readString.indexOf('d') >0) myservod.writeMicroseconds(n);
        }
        else
        {   
          Serial.print("writing Angle: ");
          Serial.println(n);
          if(readString.indexOf('a') >0) myservoa.write(n);
          if(readString.indexOf('b') >0) myservob.write(n);
          if(readString.indexOf('c') >0) myservoc.write(n);
          if(readString.indexOf('d') >0) myservod.write(n);
        }
         readString=""; //clears variable for new input
      }
    }  
    else {     
      readString += c; //makes the string readString
    }
  }
}

My thoughts... your variables should have better names to match their function and types.
myString implies a null terminated C string with no clue regarding it's function.

'S' is a single command character and is best stored in a char variable rather than a multi-character string.

I recommend this site for C++ tutorials, and this section in particular regarding Hungarian Notation
http://www.learncpp.com/cpp-tutorial/29-hungarian-notation/

constant char chTerminator = ","; 

char chCommand = '?';   // A command character variable
int  nBrightness = 0;   // An integer variable  (do you plan to use negative numbers?)

String strBrightness = "0"; // Always assign variables and objects with 'safe' values.

Is the servo code you posted a reference, or your actual project? It is not a good example.

Start your project by coding the keyboard input, and print the result. Once satisfied, then add other code to handle it.

I suggest examining each input character:

if it is a digit append it to strBrightness...
If a valid command char, assign it to chCommand
If chTerminator, convert strBrightness to int and assign it to nBrightness then execute your command if brightness is reasonable ( 0 to 255 )?
Default ignore

char chLastCharacterRead = Serial.read();  // one char
if (isDigit(chLastCharacterRead)) 
{
   strBrightness += chLastCharacterRead;  // Builds an unsigned numeric string 
}
else  // command or  terminator or garbage
{
// Setup a case structure here to test for a command char in the range expected or chTerminator, default to ignore garbage   


}