user8715: Hell0!
I run in small problem. I am try to control LED's by serial port.So I am expect to send "1" and two LED's will start to blink or send "0" and only one LED is constantly lighted up. At first I thought that do-wile will work, Arduino will run do loop while value sent by serial port is "1" or "0". However at first I sent "0" and one LED are lighted, after I send "1" and two LED's are blinking. But when I send "0" again, LED's keeps blinking. What should I change in my code?
const int ledPin2=2;
const int ledPin3=3;
const int ledPin4=4;

void setup()
{

  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(ledPin4, OUTPUT);

 Serial.begin(9600);
}

void loop() 
{
  while (Serial.available()==0);

  int serialVal = Serial.read()-'0';


  do
   {
      digitalWrite(ledPin3, LOW);
      digitalWrite(ledPin4,HIGH); 
    } while(serialVal==0);

  do
    {
      digitalWrite(ledPin4,LOW);   
      digitalWrite(ledPin2, HIGH);
      digitalWrite(ledPin3, LOW);

      delay(100);

      digitalWrite(ledPin3, HIGH);
      digitalWrite(ledPin2, LOW);

      delay(100);

  }while (serialVal==1);
};


user8716: I don't think you understand how your code is being executed. I would recommend trying the following.
const int ledPin2=2;
const int ledPin3=3;
const int ledPin4=4;
int serialVal;

void setup()
{

  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(ledPin4, OUTPUT);

 Serial.begin(9600);
}

void loop() 
{
  // If data is available, then read it!
  if (serial.available() > 0) 
  {
      serialVal = serial.read()-'0';
  }

  // If serial val is 1, turn on LEd4, turn off Led3
  if (serialVal == '1')
  {
      digitalWrite(ledPin3, LOW);
      digitalWrite(ledPin4,HIGH); 
  }

  // If serial val is 0, 
  // turn on LED2. Turn off Led3
  // delay for 100ms
  // turn of LED2, turn on LED3
  // delay for 100 ms
  if (serialVal == '0')
  {
      digitalWrite(ledPin4,LOW);   
      digitalWrite(ledPin2, HIGH);
      digitalWrite(ledPin3, LOW);

      delay(100);

      digitalWrite(ledPin3, HIGH);
      digitalWrite(ledPin2, LOW);

      delay(100);
  }

}

The reason why your code doesn't work, is once you are within a do while loop, you will not be allowed to exit until the condition is met. If you are not scanning for more serial values within that do while, you will get stuck there forever.
Another reason why you might be confused, is how digitalWirte operates. There is no need to loop over a digitalWrite, as the pin will stay HIGH, or LOW until you change its state. Once you digitalWrite(HIGH) that pin will stay high until it is told to go LOW.

8715: <p>Hell0!</p>

<p>I run in small problem. I am try to control LED's by serial port.So I am expect to send "1" and two LED's will start to blink or send "0" and only one LED is constantly lighted up. At first I thought that do-wile will work, Arduino will run do loop while value sent by serial port is "1" or "0". However at first I sent "0" and one LED are lighted, after I send "1" and two LED's are blinking. But when I send "0" again, LED's keeps blinking. What should I change in my code?</p>

<pre><code>const int ledPin2=2;
const int ledPin3=3;
const int ledPin4=4;

void setup()
{

  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(ledPin4, OUTPUT);

 Serial.begin(9600);
}

void loop() 
{
  while (Serial.available()==0);

  int serialVal = Serial.read()-'0';


  do
   {
      digitalWrite(ledPin3, LOW);
      digitalWrite(ledPin4,HIGH); 
    } while(serialVal==0);

  do
    {
      digitalWrite(ledPin4,LOW);   
      digitalWrite(ledPin2, HIGH);
      digitalWrite(ledPin3, LOW);

      delay(100);

      digitalWrite(ledPin3, HIGH);
      digitalWrite(ledPin2, LOW);

      delay(100);

  }while (serialVal==1);
};
</code></pre>
 8716: <p>I don't think you understand how your code is being executed. I would recommend trying the following.</p>

<pre><code>const int ledPin2=2;
const int ledPin3=3;
const int ledPin4=4;
int serialVal;

void setup()
{

  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(ledPin4, OUTPUT);

 Serial.begin(9600);
}

void loop() 
{
  // If data is available, then read it!
  if (serial.available() &gt; 0) 
  {
      serialVal = serial.read()-'0';
  }

  // If serial val is 1, turn on LEd4, turn off Led3
  if (serialVal == '1')
  {
      digitalWrite(ledPin3, LOW);
      digitalWrite(ledPin4,HIGH); 
  }

  // If serial val is 0, 
  // turn on LED2. Turn off Led3
  // delay for 100ms
  // turn of LED2, turn on LED3
  // delay for 100 ms
  if (serialVal == '0')
  {
      digitalWrite(ledPin4,LOW);   
      digitalWrite(ledPin2, HIGH);
      digitalWrite(ledPin3, LOW);

      delay(100);

      digitalWrite(ledPin3, HIGH);
      digitalWrite(ledPin2, LOW);

      delay(100);
  }

}
</code></pre>

<p>The reason why your code doesn't work, is once you are within a do while loop, you will not be allowed to exit until the condition is met. If you are not scanning for more serial values within that do while, you will get stuck there forever.</p>

<p>Another reason why you might be confused, is how digitalWirte operates. There is no need to loop over a digitalWrite, as the pin will stay HIGH, or LOW until you change its state. Once you digitalWrite(HIGH) that pin will stay high until it is told to go LOW.</p>
