The Arduino Inventor's Guide (48 page)

BOOK: The Arduino Inventor's Guide
7.73Mb size Format: txt, pdf, ePub
Test and Troubleshoot

Finally, rest the end of your race track without the photoresistor on the starting tower so that the ramp extends past the tower by about the length of your toy car (see
Figure 9-26
). This will be the starting position.

FIGURE 9-26:
Finished Drag Race Timer with car ready to launch

If you haven’t already done so, plug the Arduino back into your computer or into a power source. Push the reset button to make sure that the sketch starts over, and find your favorite Matchbox or Hot Wheels car and set it behind the starting gate. Push the starting button and watch your car go!

How long did it take to reach the bottom? On our track, our toy cars took just over 0.5 seconds. Try a few different cars, or invite some friends over to see whose car is the fastest. If you tape a few pennies to your car, does it go faster? Experiment and see how different things affect the drag race time of your car.

GOING FURTHER

In this project, we introduced you to using the LCD to display information directly from your Arduino sketch. Here are a few ideas of how you can take what you’ve done in this project further.

Hack

Racing against yourself is only so much fun. Let’s look at how to add a second track and have two cars race against each other. (See
Figure 9-27
.) For this hack, you’ll need an extra photoresistor, which isn’t included in the standard SparkFun Inventor’s Kit. Find a friend who also has a kit, buy one separately, or find one in the additional parts kit for this book.

First you need to create a separate finish-line sensor circuit. We were able to sneak in one more photoresistor and a pull-down resistor circuit near the bottom of the breadboard, as shown in
Figure 9-28
. Connect the second photoresistor circuit to pin 2 on
the Arduino through a 10 kΩ pull-down resistor, and connect the other leg to the 5 V power rail.

Place the photoresistor in the second track, and add male-to-female jumper wires to connect this to your circuit on the breadboard. Set your second track next to the first one on the starting tower. Now, it’s time to upload some new code that will use both sensors. You only need to add a few extra lines to allow for the two cars to race. Download the
P9_TwoCarDragRaceTimer.ino
file from
https://www.nostarch.com/arduinoinventor/
and open it.

Let’s take a look at the additions to this code. First, the code adds a new constant and variable for the second photoresistor finishline sensor,
finishSensor2Pin
and
finishSensor2
.

Then, it checks which sensor was crossed first using a compound
if()
statement. If car #1 crosses first,
finishSensor1
will be
0
and
finishSensor2
will still be
1
. Inside this
if()
statement, instructions display the winning information to the LCD and set the state variable,
finishFlag
, to
true
.

The
else-if()
statement checks whether car #2 crosses the finish line first; in this case,
finishSensor2
will be
0
and
finishSensor1
will still be
1
. In the unlikely event that both cars do actually cross the line at the same time, this code does nothing. See if you can figure out how to add a draw feature in the event of a tie.

The code is full of comments to help explain more. Now, upload the code to your board and race! Whose car is fastest?

FIGURE 9-27:
Drag Race Timer with two race tracks

FIGURE 9-28:
Adding a second photo-resistor for racing two cars at the same time

Modify

Now that you’ve seen how to use the LCD circuit, try going back and adding an LCD to one of the projects you’ve already built. In any of the projects where you used the Serial Monitor to display information, like the Reaction Timer from
Project 4
or the Tiny Desktop Greenhouse in
Project 7
, you can replace the Serial Monitor with the LCD.

You’ll need to check the wiring and the pin configuration used; you’ll need six GPIO pins from your Arduino to control the LCD. If you want to see an example of the Reaction Timer project that uses the LCD, take a look at the tutorial we created on our InventorSpace at
https://invent.sparkfun.com/cwists/preview/1145-sik-lcd-reaction-timer/
.

10
Tiny Electric Piano

In this project, you’ll use a special touch sensor and a piezo buzzer to create an Arduino piano (
Figure 10-1
). Regardless of whether you have any musical talent, this will be a fun project!

FIGURE 10-1:
A completed Tiny Electric Piano

MATERIALS TO GATHER

There are only a couple new parts in this project. One is the
soft potentiometer (SoftPot)
, which will act as your keyboard, and the other is the
piezo buzzer
, which will provide the sound. The supplies you’ll need are shown in
Figures 10-2
and
10-3
. Grab your materials, and let’s get started.

Electronic Parts

• One SparkFun RedBoard (DEV-13975), Arduino Uno (DEV-11021), or any other Arduino-compatible board

• One USB Mini-B cable (CAB-11301 or your board’s USB cable)

• One solderless breadboard (PRT-12002)

• One 10 kΩ resistor (COM-08374, or COM-11508 for a pack of 20)

• One 50 mm SoftPot membrane potentiometer (SEN-08680)

• One piezo buzzer (COM-07950)

• Male-to-male jumper wires (PRT-11026)

NOTE

All of the parts used in this project are standard in the SparkFun Inventor’s Kit.

FIGURE 10-2:
Components for the Tiny Electric Piano

Other Materials and Tools

• Pencil

• Craft knife

• Metal ruler

• (Optional) Soldering iron

• Masking tape

• Cardboard or cardstock (about 4 × 5 inches) or a small cardboard box

FIGURE 10-3:
Additional supplies

NEW COMPONENTS

As we mentioned, this project will introduce just two new parts. The first is a SoftPot touch sensor: a special kind of potentiometer that is similar to the one used in
Project 6
but reacts to pressure. The second new part is a piezo buzzer. Let’s take a look at these two parts in more detail.

The SoftPot Membrane Potentiometer

In
Project 6
, we introduced a simple potentiometer with a knob that you could turn to control the Balance Beam. The SoftPot, shown in
Figure 10-4
, works similarly but reacts to pressure instead of knob rotation.

FIGURE 10-4:
A 50 mm SoftPot

The SoftPot is a thin and flexible sensor that can detect where along its length pressure is applied. When you press down, the resistance between the middle pin and the closest end pin varies between 0 Ω and 10 kΩ, depending on how much pressure is detected. The SoftPot sensor has a thin membrane that separates the center pin connection from the outer ones. This sensor is very accurate and has a nearly infinite resolution. In industrial applications, the SoftPot is often used to identify the position of sliding parts, robot arms, and other components that make precision movements.

In this project, you’re going to use this sensor as your piano keyboard. You’ll divide the length of the strip into eight sections or “keys” that you can use to play various notes. The SoftPot is effectively the same as a knob potentiometer used in earlier projects. In schematics and circuit diagrams, you may recognize the symbol as pictured in
Figure 10-5
.

FIGURE 10-5:
The schematic diagram for the SoftPot is identical to that of the regular potentiometer.

The Piezo Buzzer

The piezo buzzer (
Figure 10-6
) is similar to a speaker and produces an audible click when you apply a voltage to the two leads; these clicks happen very fast, several hundreds or even thousands of times per second, and their frequency creates a tone. Inside a typical piezo buzzer is a special crystal called a
piezo element
that deforms when voltage is applied. The crystal is connected to a round metal disc, and when the crystal deforms, the disc vibrates the air inside a small cylinder, producing the clicking sound.

FIGURE 10-6:
Piezo buzzer

NOTE

The buzzer that’s included in the SparkFun Inventor’s Kit actually uses a small magnetic coil instead of a piezo element, but it works the same. We still refer to it as a piezo buzzer.

You’ve already seen how you can blink an LED at various rates with the Arduino. Now, if you “blink” the buzzer at a rate of hundreds of times per second, we can convert these clicks into tones! There are several different electrical symbols used to represent buzzers, speakers, and similar elements. The symbol we’ll use in this chapter is shown in
Figure 10-7
.

FIGURE 10-7:
Electrical symbol for a piezo buzzer

BUILD THE CIRCUIT

This circuit uses only two electronic elements: the buzzer and the SoftPot, shown in
Figures 10-8
and
10-9
. When inserting the buzzer into the breadboard, you may notice that the legs are slightly closer together than the holes of the board. Just gently bend the legs apart so that the pins line up with the holes and insert the buzzer. The legs should be three holes apart.

The SoftPot has three legs like the potentiometer you used in
Project 6
. But unlike a regular potentiometer, the SoftPot requires a
pull-down resistor to ensure that, when there is no input or pressure on the sensor, the SoftPot defaults to a nominal state of 0. This will prevent the buzzer from making noise when you aren’t pressing on the sensor.

FIGURE 10-8:
Schematic diagram of the Tiny Electric Piano circuit

FIGURE 10-9:
Prototype of the Tiny Electric Piano circuit on the breadboard

Other books

Rajasthani Moon by Lisabet Sarai
Wishful Thinking by Lynette Sofras
A Husband in Time by Maggie Shayne
Web and the Rock by Thomas Wolfe
The Last Empress by Anchee Min
Still thicker than water by Takerra, Allen
More Than Enough by John Fulton
White Riot by Martyn Waites