Monday, February 13, 2017

The Arduino if Statement

In this part of the Arduino programming course, the if statement is used to show how decisions can be made in a sketch.
An if statement is used to check for keyboard input to the Arduino that a user types into the Serial Monitor Window of the Arduino IDE.
Further decisions can be made, depending on which key the user presses, for example, if the '1' key is pressed, the on-board LED of the Arduino can be switched on and if the '0' key is pressed, the LED can be switched off while all other key presses are ignored.

Using the if Statement

The sketch below shows the if statement being used in a sketch to check if a character has been sent from the Arduino IDE serial monitor window.
void setup() {
  Serial.begin(9600);
}

void loop() {
  char rx_byte;
  
  if (Serial.available() > 0) {    // is a character available?
    rx_byte = Serial.read();       // get the character
    Serial.print("You typed: ");
    Serial.println(rx_byte);
  }
}
If the user sends a character from the serial monitor window, the Arduino sends back the text "You typed: X", where X is the character that the user typed.
The video below shows the sketch running.

How the Sketch Works

Arduino if Statement Structure

The structure of an if statement is shown here:
if (conditional expression) {
  Body of if statement
}
If the conditional expression evaluates to true, the code in the body of the statement is run. If the conditional statement evaluates to false, none of the code in the body of the if statement will be run.

Checking for a Character from the Serial Monitor Window

In the example sketch, the value that Serial.available() returns will only be greater than 0 if one or more characters has been received by the Arduino from the serial monitor window.
The if statement will only evaluate to true if one or more characters has been received.
When no characters have been sent to the Arduino, the if statement will be evaluated each time through the main loop and evaluate to false each time. The code in the body of the if statement will therefore not be run.
As soon as a character is sent from the serial monitor window, Serial.available() returns 1. When the if statement is evaluated again, it evaluates to true (because 1 > 0) and the code in the body of the if statement is run.
The character that was sent from the serial monitor window is the stored in the rx_byte variable using the following line of code.
rx_byte = Serial.read();       // get the character
Because the character has been stored, it can now be sent back to the serial monitor window with some preceding text.

Using the if Statement to Switch On an LED

The if statement can be used to make a decision to switch on an LED if a certain character is received as this next example shows.
void setup() {
  Serial.begin(9600);
  pinMode(13, OUTPUT);  // LED on pin 13 of UNO
}

void loop() {
  char rx_byte;
  
  if (Serial.available() > 0) {    // is a character available?
    rx_byte = Serial.read();
    if (rx_byte == 'a') {
      digitalWrite(13, HIGH);
    }
  }
}
In this sketch, if the character sent from the serial monitor window is 'a', then the LED on pin 13 of the Arduino will be switched on. If any other character is sent, nothing will happen.
The if statement:
if (rx_byte == 'a')
will only evaluate to true if the character received from the serial monitor window and stored in the variable rx_byte is equal to 'a'.

Switching the LED Off Again

The above sketch can be modified to add another if statement that can be used to switch the LED off again.
The sketch below will switch the LED on if 'a' is sent to the Arduino and switch the LED off if 'b' is sent to the Arduino (note that these are case sensitive, so 'A' and 'B' won't work).
void setup() {
  Serial.begin(9600);
  pinMode(13, OUTPUT);  // LED on pin 13 of UNO
}

void loop() {
  char rx_byte;
  
  if (Serial.available() > 0) {    // is a character available?
    rx_byte = Serial.read();       //
    if (rx_byte == 'a') {
      digitalWrite(13, HIGH);
    }
    if (rx_byte == 'b') {
      digitalWrite(13, LOW);
    }
  }
}
Now it the character 'b' is sent, the if statement testing for 'a' evaluates to false, but the if statement testing for the character 'b' evaluates to true and the LED is switched off.
A more efficient way of dealing with switching the LED on and off is dealt with in the next part of the course which covers the else and else if statements that are used with the if statement.

No comments: