switch case from Serial.read multi character / string?

Guys,

sorry for the title... know its a little vauge...

I have a sample code much like the example code that allows me to control a digital pin based on typing a "a" or "A".... (on/off). The issue I am having is that I would like to control that same pin by typing a combination of 2 characters.... for example. type "a" followed by "A" for HIGH, and "A" followed by "a" for LOW...

I can post code, but its really pretty much at the example stage. Let me know if this is clear enough...

Thanks,

Simple pin control code.

// zoomkat 8-6-10 serial I/O string test
// type a string in serial monitor. then send or enter
// for IDE 0019 and later

int ledPin = 13;
String readString;

void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT); 
  Serial.println("serial on/off test 0021"); // so I can keep track
}

void loop() {

  while (Serial.available()) {
    delay(3);  
    char c = Serial.read();
    readString += c; 
  }

  if (readString.length() >0) {
    Serial.println(readString);

    if(readString.indexOf("on") >=0)
    {
      digitalWrite(ledPin, HIGH);
    }

    if(readString.indexOf("off") >=0)
    {
      digitalWrite(ledPin, LOW);
    }

    readString="";
  } 
}

zoomkat,

Thanks for the quick reply. I apologize because I may have simplified what I am looking to do a little too much....

I need to control various pins on a mega, (20 to be exact) and was using switch case based on what character i typed on the keyboard to control the 20 pins...

the question I have is how can I change the single character typed to 2 characters (maybe even followed by enter??)

Let me know if that clarifies... thanks again

sorry. I posted this to the wrong forum. did not mean to double post. won't let me delete it from the other forum. Please let me know if this helps explain what I need....

Hi Guys,

I have a project that I am working on that has 10 shift registers...

The issue I am having is that I need to be able to send characters via serial to turn pins on and off.

the code below is what I have working, but instead of for example, case 'a' or case 'b' I need the unit to wait for more than 1 character before acting. this will enable me to have more than 255 options....

Can someone please help me. not sure what way to go here....

void loop(){
  
   if (Serial.available() > 0) {
     int inByte = Serial.read();
     // do something different depending on the character received.  
     // The switch statement expects single number values for each case;
     // in this example, though, you're using single quotes to tell
     // the controller to get the ASCII value for the character.  For 
     // example 'a' = 97, 'b' = 98, and so forth:

     switch (inByte) {
     case 'a':    
      setRegisterPin(0, HIGH);
      writeRegisters();       
       break;
     case 'b':    
      setRegisterPin(1, HIGH);
      writeRegisters();       
       break;

the below will not compile... but here is what I would like for example....

void loop(){
  
   if (Serial.available() > 0) {
     int inByte = Serial.read();
     // do something different depending on the character received.  
     // The switch statement expects single number values for each case;
     // in this exmaple, though, you're using single quotes to tell
     // the controller to get the ASCII value for the character.  For 
     // example 'a' = 97, 'b' = 98, and so forth:

     switch (inByte) {
     case '!0301':    
      setRegisterPin(0, HIGH);
      writeRegisters();       
       break;
     case '!0401':    
      setRegisterPin(0, LOW);
      writeRegisters();       
       break;
     case '!0302':    
      setRegisterPin(1, HIGH);
      writeRegisters();       
       break;
     case '!0402':    
      setRegisterPin(1, LOW);
      writeRegisters();       
       break;

Remove message