Arduino Programming: Conditional Statements

Objectives and Overview

This lesson introduces control flow and conditional statements. Additionally, this lesson includes examples of basic conditional statements and the role of assignment and comparison operators in programming.

Lesson Objectives

  • Understand and explain how conditional statements help programs make decisions.
  • Explain the purpose of assignment operators and comparison operators in conditional statements.

Control Flow in Arduino Programming

The term control flow may seem daunting at first but at its core its essentially a group of statements and structures that govern the interactivity and processes of programs. You’ve already worked with one type of control flow in your Arduino experimentation- functions.

The Block Statement

The first, and most basic, control flow statement is the block statement. You’ve actually already integrated these into your code when you’ve worked with functions. Consider this code contained within the void loop() function (you’re going to have this code memorized by the end of the course!):

void loop() { digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level) delay(1000); // wait for a second digitalWrite(13, LOW); // turn the LED off by making the voltage LOW delay(1000); // wait for a second }
Code language: JavaScript (javascript)

With regard to the above example, you can think of the block statement as containing, or controlling, the code within the function. In this particular example, this would be the code that controls the on/off messages of the LED as well as the delays.

Code contained within { } is intentionally chunked together.

Conditional Statements

Conditional statements are one of the core pillars of programming, whether it be for the web, video games or other applications. Developing a practical and functional understanding of working with conditional statements will greatly enhance the level of interactivity that you’re able to develop and include in your code.

Let’s take a look at two examples. The first demonstrates a concept, and the second is a snippet of functional Arduino code.

Example One:

if (x > 10) { // if true, do these things } else { // if false, do these things }
Code language: JavaScript (javascript)

In this example, the if statement evaluates a condition that is contained with the ( ) following the if. The statement that is within the ( ) needs to be able to be evaluated as a Boolean value, meaning it must either evaluate to true or false.

In the above example, the statement would evaluate as true if the value of the variable x is greater than 10. If this returns true, then the code within the first set of { } is executed.

This particular form includes an else { } block statement as well. This is the code that executes if the condition is false. Since the statement contained within the ( ) is Boolean, meaning it is either true or false, only one block statement of code will execute.

Example Two:

This is a snippet of code using a concept that you’ll integrate into an upcoming project.

if (buttonState == HIGH) { digitalWrite(ledPin, HIGH); } else { digitalWrite(ledPin, LOW); }
Code language: JavaScript (javascript)

This example is checking to the state of a button to determine whether it is on or off. The setup is similar to the first example, as it is checking an expression inside the ( ).

The if statement checks the buttonState variable to see if it is in a HIGH (on) state, and if that condition evaluates as true, then a digitalWrite message is sent to the board to turn on the LED.

In the else { } statement, which will execute when buttonState is NOT equal to HIGH, then sends a message to turn the LED off.

Notice the inclusion of variables in the above examples. Variables and conditional statements are commonly used together to execute commands that are determined by checking a particular state.

Now that you have an understanding of the structure of conditional statements, it’s time to introduce another key concept, operators.

Basic Operators

This section includes some of the basic operators that you’ll use in your Arduino projects. There are lots of resources available online, particularly on the Arduino Reference Page for a more in-depth list if you’re wanting to develop a deeper understanding of this tool.

Assignment Operators

You’ve already worked with the first type of operator, which is the assignment operator. These are operators that assign a value to something on the left based on the evaluation of what’s on the right. Here are some examples:

  • char placeholderName = 'Dave';
  • int x = 5;
  • int result = x + y;

Comparison Operators

Another type of operator is the comparison operator. This was used in the example above where the value of a “name” variable is checked to see if it is empty. These are commonly used in if…else statements because they return true or false based on the evaluation. Many of these are the same as what you’ve likely used in math classes. Here are some examples:

  • Equal (==): This returns true if the values are equal.
  • Not equal (!=): This returns true if the values aren’t equal.
  • Greater than (>): This returns true if the value on the left is larger than the value on the right.
  • Greater than or equal to (>=): This returns true if the value on the left is larger or equal to the value on the right.
  • Less than (<): This returns true if the left value is less than the right value.
  • Less than or equal to (<=): This returns true if the value on the left is less than or equal to the value on the right.

One thing that you should remember is that when evaluating equality within conditional statements, you want to use the (==) operator. If you use only a single (=), Arduino thinks that you’re attempting to assign value and it can become confused about how to handle the statement.

You’ll encounter other operator types. Focus on developing an understanding of how operators are used with conditional statements.