Arduino LED blinking -> need some clarification and explanation

Viewed 232

So, me and my friend were assigned a task: When 'A' is given as input to serial monitor, the led must blink once. When 'B' is given a input, the led must blink continuously. And we successfully finished the task using the code attached.

char a;
void setup()
{
  pinMode(13, OUTPUT);
  Serial.begin(9600);
}

void loop()
{
  if(Serial.available())
  {
    a= Serial.read();
    if(a == 'A')
    {
      digitalWrite(13, HIGH);
      delay(1000); // Wait for 1000 millisecond(s)
      digitalWrite(13, LOW);
      delay(1000); // Wait for 1000 millisecond(s)
    }
  }

  if(a=='B')
  {
    digitalWrite(13,HIGH);
    delay(500);
    digitalWrite(13,LOW);
    delay(500);
  }
}

But then we have a doubt to be clarified. Both the conditions are given inside loop , but the one inside the if(Serial.available()) condition makes the led blink once while the condition outside the if(Serial.available()) makes the led blink continuously. Why?? Note that the code is the same for if(a=='A') and if(a=='B'). I really need an explanation for this question.

3 Answers

if(a=='B') will be executed continuously since it stays true until new input is received and it is in loop that is being called repeatedly.

if(a == 'A') is only evaluated once after a new character is received. If there is no character it is never checked again.

loop is called repeatedly regardless of whether there is new input available. variable a have global scope so it will not change between calls. It will contain the last received input.

In your code, the character held by a is only changed when Serial.available() returns a non-zero value, which only happens when you press a key. In the API Reference, we can see that Serial.available() actually returns the number of characters available, so the recommended usage is if (Serial.available() > 0).

To get the intended behavior (if I've understood your problem correctly) you should move the if 'B' part to within the if available() part. Something like this

void loop()
{
  if (Serial.available() > 0)
  {
    a = Serial.read();
    if (a == 'A')
    {
      digitalWrite(13, HIGH);
      delay(1000); // Wait for 1000 millisecond(s)
      digitalWrite(13, LOW);
      delay(1000); // Wait for 1000 millisecond(s)
    }
    else if (a == 'B')
    {
      digitalWrite(13,HIGH);
      delay(500);
      digitalWrite(13,LOW);
      delay(500);
    }
  }
}

When you enter something in the Serial Monitor and press enter, then Serial.available() returns a true, Then you read the buffer, in this case it is either A or B, and then you assign the variable a to the character that is read.

You should note that Serial.available() returns true only when you press enter, so when yo press enter to submit your input, the code under the if condition is executed, and that's why the led blinks only once, because after that Serial.available() returns false.

Since you are assigned the variable c under the Serial.available() condition, it remains whatever is read in the serial monitor. that's why when you enter B, c == 'B' is always true and the led blinks continuously.

Related