Please Help, Switch case turn LED fading on and off using serial.read

Hi,
I have trawled through everywhere on the internet and searched through this forum, but alas I haven't been able to find a solution to this problem I am having.
What I am trying to do:
I am trying to get an LED to PWM fade in and out based on these conditions
a: when Arduino is turned on automatically PWM fade LED in and out
b: when serial read picks up 'p' from incoming Byte STOP PWM LED fade
c: when serial read picks up 'o' from incomingByte START PWM LED fade until it recieves a 'p' from incomingByte to stop.

I have tried switch case styles of programming, e.g. case a:, case b:, default:,
but it will only run the LED PWM fade through 1 in and out cycle when I hit STOP - 'p'
it will run through the cycle twice when i hit START -> 'o'
when the arduino starts up automatically the LED will stay off.
and I have tried some others, but it does seem like case switch is the best way, even though I still can't get it to work.

But still no luck ;(
if anyone has any ideas if I'm going up the right or wrong tree, please help.
here is the case style code

const int ledPin = 9;
int incomingByte;

void setup() {
  Serial.begin(9600); 
      pinMode(ledPin, OUTPUT);
      } 

void loop() {
  if (Serial.available() > 0) {
    incomingByte = Serial.read();
    switch (incomingByte) {
    case 'o': //START fade and loop until receives 'p' to STOP
    //fade in
      for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) { 
    analogWrite(ledPin, fadeValue);             
    delay(30);                            
  } 
  // fade out
  for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) { 
    analogWrite(ledPin, fadeValue);             
    delay(30);                            
  } 
      break;
    case 'p': // STOP fade    
      digitalWrite(ledPin, LOW);
      break;
    default: //automatically START fading when turned on until recieves 'p' to STOP
    //fade in
     for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) { 
    analogWrite(ledPin, fadeValue);         
    delay(30);                            
  } 
  // fade out
  for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) { 
    analogWrite(ledPin, fadeValue);         
    delay(30);                            
  } 
      }
    } 
  }

As currently written, the fade in/out will not start until you send something (anything) from Serial because ofif (Serial.available() > 0)
If you start it with an 'o' it will not stop until the fade up and down has completed because it cannot read the serial input until it is finished. When it is finished the program will read whatever is in the serial buffer which may be a carriage return, linefeed or both if you have that as your line termination in the serial monitor. As a matter of interest, what have you got set ?

You need to separate the "decide what to do" part of your code from the "do that" part.

Most of the time, you want the "fade the LED" code to be running. So, create a function to fade the LED, and call that on every pass through loop, unless a p has arrived on the serial port and an o has not.

bool paused = false;
void loop()
{
   if(Serial.available() > 0)
   {
     char ltr = Serial.read();
     if(ltr== 'p')
       paused = true;
     else if(ltr == 'o')
       paused = false;
  }

  if(!pause)
    fadeLED();
}

The code for fading the LED then goes in the function. Of course, not the code that you have, since that is blocking code. You need to look at the blink without delay example for inspiration to get rid of the delay() calls and for loops.

Think of it this way. If it is time to change the intensity, change the intensity. Apply the intensity value to the pin. Return.

Now, you have to figure out what "is it time to change" means, and how to determine that.

You need to determine how to assign a new value, and keep that value in range. Incrementing a global or static variable seems like a logical choice, but incrementing is not always appropriate. Sometimes you need to decrement the value. So, having a delta variable that is sometimes +1 and sometimes -1 is better. When should it be +1 and when should it be -1? Start with +1. When the current value for the PWM pin reaches 255, change delta to -1. When the current value reaches 0, change delta to +1.

Thank you!!
YAHOO!!!
It is now doing what I want it to do!!!
Now I can finally return home after a long day, and night, in the studio
Although it is still a little jumpy when I START the fade with 'o', as it goes back to where the fading point last stopped. e.g. if in full brightness when pressed 'p' the LED will turn off, when press 'o' to START fade it will jump back to full brightness where it left off rather than fade up.
But hell, I am happy to live with that until after I get some sleep... and plug away at it more with some fresh enthusiasm! :wink:
Again, thank you so much!!!

here is the code that worked

boolean paused = false;
const int ledPin = 9;   // the pin that the LED is attached to
int brightness = 0;    // how bright the LED is
int fadeAmount = 5;

void setup() {
  Serial.begin(9600); 
      pinMode(ledPin, OUTPUT);
      } 

void loop() {
if (Serial.available() >0)
{
  char ltr = Serial.read();
  if(ltr == 'p') //pause that loop and turn off LED
  {
  paused = true;
  digitalWrite(ledPin, LOW);
  }
  else if (ltr == 'o')
  paused = false;
}
if (!paused){
  //fade baby fade!!!
      analogWrite(ledPin, brightness);    
  brightness = brightness + fadeAmount; 
  if (brightness == 0 || brightness == 255) {
    fadeAmount = -fadeAmount ; 
  }        
  delay(30);
}
}

Although it is still a little jumpy when I START the fade with 'o', as it goes back to where the fading point last stopped. e.g. if in full brightness when pressed 'p' the LED will turn off, when press 'o' to START fade it will jump back to full brightness where it left off rather than fade up.

Because that is what you are telling it to do. If you want the fading to start from 0 every time, set brightness to 0 when the 'o' arrives.