com port and loops

Hi all, I am making a 3x3 stage blinder and want to make it do some patterns , the electrical part is all tested and works great (triacs with optocapler to turn the 220v bulbs via the 5v arduino outs) , now I have problems with the code:

  1. It will be controlled via a computer COM port, I have made this work, but only one letter sent= one pin goes on or off, I want to know how to make it go to a loop when it receives something from the computer, for example if I send letter b it will go to a loop that blinks all the bulbs? This is the code so instead of digital write(9,HIGH) some goto or something similar to go to a loop ?
void setup ()
{
  pinMode(9,OUTPUT);
  digitalWrite(9,LOW);
  Serial.begin(9600);
  }
void loop()
{
  if(Serial.available() > 0 )
  { 
    char letter = Serial.read();
    
    if(letter == '1') 
    {digitalWrite(9,HIGH);
    }
    else if(letter == '0')
    {
      digitalWrite(9,LOW) ; 
    }
    
  }
}
  1. how to make the loop repeat itself until it receives something else and how to stop it in the middle of execution ? So can the loop look like turn pins pins from 1 to 5 in a sequence on and off with delays between, repeat that until you get some other signal , and if you get the signal before you get to pin 3 stop, and go to some other loop ?

sorry if these are some trivial questions, but I am fairly new to this and cant figure this out

Put the stuff you want to happen into a function and set a variable that tells the function to work (or not) instead of (or as well as) digitalWrite() when the character is received. Something like this:

[code] if(letter == '1') 
    {digitalWrite(9,HIGH);
     startFlashing = true;
     // etc
}
myFlashingFuntion() {
   if (startFlashing == true) {
       // do some clever stuff
   }
}

And the code in loop() should be something like this

void loop() {
  getDataFromPC();  // the code you now have to get the data including setting a variable as above
  myFlashingFunction();

}

And make sure that the code in the myFlashingFunction() does not block anything. Use millis() for timing. See the demo several things at a time.

...R