The Arduino Inventor's Guide

The Arduino Inventor's Guide by Unknown Page B

Book: The Arduino Inventor's Guide by Unknown Read Free Book Online
Authors: Unknown
Ads: Link
pushes straight down to bridge that gap. Find a push button in your supplies for this project and examine it. Even though the schematic symbol in Figure 4-5 shows only two contacts, most standard push buttons for breadboards have four legs. Figure 4-6 shows a more accurate illustration of the contacts inside, along with how a button like that might look on the breadboard. When you plug one in, the legs should straddle the ditch in the middle of the breadboard.
    FIGURE 4-6: Push button schematic and button correctly placed on a breadboard with legs straddling the ditch

    Push buttons are fantastic inputs in projects, because everyone knows how they work. Push buttons are also pretty simple to connect in a circuit with an Arduino. Let’s look at how that works.
Using Resistors with Push Buttons
    To use any button as an input to an Arduino, you’ll need to use a
pull-up resistor circuit
like the one in Figure 4-7 . A pull-up resistor connects to a power source on one side and to an input component (like a button) on the other. The part of a circuit that needs to detect input is connected at the intersection of the resistor and the button.
    In the configuration shown in Figure 4-7 , the resistor to 5 V
pulls
the Arduino pin’s default voltage
up
to 5 V, which is considered HIGH . When the button is pushed, a path is created between the Arduino pin and ground, and the Arduino pin reads a LOW voltage. This works because current always flows along the path of leastresistance: when the button isn’t pressed, the Arduino pin 10 kΩ resistor is the only path the current can access, but when the button
is
pressed, it offers a path with effectively zero resistance.
    FIGURE 4-7: Pull-up resistor and push button circuit

BUILD THE REACTION TIMER PROTOTYPE
    The Reaction Timer combines an LED circuit similar to the ones in previous projects with the button circuit from Figure 4-7 to make the supercircuit in Figure 4-8 , which lights an LED and detects button presses.
    FIGURE 4-8: Schematic diagram for the Reaction Timer prototype

    Take out your breadboard and wire up a single LED and a button, as shown in Figures 4-9 and 4-10 . You’ll use this prototype to test your code before building the final Reaction Timer.
    FIGURE 4-9: Wiring diagram for the Reaction Timer circuit

    FIGURE 4-10: Final prototype circuit of the Reaction Timer with a single button and a single LED

    As you connect this circuit, note the two different resistance values: 330 Ω for the LED and 10 kΩ for the push button. (See “ Resistors and Bands ” on page 308 for details on how to determine the value of a resistor from its color bands.) The resistor on the LEDis a current-limiting resistor and should be tied to ground, while the resistor on the push button is a pull-up resistor connecting pin 3 to 5 V. But your circuit can’t do anything without the code, so let’s look at that now.
PROGRAM THE REACTION TIMER
    As your sketches and circuits become more complex, you’ll find it helpful to organize your thoughts by listing each action you want the Arduino to take, in the order you want it to happen. Some programmers refer to a list like this as
pseudocode
. Here’s our pseudocode for the Reaction Timer:
    1. Wait a random amount of time before turning on the LED (to prevent predicting/gaming the Reaction Timer).
    2. Turn on the LED.
    3. Record the starting time.
    4. Start a timer and wait for a button press.
    5. When the button is pressed, calculate the reaction time as the timer value minus the starting time.
    6. Report the time back.
    Pretty simple, right? Let’s open up Arduino and look at the sketch.
Write the setup() Function
    Open a new sketch and type the initialization and the setup() code shown in Listing 4-1 .
    LISTING 4-1: setup() and initialization code for the Reaction Timer
    ➊ unsigned int waitTime;    //random wait time before
                            //turning on LED
   unsigned int startTime;   //zero

Similar Books

Mad Cows

Kathy Lette

Inside a Silver Box

Walter Mosley

Irresistible Impulse

Robert K. Tanenbaum

Bat-Wing

Sax Rohmer

Two from Galilee

Marjorie Holmes