; BY JOE PARDUE SMILEY’S WORKSHOP
The Arduino Classroom
Arduino 101 — Chapter 4:
Digital Input ... Pushbuttons
Next, we use the if/else statements to turn the LED on
if the pushbutton state is HIGH, and turn it off if it is LOW.
Computers use LEDs to tell us something just as often as we use a pushbutton to
tell the computer something. An alarm clock buzzes and the LEDs tell us what
time it is. We then push a button to tell the alarm clock that we want to sleep
another few minutes (snooze button) or maybe we’re ready to get up, so we push
the alarm off button. These two buttons tell the computer two different things.
The snooze button tells it to turn off the alarm and set a new alarm for some time
in the future. The alarm off button tells the computer to turn the alarm off and
reset the new alarm time for 24 hours in the future. The alarm clock has a built-in
microcontroller (not unlike the one on the Arduino) that turns the LEDs on and off
to show you the time, and reads the buttons to learn what you want it to do
next. In this chapter, you will learn how to design circuits using pushbuttons and
how to utilize them to get user input with Arduino software that will let your
system take actions when a button is pressed.
The Arduino provides several ways for a program to
make decisions. One of these is to pose the question: “if
this is true do this, else do that.” It examines a statement
to see if it is true. If it is true, then it does one thing; if that
statement is not true, then it does something else. The
question is posed in code as follows:
Before we get into that, let’s learn another couple of Arduino C programming concepts that we’ll use when testing pushbuttons. We’ll look at decision-making using the if-else conditional flow control construct.
Then, we will learn to use do-while, which is similar to the
while flow control construct we saw last month. Next, we
will learn how to use the Arduino function millis() to do
some event timing. Finally, we will learn about ‘=’ and ‘==’
— the two C operators that surprisingly aren’t equal.
We will apply this knowledge in our labs where we’ll
learn how to get the Arduino to detect a button push and
use that information to control LED states. For our last
exercise, we will bring it all together along with the millis()
function to create an Arduino-based reaction timer.
if (statement is true) { // do this; } else { // do that; }
How quick can you get your finger off a pushbutton
after an LED turns on? Well, by the end of this chapter
you will know!
In this chapter’s lab’s pushbutton LED examples, we
ask the question: “Is the button pushed?” which we can
determine by looking at the Arduino pin the pushbutton is
connected to and seeing if it is HIGH. If it is true that the
pin is HIGH, then we turn the LED on. If it is not true,
then we turn the LED off as follows.
First, get the pushbutton state HIGH or LOW by using
the digitalRead function:
More Decisions: if-else
pushButtonState = digitalRead(pushButtonPin)
56 April 2014