Arduino – If Statements

Welcome to If Statements! This is where you will learn to argue with your Arduino about whether or not a statement is true. If statements are very commonly used, get to know them well. Disclaimer: You will not win an argument with your Arduino, it always knows if a statement is true or not. This is similar to marriage, you may find yourself upset if you choose to argue with your Arduino.

What you need:

What is it?

An If Statement is a block of code that gets run depending on an statement you supply to it. If the statement is true, the block of code is run. If the statement is not true, it skips the block of code.

If you supply an else if statement, it will test that statement to verify if it’s true.

If that else if statement is false as well, and you supply an else statement, it will skip to the else statement and run that code block.

You don’t have to supply an else if statement, if your first if statement is false, it will go directly to else. You also don’t have to supply an else statement, if the first if statement is false, it would just not run the code at all without an else statement.

Uses

A good example of an if statement would be in a weather monitoring application. You could have a device monitoring the temperature, and if the temperature is below 32 degrees Fahrenheit, it will tell you ice is possible on the roads. An else statement for this could be an output stating that the roads should not have ice on them.

Another example would be when using an RFID tag. You could have a list of acceptable RFID tags, and if the one that is scanned belongs to the list, the door opens. An else statement for this would be a red blinking LED, that indicates a RFID tag that is not allowed access.

Usage

Lets look at the anatomy of an if statement.

The syntax from Arduino.cc looks like this:

if (condition)
{
  //statement(s)
}

Statement to be tested

Declaring the statement starts with “if ()”, you must have parenthesis here. In between the parenthesis, you put the condition that you’re testing. An example would be “if (x > 10)”. If x is 11, it would run the code. If x is 1, it would not run the code.

Code to be run

The second part of this is the curly braces { and }. This is where you put the code to be run. Anything outside these braces will be run, regardless of whether the condition is true. Inside the braces is code to be run if the statement is true.

If the If gets run, the others are ignored

The main thing to remember here, is that if any if statement equals true, the code gets run and any other part of the if statement is ignored. This means that it skips any else if, or else statements.

Examples

Simple predefined variable:

This example uses a predefined variable, which is an integer named age. This variable is declared right at the top of the following code. Here we’re trying to figure out if the person is old enough to drink. In this case the person is 14, so it should determine that the persons age is under 21, and run the else statement.

int age = 14;

void setup() {
 
Serial.begin(9600);//Initialize pc communication

}

void loop() {

if (age < 21) {
 Serial.println("You are not old enough to drink!");
}
else {
 Serial.println("You are 21 or older, you may have a drink.");
}
delay(5000);//wait 5 seconds
}

Let’s run the code, and see what we come up with. If you’re unsure how to upload code, check out my hello world post. Here is the output you should expect from the above code:

Success! It determined that we are unable to drink.

Let’s change the age variable to 24 now, and see what we get.

int age = 24;

The Arduino wins again! If statements are very useful when writing Arduino sketches. You’ll find yourself using them quite often. Let me know in the comments if you have any questions, and please subscribe for additional Arduino tips.

If you’re looking for something a little more versatile, you may try Arduino Switch Statements.

Join the newsletter

Subscribe

Subscribe to get our latest content by email.

We won't send you spam. Unsubscribe at any time. Powered by ConvertKit

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.