The Arduino Inventor's Guide (43 page)

BOOK: The Arduino Inventor's Guide
4.56Mb size Format: txt, pdf, ePub

As we mentioned earlier, the chassis template includes a notch on the front end to place a marker. We suggest using either a washable or dry-erase marker. Find a large piece of poster paper or dry-erase board that you can lay on the floor. Be really careful not to draw all over your floor! It might get you in trouble. (Trust us, we’ve made this mistake before, and we definitely regret it.)

Place your Drawbot on the drawing surface. Use a piece of masking tape to secure the marker in place, positioning it so that it makes good contact with the drawing surface. Move the Drawbot
around manually on the drawing surface to test the marker placement. Now, plug the USB cable in to your computer and watch what happens. Be quick to grab the Drawbot if it looks like it’s going to run off your drawing surface and onto the floor.

To mix things up, change marker colors or modify your code to draw different size squares. See if you can make your Drawbot draw spirals and stars!
Figure 8-19
shows some of the fun patterns our Drawbot made in our office.

FIGURE 8-19:
Drawbot in action—make sure you use a large piece of paper to keep it from drawing all over the floor!

If you want to add some style to your Drawbot, try digging through your craft supplies or finding some spare cardboard and see what you can come up with. We placed an old pretzel jar container on our Drawbot at the office. It’s now called Pretzel Bot, and it drives around and gives away free pretzels (
Figure 8-20
).

FIGURE 8-20:
Pretzel Bot! The Arduino and breadboard are hidden inside the box.

GOING FURTHER

The Drawbot is an introduction to basic robotics. The simplest robots are really just a controller and two motors, and that’s what you have here. We’ll give you a few ideas to take it to the next level.

Hack

Preprogrammed motion is fun, but every time you want to change up the pattern, you have to reupload the sketch. But with some changes to the code, you can use the Serial Monitor to control your Drawbot while it’s still going.

So far, you’ve used the Serial Monitor only to read data the Arduino sends back to the computer as the project is working, such as sensor data, but the Serial Monitor can also send data to the Arduino. Open the Serial Monitor window, and you’ll see a small text box at the top with a button labeled Send, as shown in
Figure 8-21
. This box allows you to send data to the Arduino so you can control it from the Serial Monitor.

FIGURE 8-21:
Serial Monitor window

The
P8_DrawbotSerial.ino
sketch (downloadable from
https://www.nostarch.com/arduinoinventor/
) uses the Serial Monitor to send just three numbers to control Motor A, Motor B, and the driving time.

Take a look at the code. This new sketch declares three variables to hold the speed of Motor A, the speed of Motor B, and the delay time, which are the three numbers you send. You initialize communication with the Arduino with
Serial.begin(9600);
to send and receive data using the Serial Monitor. The Arduino reads the data you put into the Serial Monitor and assigns any integers to the three
speedA
,
speedB
, and
delayTime
variables, which are then used for the familiar
setMotorA()
,
setMotorB()
, and
delay()
functions. See the comments in the code for a more complete explanation.

Upload the sketch to your Drawbot, open the Serial Monitor, enter
100 -100 500
, and press
ENTER
or click
Send
(see
Figure 8-22
). The Drawbot should move forward for half a second and then stop. Now you can fine-tune your Drawbot routines without having to reupload the code each time! The code will run once and doesn’t repeat unless you give it additional commands. What happens when you enter six numbers, such as
100 -100 500 -100 -100 250
? See if you can choreograph a dance routine that is represented by a sequence of numbers.

FIGURE 8-22:
Choreographing a robo-dance

Modify

What other shapes can you program your Drawbot to create? Use what you learned with
for()
loops and see if you can hack your code to make it draw a triangle or star. You’ll have to do some experimenting to get the timing and speeds just right. What happens when you move just one wheel at a time?

Bonus

There’s a bonus script (at
https://www.nostarch.com/arduinoinventor/
) that will let you control the turtle with even simpler commands, like
fd 10
and
bk 10
to move forward or backward 10 spaces. Download
P8_BonusTurtle.ino
and load it into your IDE. Then open your Serial Monitor and enter a few of the following commands:
fd 10
to move forward 10 spaces,
bk 10
to move backward 10 spaces,
rt 90
to turn right 90 degrees, and
lt 90
to turn left 90 degrees. See if you can instruct your new turtle to do a square dance with these instructions!

9
Drag Race Timer

In
Project 4
, you built a reaction timer to measure how fast you can hit a button. In this project, you’ll build on the techniques you learned there to make a race timer for a Hot Wheels-inspired race track (see
Figure 9-1
). We’ll show you how to display the finish time on a small, portable LCD screen so that you can detach your
project from your computer. We’ll also show you how to hack this project to add a second track and indicator lamps (LEDs) to show which car has won. Are you ready?

FIGURE 9-1:
The completed Drag Race Timer

MATERIALS TO GATHER

Many of the parts used in this project will be familiar to you already (see
Figures 9-2
and
9-3
). We’ll introduce only one new part: the 16 × 2 character LCD that you’ll use to display your race time directly, rather than displaying it in the Serial Monitor on your computer screen.

NOTE

In “
Going Further
” on page
273
, we’ll show you how to modify your Drag Race Timer so you can race two cars and display the winning time. The standard SparkFun Inventor’s Kit includes one photoresistor (SEN-09088), but you’ll need two for this final hack. Thankfully, the photoresistor is a pretty inexpensive part, so you could either buy another one or partner with a friend who has an Inventor’s Kit to build the two-player version.

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, or two if you want to build the two-player version (COM-08374, or COM-11508 for a pack of 20)

• One photoresistor (SEN-09088), or two* if you want to build the two-player version

• One push button (COM-10302)

• One 10 kΩ potentiometer (COM-09806)

• One 16 × 2 character LCD (LCD-00255)

• One submicro size servo motor (ROB-09065)

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

• Male-to-female jumper wires (PRT-09140*)

NOTE

The parts marked with an asterisk (*) do not come with the standard SparkFun Inventor’s Kit but are available in the separate add-on kit.

FIGURE 9-2:
Components for the Drag Race Timer

Other Materials and Tools

• Craft knife

• Metal ruler

• Needle-nose pliers

• Wire cutters

• Masking tape

• Glue (hot glue gun or craft glue)

• Cardboard (about 8.5 × 11 inches), a small cardboard box, or thick cardstock

• Bamboo skewer

• Enclosure template (see
Figure 9-15
on page
266
)

• Hot Wheels or other small toy cars to race (not shown)

• (Optional) Toy car race track (not shown)

FIGURE 9-3:
Recommended tools and materials

In previous projects, we’ve used the Serial Monitor to display information sent from the Arduino on your computer. In this project, we’ll show you how to add an LCD directly to your project, a skill well worth learning. The LCD requires a lot of wires, but don’t worry— we’ll take it one step at a time. After you’ve mastered the use of this part, you can go back and add it to some of your past projects to make them fully portable!

NEW COMPONENT: THE 16 × 2 CHARACTER LCD

LCD
is short for
liquid crystal display
. Invented over 40 years ago, liquid crystal technology is used in digital watches, alarm clocks, projectors, televisions, computer monitors, and more.

The LCD you’ll use in this project is a simple
monochromatic
display, meaning it displays only one color. Beneath the screen of the display is a layer of liquid crystal. This is a unique chemical that, when a small electric current is applied to it, changes from transparent to opaque. Combined with a backlight or a reflective mirror, liquid crystal is used to build very simple displays. Light comes through or is blocked depending on which areas of the liquid crystal electricity is applied to—which means you can make shapes if you can control the current.

The 16 × 2 character LCD displays up to 32 characters of information, each of which is broken down into a 5 × 8 pixel matrix. Each individual pixel can be made either opaque or transparent depending on the applied electric current, controlled by the Arduino. The letter
A
,
for example, will display on the LCD screen when the yellow pixels in
Figure 9-4
are made opaque.

FIGURE 9-4:
The uppercase letter
A
represented on a 5 × 8 pixel matrix

There are 40 individual pixels in a single character, each controlled by the Arduino, meaning there are 1,280 different control lines! Thankfully, the LCD used in this project has a special parallel interface LCD driver IC by Hitachi called the HD44780. This chip allows you to display almost any character on the screen using just six control lines from the Arduino.
Figure 9-5
shows the pins on an LCD.

FIGURE 9-5:
A simple 16 × 2 character LCD

NOTE

The LCD can use up to eight pins for data (d0–d7), but the way we are going to use the LCD, it only uses four. These are labeled d4–d7.

The LCD has a total of 16 pins, but this project uses only pins 1–6 and 11–16. The pins are numbered 1 through 16 from left to right (with the pins at the top of the screen).
Table 9-1
describes each of the pins on the LCD. In some datasheets you might see a line over a label, as with the
label on pin 5. This line indicates that the feature is an
active low
, which means the pin is activated at low voltage. So, in this case, when you want to write
to the LCD, pin 5 needs to be set to
LOW
. We’ll discuss this more in “
Connect the Data and Control Wiring
” on page
255
.

TABLE 9-1:
Pin descriptions for 16 × 2 character LCD

PIN

DESCRIPTION

1

Ground (GND)

2

VDD power for the LCD (5 V)

3

Contrast adjust (0–5 V)

4

Register select (RS)

5

6

Enable

7–10

Data lines d0–d3 (not used)

11–14

Data lines d4–d7 (data transferred in 4 bits at a time)

15

Backlight power (5 V)

16

Backlight ground (GND)

Other books

La cantante calva by Eugène Ionesco
Safer With You by Trisha Madley
Broken by Christina Leigh Pritchard
The Death of Nnanji by Dave Duncan
The Inquisitor by Peter Clement