Breaking up Serial.read result

Hey guys, I tried searching different terms but honestly I'm not really sure what to call this question. I have a simple code that receives a serial command for instance "001B" I want to be able to trim the 001 and the B and use them to make decisions but everything I've tried either locks up the arduino or just loops endlessly, so for the time being I've hard coded each option. What is the best way to accomplish this? Thanks!

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

}

void loop() {
String readString;
String Q;


while (Serial.available()){
  delay(1);
  if(Serial.available()>0){
  char c = Serial.read();
    if (isControl(c)){
   break;
   }
   readString += c;
   }
  }
Q = readString;

if (Q=="001B"){
  position1B();
 }

You are using Strings so why not use the String functions startsWith(), endsWith() or substring() to parse the input ?

Whether you should be using Strings (uppercase S) as opposed to C style strings (lowercase s) is a different question

almost all my code starts with the following called from loop() to accept uncomplicated commands from the serial monitor, customized for what the program needs.

// -----------------------------------------------------------------------------
// process single character commands from the PC
void
pcRead (void)
{
    static int  analogPin = 0;
    static int  val = 13;

    if (Serial.available()) {
        int c = Serial.read ();

        switch (c)  {
        case '0':
        case '1':
        case '2':
        case '3':
        case '4':
        case '5':
        case '6':
        case '7':
        case '8':
        case '9':
            val = c - '0' + (10 * val);
            break;

        case 'A':
            analogPin = val;
            Serial.print   ("analogPin = ");
            Serial.println (val);
            val = 0;
            break;

        case 'D':
            debug ^= 1;
            break;

        case 'I':
            pinMode (val, INPUT);
            Serial.print   ("pinMode ");
            Serial.print   (val);
            Serial.println (" INPUT");
            val = 0;
            break;

        case 'O':
            pinMode (val, OUTPUT);
            Serial.print   ("pinMode ");
            Serial.print   (val);
            Serial.println (" OUTPUT");
            val = 0;
            break;

        case 'P':
            pinMode (val, INPUT_PULLUP);
            Serial.print   ("pinMode ");
            Serial.print   (val);
            Serial.println (" INPUT_PULLUP");
            val = 0;
            break;


        case 'a':
            Serial.print   ("analogRead: ");
            Serial.println (analogRead (val));
            val = 0;
            break;

        case 'c':
            digitalWrite (val, LOW);
            Serial.print   ("digitalWrite: LOW  ");
            Serial.println (val);
            val = 0;
            break;

        case 'p':
            analogWrite (analogPin, val);
            Serial.print   ("analogWrite: pin ");
            Serial.print   (analogPin);
            Serial.print   (", ");
            Serial.println (val);
            val = 0;
            break;

        case 'r':
            Serial.print   ("digitalRead: pin ");
            Serial.print   (val);
            Serial.print   (", ");
            Serial.println (digitalRead (val));
            val = 0;
            break;

        case 's':
            digitalWrite (val, HIGH);
            Serial.print   ("digitalWrite: HIGH ");
            Serial.println (val);
            val = 0;
            break;

        case 't':
            Serial.print   ("pinToggle ");
            Serial.println (val);
            pinToggle (val);
            val = 0;
            break;

        case 'v':
            Serial.print ("\nversion: ");
            Serial.println (version);
            break;

        default:
            break;
        }
    }
}

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. This can happen after the program has been running perfectly for some time. Just use cstrings - char arrays terminated with '\0' (NULL).

Have a look at the examples in Serial Input Basics - simple reliable non-blocking ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

...R

alanblinkers:
Hey guys, I tried searching different terms but honestly I'm not really sure what to call this question. I have a simple code that receives a serial command for instance "001B" I want to be able to trim the 001 and the B and use them to make decisions but everything I've tried either locks up the arduino or just loops endlessly, so for the time being I've hard coded each option. What is the best way to accomplish this? Thanks!

Is it your requirement?
You want to send this string: "001B" from the InputBox of the Serial Monitor of Fig-1 to Arduino UNO which will strip out 001 from the string and will save in an int type variable and will save B in a char type variable.
SerialMonitor.png
Figure-1:

SerialMonitor.png