The ATG5_Button sketch’s loop

Now that we understand what digital inputs do, let’s take a look at the ATG5_Button sketch’s loop code.

ch5-loop-button-code-2-01
First, let’s look at the line at the top of the code.

Exploring the loop

In the first line of the ATG5_Button sketch’s loop section (after the void loop() { bit and the comment), the Arduino uses a function called digitalRead to check whether a pin is on or off. In this case, we are checking the pin represented by our buttonPin variable, so we are evaluating the state of pin 2. The results of our digitalRead function will either be a value of 1 (HIGH) or 0 (LOW). We then set our variable named “buttonState” to this value.

ch5-code-buttonstate-labelled-01

The next part of the loop code gives us another new programming concept: the use of conditional statements.

What is a conditional statement?

Conditional statements are a powerful way to change what happens within our code depending on conditions we specify, such as whether a button is on or off. You have experienced the use of conditional statements in everyday language:
ch5-if-statements-01

Conditionals in programming work the same way. They have three basic parts: the “IF,” the expression we are evaluating, and what we want to happen if our statement is true. Let’s take a look at conditional statements in our loop code.

Below is the first part of the conditional statement within our loop code for HFA5_Button. In some sketches, this could be your whole conditional statement; ours happens to have a second part–you’ll learn about it in a couple of pages.

ch5-code-if-statement-1-01

This part of the conditional statement includes the “IF,” the expression we are evaluating, and what happens if our expression is true. Everything that will happen, if the statement is true, is contained within a set of brackets. We, as the programmer, get to tell the program what to do if certain situations happen.

Conditional statements in our loop

Conditional statements start with an “if.” The “if” tells the computer that we are going to evaluate the next expression.
ch5-code-if-statement-2-01

The next part of the conditional statement is the condition to be evaluated. This is a section of code which the Arduino has to assess for truth. “True” in a programming context means that the condition is logically valid. For example, the english statement “One is equal to one” doesn’t tell us anything interesting, but it is true. The nonsensical statement “two plus two equals five” is false. We will see various types of conditional statements throughout the book that evaluate what is happening with our Arduino and the rest of the circuit.

In the case of this sketch, the code is trying to evaluate whether the button is currently pressed. (Remember, pressed means “on”, which is the same as HIGH in the Arduino programming language.) To test whether a value is equal to another value we use two = signs, or ==.

Conditional statements start with an IF.
ch5-if-statement-equality-testing-01

The last part is the “true” code block, the commands that are run if the condition is true. There is no limit to the number of actions you can include inside of the true code block, as long as they are all contained within the brackets. In this case the code block will turn on the LED attached to the ledPin, also known as pin 13.

Conditional statements check for “truth”.
ch5-code-if-statement-3

State out loud what you want your conditional statement to do.

Conditional statements: else

What happens if the button is NOT pressed? For this conditional statement, there is an “else” clause, which handles any events which happen when the statement is not true. “Else” is helpful for dealing with cases where the if statement is false, but are not required for every conditional statements. Some conditionals have an else, and some do not. If this conditional didn’t have an else, then if the condition we’re evaluating was false, nothing would happen.

For our button code, the else statement can also be broken down into a simple english statement. “If the button is not pressed, then turn the light off. “
ch5-code-else-01
Else is not required in all conditional statements.

Here’s a quick summary of the conditional statement we just examined:

What is happening in the circuit? Condition to be evaluated Truth Value Result
button is pressed if (buttonState == HIGH) true turn LED ON
button is NOT pressed if (buttonState == HIGH) false turn LED OFF

Questions?

Q: What if I want more than two possible outcomes?

A: Then you might use an else if, or maybe even multiple else ifs. We’ll cover this later on when we require more complicated chains of logic.

Q: Can I place a conditional inside of another conditional?

A: Yes, we will see examples of what’s called a nested conditional later on in the book as we learn more about evaluating more complicated logic.

Now that we’ve connected our button and made it turn the LED on and off, we are ready to make our circuit more interesting. Let’s add a speaker, then add some code so the speaker plays a tone when we press the button. First we’ll see how to add the speaker to the breadboard.

Add a speakerDigital input

Leave a Reply

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