0

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);
};
1

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.

  • Okay, I just tried your code. If 1 sent by serial then LED3 will light up (HIGH) and will continue to stay on HIGH. After I sent 0 and LED1 lights up and after delay 100ms LED1 turns off and LED2 turns on, but that is all it is not continuing to repeat blinking. Is it possible to loop within last IF statement of your code, so blinking will continue? – Austris Apr 27 '15 at 19:54
  • That is because serial.available() is a blocking method, when you nest it into a while statement This means that it will not continue in the code until a serial value has been received. Try this code gist.github.com/ericellb/12d7337396f6646ef3f8 – Eric E Apr 27 '15 at 19:58
  • If you need anything else, id be glad to help. – Eric E Apr 27 '15 at 20:09
  • 1
    This is a good answer, but be careful of your wording. "you will not be allowed to exit until the contition is met", implies break statements don't exist. "serial.available() is a blocking method, when you nest in a while statement", serial.available() does not becoming a blocking method in a while loop, the loop is blocking because of its return value. – BrettAM Apr 28 '15 at 1:11
  • Good points BrettAM. – Eric E Apr 28 '15 at 1:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.