Random Relay Switch using Bluetooth

Hello people! I want to make a Bluetooth controlled Arduino Relay controller. The idea is that if I insert the character 'a' the relay would turn on for a random amount of time, and then turn off, then on, and off ... (you get the idea) until I input character 'A'. In the current state if I insert the character 'a' it only works for a single time, and it doesn't loop. Here's the code:

char val;
int m;
int n;
void setup ()
{
  pinMode(2, OUTPUT);
  Serial.begin(9600);
}

void loop ()
{
  if ( Serial.available() )
  {
    ;
  }
  val = Serial.read();
  if ( val == 'a' )
  {
    do
    {
      n = random(50, 1500);
      m = random(50, 200);
      digitalWrite(2, HIGH);
      delay(n);
      digitalWrite(2, LOW);
      delay(m);
      if ( Serial.available() )
      {
        ;
      }
      val = Serial.read();
      if ( val == 'A' )
      {
        break;
      }
    }
    while ( val == 'a' );
  }
  if ( val == 'A' )
  {
    digitalWrite(2, LOW);
  }
}

Hi, I would get rid of the do loop and have a look the 'Blink without Delay" example in the Arduino IDE to get rid of the delay statements.

delay blocks the execution of your code.

delay(1000); will stop your code for 1 Second, nothing happens, not even any I/O functions.

Tom.. :)