Make Arduino Bots And Gadgets Pdf Download

 Posted admin

Want to build your own robots, turn your ideas into prototypes, control devices with a computer, or make your own cell phone applications? It’s a snap with this book and the Arduino open source electronic prototyping platform. Embedded systems are everywhere - inside cars, children’s toys, robots, and mobile phones. Download Book Arduino Robotics in PDF format. You can Read Online Arduino Robotics here in PDF, EPUB, Mobi or Docx formats. Make Arduino Bots And Gadgets Author: Tero Karvinen. Gain the know-how and experience to invent your own cool gadgets. With Arduino, building your own embedded gadgets is easy, even for beginners.

  1. Make Arduino Bots And Gadgets Pdf Download Free
  2. Make Arduino Bots And Gadgets Pdf Download Pc

Make Arduino Bots And Gadgets Pdf Download Free

Pdf

Make Arduino Bots And Gadgets Pdf Download Pc

Make: Arduino Bots and Gadgets Learning by Discovery
Kimmo and Tero Karvinen with photographs and illustrations by the authors
Beijing • Cambridge • Farnham • Köln • Sebastopol • Tokyo
Make: Arduino Bots and Gadgets by Kimmo and Tero Karvinen Copyright © 2011 O’Reilly Media, Inc. All rights reserved. Printed in Canada. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly Media books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (my.safaribooksonline.com). For more information, contact our corporate/institutional sales department: 800-998-9938 or [email protected]
Development Editors: Brian Jepson and Brian Sawyer Production Editor: Holly Bauer Technical Editor: Joe Saavedra Copyeditor: Rachel Monaghan Proofreader: Jennifer Knight Translator: Marko Tandefelt Indexer: Ellen Troutman Zaig Cover Designer: Mark Paglietti Interior Designer: Ron Bilodeau Illustrator/Photographer: Kimmo Karvinen Cover Photographer: Kimmo Karvinen Software Architect: Tero Karvinen Print History: March 2011:
First Edition.
The O’Reilly logo is a registered trademark of O’Reilly Media, Inc. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and O’Reilly Media, Inc., was aware of a trademark claim, the designations have been printed in caps or initial caps. Important Message to Our Readers: The technologies discussed in this publication, the limitations on these technologies that technology and content owners seek to impose, and the laws actually limiting the use of these technologies are constantly changing. Thus, some of the projects described in this publication may not work, may cause unintended harm to systems on which they are used, or may not be consistent with current laws or applicable user agreements. Your safety is your own responsibility, including proper use of equipment and safety gear, and determining whether you have adequate skill and experience. Electricity and other resources used for these projects are dangerous unless used properly and with adequate precautions, including safety gear. These projects are not intended for use by children. While every precaution has been taken in the preparation of this book, O’Reilly Media, Inc. and the authors assume no responsibility for errors or omissions. Use of the instructions and suggestions in Make: Arduino: Bots and Gadgets is at your own risk. O’Reilly Media, Inc. and the authors disclaim all responsibility for any resulting damage, injury, or expense. It is your responsibility to make sure that your activities comply with applicable laws, including copyright.
This book uses Otabind™, a durable and flexible lay-flat binding. ISBN: 978-1-449-38971-0 [TI]
Contents
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1. Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
vii 1
Building Philosophy.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Reusing Parts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Buying Components.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Useful Tools.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Electronic Circuit Theory Review.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
2. Arduino: The Brains of an Embedded System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
17
Why Arduino?.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Starting with Arduino.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 Hello World with Arduino.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 Structure of “Hello World”.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 Arduino Uno.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Arduino Nano.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
3. Stalker Guard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 What You’ll Learn.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 Tools and Parts.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 Solderless Breadboard.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 Jumper Wire.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 Ping Ultrasonic Sensor.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 Vibration Motor.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 Combining Components to Make the Stalker Guard.. . . . . . . . . . . . . . . . . . . . . . . 41 Making the Motor Vibrate. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 Providing Power from a Battery.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 What’s Next?.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 Making an Enclosure.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 iii
4. Insect Robot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 What You’ll Learn.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 Tools and Parts.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 Servo Motors.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 Constructing the Frame. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 Programming the Walk.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 Avoiding Obstacles Using Ultrasound.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 What’s Next?.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
5. Interactive Painting . . . . . . . . . . . . . . . . . . . . . . . . . 79 What You’ll Learn.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 Tools and Parts.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 Resistors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 LEDs.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 Detecting Motion Using Ultrasonic Sensors.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 Moving Images.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 Installing Python.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 Hello World in Python .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 Communicating over the Serial Port.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 Displaying a Picture.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 Scaling an Image to Full Screen.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 Changing Images with Button Control.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 Gesture-Controlled Painting in Full Screen.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 Animating the Sliding Image.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116 Connecting Arduino with Processing.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 Processing Code for the Painting.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124 The Finished Painting.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128 Creating an Enclosure. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128 Building a Frame.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
6. Boxing Clock . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . What You’ll Learn.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Tools and Parts.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Android Software Installation.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Creating a Boxing Clock in Android.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . What’s Next?.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
7. Remote for a Smart Home . . . . . . . . . . . . . . . .
137 137 138 138 145 176
177
What You’ll Learn.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177 Tools and Parts.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178 The Relay: A Controllable Switch. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
iv
Contents
Hacking the Remote Control.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Controlling the Arduino from the Computer.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . Creating a Graphical User Interface.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The Finished Remote Control Interface.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Creating an Enclosure. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
181 184 190 192 195
8. Soccer Robot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199 What You Will Learn.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Tools and Parts.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Continuous Rotation Servos.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Modding a Standard Servo into a Continuous Rotation Servo.. . . . . . . . . . . Connecting the Arduino to the Bluetooth Mate.. . . . . . . . . . . . . . . . . . . . . . . . . . Testing the Bluetooth Connection.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Building a Frame for the Robot.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Programming the Movements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Controlling Movement from a Computer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Steering with an Android Cell Phone.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The Accelerometer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . An Easier Approach to Bluetooth.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Controlling the Robot with Cell Phone Motion.. . . . . . . . . . . . . . . . . . . . . . . . . . . Completing the Soccer Robot.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . What’s Next?.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
200 200 203 207 211 215 217 228 231 234 238 242 249 253 262
A. tBlue Library for Android . . . . . . . . . . . . . . . . .
263
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
269
Contents
v
Preface
In the early days, embedded systems were built primarily by engineers in a pretty exclusive club. Embedded devices and software tools were expensive, and building a functional prototype required significant software engineering and electrical engineering experience. With the arrival of Arduino, the open source electronics prototyping platform, things are cheaper and easier. The hardware is inexpensive (around $30), the software is free, and the Arduino environment is designed for artists, designers, and hobbyists rather than engineering professionals. The ultimate goal of this book is to teach you how to build prototypes using Arduino. We’ll offer just enough theory to help you apply your new skills to your own projects. You will also become familiar with the logic behind coding and components. We will explain every single line of code and tell you how each component is used. You will learn by completing actual projects, and the knowledge you gain will enable you to further develop your own ideas. Most books on embedded systems are either so specialized that you need to work within the particular field or too simplistic to be interesting. Books for beginners often just teach you to blindly follow instructions; here, we aim to promote a deeper understanding and a skill set that can be applied more flexibly. Finally, this book is meant for readers who want to learn how to build prototypes of interesting gadgets, not for those who want to build a dental X-ray machine or a microwave oven. At the same time, you will be able to apply the techniques covered in the book to make prototypes of commercial device concepts.
Embedded Systems Are Everywhere An embedded system is a microcontroller-based device designed for a very specific purpose. Some examples include washing machines, cell phones, elevators, car brakes, GPS devices, air conditioning units, microwave ovens, wristwatches, and robotic vacuum cleaners. Unlike the user interface you’re accustomed to with traditional computers, embedded systems typically do not include a display, mouse, and keyboard. Instead, you might control them via switches and foot pedals, for example.
vii
Embedded Systems Are Everywhere
Most embedded systems are reactive systems, operating in a continuous interaction with their environment and responding within a tempo defined by that environment. This makes them a logical choice for tasks that must react immediately, such as a car braking system. In some cases, it can be hard to tell whether a particular system should be classified as an embedded system or a computer. For example, cell phones are starting to include more and more features typically associated with computers, but they still have much in common with embedded systems.
Why Should You Study Embedded Systems? The world is already full of embedded systems. With reasonable effort, you can learn how to build one yourself. Turn inventions and ideas into inexpensive prototypes, automate your home by creating a fish-feeding device or controlling lighting from your computer, or build a remote-controlled surveillance camera for your yard that you can access via a computer located anywhere in the world. Artists can create interactive installations or integrate sensors into a game that you can control without touching a computer. Possible implementations are endless. During the 2000s, the DIY meme gathered more and more popularity, as is evident with the growth of MAKE Magazine and websites such as http://www .instructables.com. The Bay Area Maker Faire, an annual DIY festival, went from 22,000 attendees in its first year (2006) to more than double that amount (45,000) in its second year. And each year, Maker Faire attendance keeps growing. Learning embedded systems is becoming even more appealing due to the growing interest in robotics. In a 2006 Scientific American article,* Microsoft founder Bill Gates predicted that robotics would be the next revolution within homes, comparing the current state of the robotics industry to the computing industry in the 1970s. Gates anticipates that robots will soon become a natural part of a home, taking care of simple tasks such as vacuum cleaning, lawn mowing, surveillance, and food service. In addition, because robots can be controlled remotely from anywhere, we’ll be able to use them for telepresence— viewing, hearing, and touching people and things without even having to be present.
Intelligent Air Conditioning The common use of embedded systems is not just the stuff of science fiction or future technology. It’s already here and pervasive in the home. Consider air conditioning. A smart air conditioning system adjusts itself based on measurements. How does it know when the air is thick or stale? Air conditioners measure the temperature, humidity, and sometimes also carbon dioxide levels using sensors. A microcontroller (a small, dedicated computer) follows these measurements, and if the air is damp, for example, it activates a servo that opens an air valve, letting fresh air flow in. This type of *http://www.scientificamerican.com/article.cfm?id=a-robot-in-every-home
viii
Preface
Learn Embedded Systems in a Week
intelligent air control system has many benefits. It saves energy, because the air conditioning system doesn’t need to be used at full power all the time, and it makes working in such a space more comfortable, because there’s neither a constant draft nor stagnant air. The heating and air conditioning system at your own school or job likely functions on the same principles.
Sensors, Microcontrollers, and Outputs Embedded systems include sensors, microcontrollers, and outputs. Sensors measure conditions within a physical environment, such as distance, acceleration, light, pressure, reflection of a surface, and motion. The microcontroller is the brain of an embedded system. It’s a tiny computer, with a processor and memory, which means you can run your own programs on it. The Arduino microcontroller used in this book is programmed using a full-size computer via a USB cable, with sensors and outputs connected to the microcontroller pins. Outputs affect the physical environment. Examples of outputs you’ll learn to control in this book include LEDs and servo motors. Output devices are sometimes known as actuators.
Learn Embedded Systems in a Week This book will teach you the basics of embedded systems in just one week, during which time you’ll build your first gadget. After that, you can move on to more complex projects and prototypes based on your own ideas. Within seven days, you will already be deep within the world of embedded systems. This goal can sound immense—at least, we felt it was impossible before we became familiar with contemporary development environments. But today, many projects that once felt impossible now seem straightforward. The purpose of this book is to teach you how to build embedded systems, and we’ve left out any topic that does not support the practice of building prototypes. For example, we don’t cover history, movement of electrons, or complex electrical formulas. We believe it makes more sense to study these concepts after you are surrounded by your own homemade devices.
Classroom Use We tested this book with actual students during a one-week, intensive course led by Tero Karvinen. By the end of the week, all the students in the course were able to build their own prototypes. The students built many types of projects: a burglar alarm that can be disarmed with a wireless RFID keychain; a flower-measurement device that saves the height, humidity, and temperature of a flower to memory; a sonar device that draws an image of its distance on a computer screen; an automatic triggering device for a camera; a web-based control device for a camera; and a temperature meter observable via an Internet interface. For more examples of projects, visit http://BotBook.com/.
Preface
ix
How to Read This Book
Feedback from the class included one common wish: a longer course with more theory. Hopefully, you will become equally hungry for more after you have learned how to build gadgets. We believe that learning electronic theory becomes more interesting after you have already built functional devices. For a complete book on electronics that begins at the beginning, see Charles Platt’s Make: Electronics (O’Reilly, http://oreilly.com/catalog/9780596153748).
What You Need to Know Being able to use a computer is a prerequisite for completing the exercises in this book. You will need to know how to install programs and solve simple problems that often pop up during program and driver installation. We’ve tested the instructions in this book in Ubuntu Linux, Windows 7, and Mac OS X. You should be able to implement the instructions relatively easily for other Windows systems or other Linux distributions. Programming skills can be helpful but are not necessary for learning embedded systems. The particular programming language you know isn’t important, but being familiar with basic programming principles such as functions, if-then statements, loops, and comparisons is beneficial. It’s possible to learn programming along with learning about embedded systems, but this approach could take more time. You might find it useful to consult a beginner’s book on programming. High school–level electrical theory and knowledge of voltage, current, resistance, and circuits is sufficient. Have you already forgotten this? No worries— we will revisit basic electrical theory before starting the projects.
How to Read This Book One of our goals is to provide information in an easily digestible form. By reading this book, anyone can learn how to build impressive-looking electronic devices. Instead of splitting the book into separate sections for techniques and code, we have attempted to combine the information within six projects. This way, you will learn new things bit by bit and can immediately test them in real situations. The beginning of each project provides learning goals and a list of necessary parts. Before building a device, you can test each part individually; applying the components usually becomes much easier once you understand their core functions. It is useful to come back to these introductory sections later, as you incorporate things you have learned into your own new applications. We also explain each line of code. This does not mean that you should first read the explanations and continue only after you have internalized everything. We always provide the entire functional code, which you can type or download from http://BotBook.com/. Once you have succeeded in getting one version of the code to work, you’ll be motivated to find out how it works or to customize it for your own purposes. When you start to build your own devices, the explanations will make it easier for you to identify the necessary sections of the provided code. x
Preface
Contents of This Book
The projects are partitioned so you can test each part one step at a time. This way, it is easier to understand the function of each step and the relationships between different parts. This also helps ensure that once you have built a device, you can easily troubleshoot any problems; if something doesn’t work, you can always go back to an earlier functioning phase and restart from there. There are examples of enclosures for several projects in this book. They are useful as teaching techniques for mechanical construction and give you ideas for how to make a demonstrable prototype relatively inexpensively. You are not obligated to follow the instructions literally. You might have different parts or a better vision for the look of your device.
Contents of This Book This book includes two introductory chapters followed by six chapters with projects. As you move through the book, you’ll go from learning the basics of Arduino to completing projects with moving parts, wireless communication, and more: Chapter 1, Introduction This chapter explains prototyping, including an overview of the philosophy behind it, techniques, and tools. Chapter 2, Arduino: The Brains of an Embedded System This chapter familiarizes you with Arduino, the open source electronics prototyping platform used in every project in this book (except the Boxing Clock in Chapter 6). Chapter 3, Stalker Guard In this chapter, you’ll learn how to use distance-finding sensors to detect when someone is trying to sneak up on you. Chapter 4, Insect Robot This chapter uses distance-finding sensors, servos, and spare parts to make an obstacle-avoiding robot. Chapter 5, Interactive Painting This chapter combines Arduino, your computer, and distance-finding sensors to create an interactive slideshow you can control with your hands. You’ll also learn about two languages for programming on the computer: Processing and Python. Chapter 6, Boxing Clock This chapter teaches you how to build a graphically rich timer clock on an Android phone. It will also serve as a primer for Chapter 8. Chapter 7, Remote for a Smart Home In this chapter, you’ll hack some remote-controlled power outlets so you can turn things on or off using a sketch running on Arduino—or even from the convenience of your desktop computer.
Preface
xi
Using Code Examples
Chapter 8, Soccer Robot This chapter combines a lot of what you’ve learned so far: Arduino, robotics, and cell phone (Android) programming. You’ll learn how to create a remote-controlled, soccer-playing robot. You’ll control it from your cell phone’s built-in accelerometer; simply tilt the phone to tell the robot to move or kick a small ball! Appendix, tBlue Library for Android The appendix presents tBlue, a lightweight library that makes it easy to communicate over Bluetooth between an Android phone and Arduino.
Conventions Used in This Book The following typographical conventions are used in this book: Italic Indicates new terms, URLs, email addresses, filenames, and file extensions. Constant width Used for program listings, as well as within paragraphs to refer to program elements such as variable or function names, databases, data types, environment variables, statements, and keywords. Constant width bold Shows commands or other text that should be typed literally by the user. Constant width italic Shows text that should be replaced with user-supplied values or by values determined by context.
Using Code Examples This book is here to help you get your job done. In general, you may use the code in this book in your programs and documentation. You do not need to contact us for permission unless you’re reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing a CD-ROM of examples from O’Reilly books does require permission. Answering a question by citing this book and quoting example code does not require permission. Incorporating a significant amount of example code from this book into your product’s documentation does require permission. We appreciate attribution. An attribution usually includes the title, authors, publisher, copyright holder, and ISBN. For example: “Make: Arduino Bots and Gadgets, by Kimmo Karvinen and Tero Karvinen (O’Reilly). Copyright 2011 O’Reilly Media, 978-1-449-38971-0.” If you feel that your use of code examples falls outside fair use or the permission given above, feel free to contact us at [email protected]
xii
Preface
Safari® Books Online
We’d Like to Hear from You Please address comments and questions concerning this book to the publisher: O’Reilly Media, Inc. 1005 Gravenstein Highway North Sebastopol, CA 95472 (800) 998-9938 (in the United States or Canada) (707) 829-0515 (international or local) (707) 829-0104 (fax) We have a website for this book, where we list errata, examples, and any additional information. You can access this page at: http://oreilly.com /catalog/9781449389710. All code examples and programs are available on http://BotBook.com. To comment or ask technical questions about this book, send email to: [email protected] Maker Media is a division of O’Reilly Media devoted entirely to the growing community of resourceful people who believe that if you can imagine it, you can make it. Consisting of MAKE Magazine, CRAFT Magazine, Maker Faire, and the Hacks series of books, Maker Media encourages the Do-It-Yourself mentality by providing creative inspiration and instruction. For more information about Maker Media, visit us online: MAKE: www.makezine.com CRAFT: www.craftzine.com Maker Faire: www.makerfaire.com Hacks: www.hackszine.com
Safari® Books Online Safari Books Online is an on-demand digital library that lets you easily search over 7,500 technology and creative reference books and videos to find the answers you need quickly. With a subscription, you can read any page and watch any video from our library online. Read books on your cell phone and mobile devices. Access new titles before they are available for print, and get exclusive access to manuscripts in development and post feedback for the authors. Copy and paste code samples, organize your favorites, download chapters, bookmark key sections, create notes, print out pages, and benefit from tons of other timesaving features. O’Reilly Media has uploaded this book to the Safari Books Online service. To have full digital access to this book and others on similar topics from O’Reilly and other publishers, sign up for free at http://my.safaribooksonline.com.
Preface
xiii
Acknowledgments
Acknowledgments Thanks to: • Juho Jouhtimäki • Marjatta Karvinen • Nina Korhonen • Mikko Toivonen • Marianna Väre • Medialab, Aalto University School of Art and Design • O’Reilly Media • Readme.fi • Tiko, Haaga-Helia University of Applied Sciences
xiv
Preface
Introduction
This chapter will get you started building and designing prototypes for embedded systems. You will learn basic principles that you’ll follow in Chapters 3 and 4 as you build the Stalker Guard and Robot Insect. Prototypes in this book are just the beginning. Once you know the techniques, you’ll be able to build prototypes for your own inventions.
1 In this chapter Building Philosophy Reusing Parts Buying Components Useful Tools Electronic Circuit Theory Review
Building Philosophy When you break a programming problem down into smaller pieces, be sure to test and validate each piece as you go. If you don’t do this, you could find yourself wildly off track by the time you’ve gotten through a few pieces.
Prototype This book provides techniques for building prototypes, or test versions of a device. A prototype such as the one shown in Figure 1-1 provides a proof of concept—a concrete realization of a device’s intended functions. Try to finish a functional prototype as quickly as possible. Once you’ve documented a working prototype, you can build in improvements in later versions. You can make a working end result by stripping out unnecessary functions and taking shortcuts. If it makes testing quicker, use rubber bands and duct tape when you have to. Don’t try to optimize your code in the first version. It’s much easier to build an impressive version once the first prototype is finished. Usually, you’ll find that many challenging problems you face in the prototype don’t even need to be solved for the final version. In the same way, building a prototype can reveal new opportunities for development.
Figure 1-1. Jari Suominen testing a prototype made of Legos
1
Building Philosophy
Having a prototype can also help you secure funding for your project. Who would you believe more: someone who talks about a walking robot, or someone who has actually built one?
Start with Hello World How does Arduino say “hello” to the world? By blinking an LED. You’ll learn more in Chapter 2.
Starting a project with Hello World is usually a good idea, because it’s the simplest possible program. Typically, Hello World will print a row of text to a computer screen or blink an LED. It is used for testing to make sure the development environment works. If your next, more complicated iteration doesn’t work, you can search for the cause of the problem within the added code. Hello World lets you know that the microcontroller, development environment, interpreter, and USB port all function correctly.
Build in Small Steps Complex problems (see Figure 1-2, Figure 1-3, and Figure 1-4) are hard to solve, but you can usually make them easier by breaking them down into smaller pieces. You can then solve the problem one manageable piece at a time. A student of ours once built a burglar alarm after studying embedded systems for a week. The alarm buzzed whenever an infrared sensor detected movement. Users could log into the system wirelessly by presenting an ID in the form of a keychain. Once the system approved the login, the user could then move freely in the space without triggering an alarm.
Figure 1-2. Juho Jouhtimäki and Elise Liikala building a motion-sensitive soft toy
Figure 1-3. Welding a robot hand
A project like this can sound quite complex to a novice, but it really consists of three clearly separate components (motion detector, buzzer, RFID reader). First, the student programmed and tested the motion sensor. That section was finished when the program could detect movement and sound the alarm.
Figure 1-4. 
2
The three components of the system do not affect one another in any way, and the only unifying factor is the code. Program code can check with the motion detector to determine whether movement is present and, if so, it can switch on the buzzer. Chapter 1
Building Philosophy
Test in Steps “I wrote the code for a singing and dancing robot that can walk up stairs. The code is 30,000 lines long. I just tried compiling it, but it doesn’t work. Do you have any advice?” Conduct testing as early as possible. If, for example, you build a walking robot, the first thing to test is whether you can make the servo motor move. The next test can make the servo move back and forth. After you have tested the functionality of a specific version of code, save it separately from the version you are working on.
Revert to the Last Known Good Version When you have developed your code into a confusing and nonfunctional state, the solution is easy. Go back to the last working version. More specifically, go back to a working stage when the situation was already becoming confusing. This method removes the problem areas and lets you start over with a functional clean slate, helping you isolate what went wrong.
Read the Friendly Manual RTFM is an old Internet acronym. (Actually, the F is not always friendly, so we usually stick with just RTM.) The point of the expression is that most answers are out there, written in a manual. When you’re surrounded by parts (see Figure 1-5), you're going to need answers. Friends and students sometimes wonder how we know so much. How do we know the Arduino operating voltage or the way to install SSL encryption to the Apache web server?
Figure 1-5. Mikko Toivonen, surrounded by robots and microcontrollers
The answer is easy. You can find instructions for almost anything if you know where to look. Instructions don’t always come with devices and parts, but you can often find them on manufacturer’s web pages (such as http://www.parallax.com) or by searching in Google. Good search terms include device names (e.g., “ping ultrasonic sensor”) or a sequence of numbers on a circuit board (e.g., “H48C”). Introduction
3
Reusing Parts
You could also combine a search sequence with a technology—for example, “H48C arduino.” Some web pages are devoted specifically to Arduino—for example, http://arduino.cc and our site, http://BotBook.com.
Document Most things appear easy once you know them. The details of a project seem obvious on the day you complete them (“of course I remember when I programmed the 16-servo walker”). But a week after building, coding details begin to disappear from your memory. After a year or so, it can be hard for someone who builds many projects to remember anything about a specific one. For this reason, it is worthwhile to document all projects. Typing notes avoids the potential problem of illegible handwriting, and shooting stages with a digital camera provides an accurate visual snapshot of each stage.
Figure 1-6. Jenna Sutela and David Szauder demoing functions of a wearable prototype
You might also consider publishing your results on the Web. Some projects that would otherwise be collecting dust in your drawer might actually be useful to others. You might even find your own instructions (long since forgotten) when looking to solve a new problem with similar logic. Two sites where you can publish projects are Make: Projects (http://www.makeprojects.com) and Instructables (http://www.instructables.com).
Reusing Parts
Not every device is safe to salvage: for example, a CRT (Cathode Ray Tube) TV retains a hazardous voltage for a long time after you unplug it from the wall.
Prototype mechanics (see Figure 1-6) need all kinds of parts, such as frames, limbs, and joints. Finding appropriate materials can seem daunting. Customizing more complicated parts using homebrew methods isn’t always easy, and even basic materials—such as lightweight and sturdy metal plates—can be significantly expensive at hardware stores. As a starting point, we recommend using recycled parts. Old devices are filled with usable materials, so remove all salvageable parts before you throw them away. One additional perk that comes with using recycled parts is a unique aesthetic. Old parts often have interesting shapes, curves, and worn areas (Figure 1-7).
Figure 1-7. An assortment of parts that can be reused
4
Chapter 1
Reusing Parts
Computer DVD drives and hard drives can make great frames for robots, because their covers are often made of lightweight, easily drillable, and sturdy material. You can also remove DC (direct current) motors and gears from DVD drives. Nowadays, there is more readily available computer junk than you can gather and store in your home. Educational institutions and corporations are particularly good sources, as they’re continuously throwing out old devices. Flea markets can also hold great finds. Mechanical typewriters deserve a special mention here. Though they are relatively hard to disassemble, they house an unbelievable amount of small springs, metal pieces of different shapes, and screws. Disassemble devices as soon as you find them and then discard or recycle unnecessary parts. This way, you’ll avoid turning your home into a graveyard of retired devices, and more importantly, the parts will be immediately usable when you really need them. When you are searching for a suitable attachment piece for a servo, you probably don’t want to start a six-hour disassembly operation. Parts usually won’t find a new purpose until you’ve removed them from the original device, at which point inspiration might strike. You might even wonder how a specific “whatchamacallit” fits a new purpose so perfectly. When you begin working on some difficult new mechanism, think about where you might have seen something similar. You’ll often find everyday solutions to many problems. For example, parts purchased from bicycle or automotive shops can sometimes work in other projects. Figure 1-8 shows a hand with fingers that are moved with servo motors; every joint in each finger bends. The fingers were made by attaching sections of a steel pipe to a bicycle chain. They bend when a brake cable is pulled down. Typewriter parts welded to the opposite side of the structure pull the fingers back into a straight position.
Figure 1-8. Robot hand made of junk
Also keep your eyes open in military surplus stores, where you can find inexpensive, sturdy, and personalized enclosures for prototypes. Various parts and accessories in these shops can also, with a bit of creativity on your part, give devices significantly more street cred. For example, Figure 1-9 shows a porcupine robot cover built from an MG/42 machine gun ammunition belt.
Introduction
5
Buying Components
Figure 1-9. Porcupine robot cover made from a machine gun ammunition belt
Buying Components If you’re in the US, you will generally be able to find all the parts you need within the country. However, if you ever need to order large amounts of something (such as hundreds or thousands of LEDs), you may find yourself purchasing from an overseas supplier (for example, many bulk LED sellers on eBay ship from Hong Kong). When choosing a country to order from, take into consideration customs rules and additional fees incurred by international orders. Shipping costs can also be high in some countries, and some companies won’t even ship overseas. Also, consumer protections might not apply to international orders in the event that the package is broken or the product is different from what you ordered. Regardless of all the scaremongering, ordering internationally usually works out without major problems. We have received everything we have ordered, and the products haven’t had any major faults.
If you can’t find exactly what you’re looking for in recycled materials, order component parts online. Many unique components can’t be found locally at all, or will be overpriced if you do find them. Luckily, comparing prices and ordering online is quite easy. Because online stores often change, make sure to check the latest links available at http://BotBook.com/. We purchased parts for this book from a variety of sources. We ordered most of the sensors and full-rotation servos from the United States. Arduinos and some of the sensors came from Sweden. We rounded up ordinary components— such as resistors, LEDs, and wiring—from electronics stores in Helsinki. Standard servos came from a Finnish online store specializing in radio-­controlled cars and airplanes. Some servos were ordered from Hong Kong. Here are a few sources to consider: Maker SHED MAKE Magazine’s store can be found online at http://www.makershed .com/ and in real life at Maker Faire (http://makerfaire.com/). Maker SHED carries Arduinos, project kits, tools, parts bundles, books, and much more. Keep on eye on Maker SHED for special parts bundles or kits dedicated to projects in this book. Adafruit Industries The Adafruit store (http://www.adafruit.com) specializes in Arduinos, microcontrollers, electronic and robotic components (including servo motors), tools, and kits. It also has a comprehensive set of Arduino tutorials and produces its own Arduino-compatible boards such as the Boarduino. SparkFun Electronics Among many other things, SparkFun (http://www.sparkfun.com/) is a great source for all kinds of sensors—from light and temperature sensors to accelerometers and gas sensors. What’s more, it sells the sensors mounted to breakout boards so you can easily connect them to an Arduino without having to do tricky surface-mount soldering. SparkFun has much more, including tools, parts, and Arduinos.
6
Chapter 1
Useful Tools
Useful Tools When building prototypes, you’re going to need some tools (Figure 1-10). The following sections cover the tools that we have found a consistent need for. They are not all mandatory, but depending on your own projects or needs, you may have a use for them in the future.
Hearing Protectors and Safety Glasses When using power tools, you must cover your ears with proper hearing protectors and wear safety glasses to protect your eyes from harmful flying debris and material fragments (Figure 1-11). Note that metal can fly forcefully, even when you’re cutting or bending with pliers. Figure 1-10. Wire stripper and side-cutter pliers are sufficient for building prototypes on a prototyping board
Figure 1-11. Hearing protectors and safety glasses
Needlenose Electronics Pliers You should immediately purchase good needlenose pliers (Figure 1-12), which can be used to grab small components and parts. The tip for the pliers should be sharp enough to fit into even the smallest of spaces.
Introduction
Figure 1-12. Needlenose electronics pliers
7
Useful Tools
Diagonal-Cutter Pliers Diagonal-cutter (or side-cutter) pliers, shown in Figure 1-13, are used for cutting wires and are also suitable for other small cutting jobs. Always keep at least one set of side cutters in good shape, and use a secondary pair for tasks that cause more wear.
Metal Saw A metal saw is a basic, functional tool for shaping and cutting metal (Figure 1-14). Keep a spare blade on hand to keep promising building processes from being interrupted by a broken blade.
Wire Strippers Figure 1-13. Diagonal-cutter pliers
Wire strippers are used to remove the plastic around a wire to expose a conducting metal within specific areas. Do not use your teeth to strip wires! It is much more expensive to fix dental enamel than to spend just a few dollars on good wire strippers. The adjustable wire strippers on the left side of Figure 1-15 are much more useful than the multigauge model on the right, but they’re not as common.
Figure 1-14. Metal saw
Figure 1-15. Wire strippers
8
Chapter 1
Useful Tools
Screwdrivers You’ll need many different types of screwdrivers, especially when opening devices. Using the wrong screwdriver tip for a particular screw could destroy either the screw or the screwdriver and is just not worth the potential damage. The easiest and most economical thing to do is to buy a kit that comes with a handle and various attachable bits (Figure 1-16). Many electronic devices require a Torx driver and can’t be opened with a flat- or Phillips-head screwdriver.
Alligator Clips Alligator clips (Figure 1-17) can be useful for quickly connecting components and cables. They can also connect multimeter probes, enabling hands-free measurements.
Electric Drill You’ll need an electric drill for many projects. A hammer drill, shown in Figure 1-18, is also suitable for drilling into concrete, but a rechargeable cordless drill is easier to handle.
Figure 1-16. Screwdriver kit with a variety of bits
A drill bit can break easily, especially when you’re drilling metal with thin bits, so you must wear eye protection when working with a drill. Always position the drill directly into the hole; drilling at an angle will bend the bit and cause it to break under rotation.
Figure 1-17. Alligator clips
Figure 1-18. Electric drill
Introduction
9
Useful Tools
Leatherman A portable handy tool such as a Leatherman (Figure 1-19) is useful during several phases of project building. In this case, it makes sense to invest in the name-brand tool rather than buying cheap imitations. A high-quality multipurpose tool can withstand heavy use, and its individual parts function in the same way as separate tools. Maker SHED sells an assortment of MAKE-branded Leatherman Squirt tools, such as the MAKE: Circuit Breaker Leatherman, a set of electronics tools that can fit on a keychain. See http://www.makershed.com/SearchResults. asp?Search=leatherman for more information. Figure 1-19. Leatherman
Mini Drill A mini drill (Figure 1-20) is not absolutely necessary, but it makes many tasks easier. Compared to an electric drill, a mini drill is lightweight and relatively precise to work with. By using an appropriate bit, you can use a mini drill for drilling, sanding, sharpening, shining, cutting, and more. Of course, it doesn’t replace a normal drill, because it doesn’t have sufficient torque for drilling larger holes.
Figure 1-20. Mini drill
10
Chapter 1
Useful Tools
Headlamp A headlamp (Figure 1-21) can be handy for focusing light in the direction you’re working. Additional light is useful to have, even in well-lit spaces.
Hot-Glue Gun A hot-glue gun (Figure 1-22) can adhere items together quickly. The resulting connection is not necessarily very strong, and glued items can bend away from each other, but it works sufficiently well in many prototyping phases. In addition, the fact that hot glue hardens quickly, and items glued with it can be (at least in theory) removed from each other relatively easily, can make the building process less stressful. Still, hot glue is not a replacement for Blu-Tack, and another downside is that if you’re unsuccessful in your first attempt to join items together using hot glue, you’ll usually need to scrape and shine the surfaces before trying again.
Figure 1-21. Headlamp
Nail Punch and Hammer Drilling metal at home without a drill press can be quite challenging, especially with smooth metal surfaces on which a bit can slide and go through the wrong spot. A nail punch (Figure 1-23, left) can fix this problem. It can create a small dent on the spot where you want to drill a hole, making drilling much easier.
Figure 1-22. Hot-glue gun
A hammer is a useful tool in its own right, but it’s not always the right tool for the job. If you have something to dislodge or to set in place, look for a gentler tool first, so you don’t break your project into many little pieces. As Abraham Maslow said, “I suppose it is tempting, if the only tool you have is a hammer, to treat everything as if it were a nail.”
Figure 1-23. Nail punch and hammer
Introduction
11
Useful Tools
Soldering Iron A soldering iron (Figure 1-24) joins metal sections of components together with molten metal (usually lead, but lead-free solder is available as well). The tip of a soldering iron must be sufficiently thin to enable precise attachment of small parts. Irons with a built-in thermostat are more expensive, but having the capability to adjust the temperature lessens the likelihood of destroying more sensitive components. You will learn the basics of soldering in Chapter 3.
Figure 1-24. Soldering iron
Multimeter A multimeter (Figure 1-25) is used for measuring current, voltage, and resistance. You can use it to test a value of a resistor or whether two sections of a circuit are connected. You also can test the condition of a battery by measuring its voltage. The multimeter shown in Figure 1-25 has two ranges for measuring voltage: DC (direct current) and AC (alternating current). All Arduino circuits in this book use direct current. The correct measurement range for voltage and resistance is the smallest possible range onto which measured readings can fit.
Figure 1-25. Multimeter
12
A continuity test works technically in the same way as measuring a value of a resistor. Instead of displaying a resistance value, the continuity test beeps when an unrestricted flow of electricity is detected between two measurement probes.
Chapter 1
Useful Tools
Figures 1-26 and 1-27 illustrate some common uses for a multimeter. The Interactive Painting project in Chapter 5 covers measuring resistance in more detail.
Figure 1-26. The most common functions of a multimeter
Figure 1-27. Studying a remote controller by measuring a voltage difference between two terminals of a button
Introduction
13
Electronic Circuit Theory Review
Electronic Circuit Theory Review We’ll end this chapter with just enough theory to get you started with the practice.
Voltage Creates an Electrical Current Voltage refers to a difference in electrical potential between two parts of a circuit. For example, the terminals of a battery can have a 9-volt voltage between them. If two parts of a circuit with different electrical charges are connected, voltage potential creates a current flow. For example, current will start flowing through a lamp that is connected between the two terminals of a battery, causing the lamp to light up. A unit of voltage is a volt (V). The Arduino microcontroller used in this book functions with a minimum 7V and maximum 12V power adapter (or it can be powered from a 5V USB connection). Voltages inside computers are within a similar range. US AC sockets provide 110 volts and European AC sockets provide 230 volts. A lamp will be brighter with a 9V battery than with a smaller 4.5V battery. Larger voltage creates a larger current. If a component is used with a voltage higher than what it is rated for, it will usually burn out. If you supply 5 volts to an LED that is rated for 2.4 volts, it will probably make a popping sound, release a little smoke, and cease to function. A running joke among electrical engineers and technicians is that once you’ve released the “magic smoke” inside an electronic component, you can’t put it back in.*
A Resistor Resists the Flow of Current If a resistor is added between a lamp and a battery, the lamp will be dimmer. A resistor resists the flow of current. All components create at least a bit of resistance. A filament of an incandescent light bulb is sufficient by itself to resist the current flow. A resistor may be all that’s needed to avoid releasing the magic smoke inside an LED. For example, a 1 kOhm resistor is generally more than sufficient to protect a red LED. If you have the specifications for your LED, you can calculate the value of the resistor. Evil Mad Scientist Laboratories has a handy papercraft pocket LED calculator that you can print out and carry with you: http://www.evilmadscientist.com/article. php/ledcalc.
* http://en.wikipedia.org/wiki/Magic_smoke
14
Chapter 1
Electronic Circuit Theory Review
Short Circuits Are Dangerous If you bridge a battery’s positive and negative terminals with a wire, it forms a short circuit. The current flows rapidly through the wire, and the wire and battery will both become warm and may possibly leak or explode. Why do we mention this? Because it’s possible to create a short circuit in your own projects if you don’t use the correct resistor values. When you follow the instructions to build a project, you must be sure to use the resistor values specified to avoid creating the hazardous condition that comes with a short circuit.
Closed Circuits Allow Electricity to Flow When a device is powered, its circuit is closed and electricity will flow through the device. An open circuit means that electricity cannot flow through a device. For example, a device that is shut down by its power switch is an open circuit. Electricity can’t flow when the circuit is opened by the switch. Figure 1-28 shows a closed circuit: two batteries powering an LED. The magic smoke didn’t come out because the batteries and LED are well matched: the LED has a voltage of 2.6V, which is more than the voltage delivered by two AA rechargeable batteries. Standard AA batteries (1.5V each) might overpower the LED. Still, if you intended to run this circuit for hours on end, it would be advisable to include a low-rated resistor, even a 10 or 100 Ohm.
Figure 1-28. Simple closed circuit powering an LED
Ground = Zero Voltage Level To make it easier to discuss topics related to voltage, a single point in a circuit is usually compared to the negative terminal of a power supply. The voltage level of a negative terminal is 0V, against which all other points of the circuit are measured. For example, the positive terminal of a 9V battery can be said to have 9V of voltage. Ground has many names, all of which mean the same thing: 0V, minus terminal, earth, and GND. Black wire is often used to connect to the ground (red is used for positive voltage). In a circuit, ground is marked with its own symbol (shown in Figure 1-29) to avoid having to always draw a line to the minus terminal.
Figure 1-29. Symbol for ground
In this chapter, we’ve covered prototyping principles, techniques, and tools, and reviewed some basics of electrical theory. Now we’ll move on to Chapter 2, where we introduce Arduino, the open source prototyping platform that will be the brain of your projects.
Introduction
15
Arduino: The Brains of an Embedded System
In this chapter, you’ll compile a program you have written onto an Arduino microcontroller, a small computer that acts as the brains of an embedded system. Arduino, an easy-to-learn hardware and software development environment and prototyping platform, is the foundation for the projects we’ll complete in upcoming chapters.
2 In this chapter Why Arduino? Starting with Arduino Hello World with Arduino Structure of “Hello World” Arduino Uno Arduino Nano
A microcontroller is a small computer with a processor and memory that controls the functions of many everyday devices. Some microcontrollers are designed to connect easily to a computer for programming for specialized purposes. Arduino is an example of one of these easy-to-program microcontrollers. Microcontrollers make it easier to build electronic devices because you can control their functions via code. Microcontrollers can control and interpret forms of both input and output. For example, you can flicker an LED by connecting it to a specific Arduino pin with code that instructs it to switch the current on for one second and then off for one second. The LED is an example of an output, which you could then control using a sensor, button, switch, or any other form of input. Naturally, most programs do many other, more sophisticated tasks. Microcontrollers enable us to solve quite complex problems step by step.
Why Arduino? The most suitable microcontroller choices for a beginner are Basic Stamp and Arduino. Basic Stamp has existed since the early 1990s and has become popular among hobbyists. It uses the Basic programming language, which is easy to use but somewhat limited compared to the C language used by Arduino.
17
Starting with Arduino
Functionally, Arduino is quite similar to Stamp, but it solves many problems that Stamp has traditionally faced. One significant feature for hobbyists is Arduino’s lower cost: the basic Arduino starting package is approximately a quarter of the price of a comparable Stamp package. And, despite its cheaper price, Arduino has a more powerful processor and more memory. Arduino is also smaller than Stamp, which is beneficial in many projects. The Arduino Uno model (see Figure 2-1, left) is slightly smaller than the Stamp, but the tiny Arduino Nano (Figure 2-1, right) is about the same size as the Stamp module that sits on the Stamp board (just above the serial port in Figure 2-2). For comparison, Figure 2-2 shows the Stamp and the Nano next to each other.
Figure 2-1. Arduino Uno (left) and Arduino Nano (right)
Figure 2-2. Basic Stamp (left) and Arduino Nano (right)
One final asset is that the Arduino programming environment is based on open source code and can be installed on Windows, Mac OS X, and Linux. Chapter 8 includes a project that uses Bluetooth. Although there is an Arduino model with built-in Bluetooth (Arduino BT), a more flexible option when you’re creating Bluetooth projects with Arduino is to use a third-party Bluetooth adapter, such as SparkFun’s Bluetooth Mate (http://www.sparkfun.com/products/10393). This will allow you to use the Bluetooth module with different projects, or to replace Bluetooth in one of your projects with another type of wireless module such as an XBee radio.
18
Starting with Arduino Arduino is available in a few different models. This book covers the aforementioned Arduino Uno and Arduino Nano. Uno is an inexpensive (around $30) and sturdy basic model, and is the most current version of the board. It was released publicly in September 2010 and is the successor to the Arduino Diecimila and Arduino Duemilanove. Nano is significantly smaller, but more fragile and slightly more expensive ($35). Both models are described in a bit more depth at the end of this chapter. First, you have to buy an Arduino and a compatible USB cable. Uno and Nano communicate to your computer via USB (for uploading new programs or sending messages back and forth). They can also take their power over USB. Uno uses a USB-B cable and Nano uses a Mini-B, and each connects to the computer with a USB-A male connector. All three connectors are shown in Figure 2-3.
Chapter 2
Starting with Arduino
Figure 2-3. Arduino USB cables: Mini-B, USB-A, and USB-B
Installing Arduino Software Next, you need to install the Arduino development environment for your operating system and compile the first test program. This “Hello World” code is the most important part of getting started with a new device. Once you are able to compile simple, light-blinking code in Arduino, the rest is easy. The examples in this book were tested with version 0021 of the Arduino development environment. If you decide to use some other version, the installation routine might differ. If you are using an operating system other than Windows, Ubuntu Linux, or Mac OS X, or an Arduino other than Uno or Nano, look for installation instructions at http://arduino.cc/. And remember that you will find all complete code examples, links, and program installation packages at http://BotBook.com/.
Windows 7 Here’s how to get up and running under Windows 7: 1. Download the Arduino development environment from http://arduino.cc/ en/Main/Software and unzip it to the desired folder by clicking the right button and selecting “Extract all.” 2. Connect the USB cable to your computer and to the Arduino’s USB port. The Arduino LED should light green. 3. Windows will search for and install the necessary drivers automatically. It notifies you when the installation is complete. If Windows does not locate the driver: a. Open Device Manager by clicking the Start Menu, right-clicking Computer, choosing Properties, and then clicking Device Manager in the list of options on the left.
Arduino: The Brains of an Embedded System
19
Starting with Arduino
b. Locate Arduino Uno in the list of devices (it should be in the section called Other Devices). Right-click it and choose Update Driver Software. c. Choose “Browse my computer for driver software.” d. Navigate to the Arduino folder you extracted, select the drivers subdirectory, and press Next. e. If prompted to permit the installation of this driver, choose “Install this driver software anyway.” When the driver is successfully installed, you’ll see the dialog shown in Figure 2-4.
Figure 2-4. Drivers installed
Windows XP In general, installation for most Windows XP programs is pretty similar to Windows 7, but Arduino is an exception. If you have XP, start by downloading the Arduino development environment, extracting the file to a location on your computer, and connecting the Arduino to your computer as described in the previous section. Then follow these additional instructions: 1. Windows opens the Found New Hardware Wizard. 2. Select “Install from a list or specific location” in the window and press Next.
20
Chapter 2
Starting with Arduino
3. Deselect the checkbox in “Search removable media” and check the box “Include this location in the search.” Navigate to the Arduino folder you extracted, select the drivers subdirectory, and press Next. If you are using an older model of Arduino, or the Nano, you may need to choose the drivers/FTDI USB Drivers subdirectory instead. 4. Click Finish.
Ubuntu Linux Though you can install Arduino on Ubuntu and other Linux environments using graphical user interface tools, the following steps use the Terminal (Figure 2-5) to simplify the instructions. Open the Terminal by choosing Applications→Accessories→Terminal. The dollar sign at the beginning of the following command lines is the command prompt created by the computer; do not type the dollar sign, just the characters that follow it.
Before you try to install Arduino on Linux, consult the Arduino FAQ (http://arduino.cc/en/Main/ FAQ#linux) for links to the latest instructions.
We tested this installation process with Ubuntu 9.04, but it should also function (with minor alterations) with other versions.
Figure 2-5. Command line
Start using the universe program repository, which includes free, open source programs, with publicly available source codes: $ sudo software-properties-gtk --enable-component=universe
When asked by sudo, type your password. The command after sudo will be executed using root user privileges.
Arduino: The Brains of an Embedded System
21
Hello World with Arduino
Update the available software list: $ sudo apt-get update
Now it’s time to install dependencies: all the programs the Arduino development environment requires to function, including Java (openjdk) and programming tools for the AVR chip gcc-avr, avr-libc, and avrdude. New 64-bit computers also require the 32-bit compatibility library ia32-libs. $ sudo apt-get install --yes gcc-avr avr-libc avrdude openjdk-6-jre $ sudo apt-get install --yes ia32-libs
Next, download and open the Arduino development environment from the official Arduino home page (http://arduino.cc/en/Main/Software), where you’ll find two packages: “Linux (32bit)” and “Linux (AMD 64bit).” Newer computers are based on 64-bit technology. If you don’t know which package to download, use the uname-m command to determine whether your computer is a newer 64-bit model (x86_64) or an older 32-bit model (i386). Uncompress the software package you downloaded (this will create an arduinoversion directory under your current working directory): $ tar -xf ~/Downloads/arduino-*.tgz
Start the Arduino development environment. Because you will execute the command from a specific folder, define the whole path to that folder: $ ./arduino
The Arduino development environment will start.
Mac OS X Here’s how to get up and running under Mac OS X: 1. Download the Arduino development environment from http://arduino.cc/ en/Main/Software and open the .dmg file. 2. A new Finder window appears with three icons (Arduino, a link to your Applications folder, and the FTDI USB serial driver package). 3. Drag the Arduino icon to your Applications folder. 4. If you are using a version of Arduino prior to the Uno, install the ​FTDIUSBSerialDriver package. 5. When you connect the Arduino, you may see the message “A new network interface has been detected.” Click Network Preferences and then click Apply. You can close the Network Preferences when you are done.
Hello World with Arduino Now you’re ready to upload your first Arduino program. Open the Arduino development environment: Windows Double-click the Arduino icon (you’ll find it inside the Arduino folder that you extracted earlier).
22
Chapter 2
Hello World with Arduino
Linux Change directory to the Arduino folder and run Arduino: $ cd arduino-0021 $ ./arduino
Mac OS X Double-click the Arduino icon (you’ll find it inside the Arduino folder). Select Tools→Board→Arduino Uno, as shown in Figure 2-6. If you are using a different model of Arduino, select it instead.
Figure 2-6. Arduino board selection
Select File→Sketchbook→Examples→1. Basics→Blink. This example code flashes the LED on the Arduino pin 13 (the Uno includes an onboard LED connected to pin 13). Determine which serial port Arduino is using: Windows Open the Start menu, right-click the computer icon, and select Properties. System Properties will open. On Windows XP, click Hardware. On Vista or Windows 7, look in the list of links to the left. Select Device Manager from the list and open the “Ports (COM & LPT)” node. See which COM port is marked as a USB COM port, as shown in Figure 2-7.
Arduino: The Brains of an Embedded System
23
Hello World with Arduino
Figure 2-7. Determine the correct port
Linux and Mac OS X With the Arduino unplugged from your computer, select Tools→Serial Port in the Arduino development environment, and observe the list of serial ports listed (such as /dev/ttyUSB0 in Linux or /dev/tty.Bluetooth-Modem in Mac OS X). Dismiss the menu by clicking elsewhere onscreen. Plug the Arduino in and choose the same menu options again (Tools→Serial Port). Observe which serial port appeared. You’ve figured out which serial port your Arduino is using. In the Arduino development environment, select Tools→Serial Port and choose the port you found in the previous step. Click the icon with the right-pointing arrow in a square box, or choose File→“Upload to I/O Board.” The Arduino transmission lights will flash briefly, and you should see the message “Done uploading.” Now you should see the yellow LED labeled L on the Arduino board flashing (see Figure 2-8). This means you have successfully installed the Arduino development environment and uploaded your first Arduino program to the microcontroller. If the light is not flashing, follow the instructions again to see where the installation went wrong. You cannot proceed if this does not work. If you continue to have problems, see the online Arduino troubleshooting guide at http://www.arduino.cc/en/Guide/Troubleshooting.
24
Chapter 2
Structure of “Hello World”
Figure 2-8. The LED on pin 13
You might want to come back to this section and run through these steps if you have problems working with Arduino in the future; the Blink example is a good test to ensure that Arduino is working. If the LED does not flash, it’s a good time to make sure the cord is plugged in, or determine whether your computer or the Arduino is having problems. Once you establish that something as simple as blinking an LED works, it will be easier to solve the more complex problems later.
Structure of “Hello World” You just took an important step by running the “Hello World” of Arduino: Blink. The program blinks an internal LED on the Arduino. If you can get the LED to blink, you can be confident that you can compile and upload programs to Arduino. All Arduino programs have a similar structure. Since Blink is a simple program, it is easy to understand the program structure by examining it. Here is the source code for Blink, which comes with Arduino but is offered here with our commentary: /* Blink 1 Turns on an LED on for one second, then off for one second, repeatedly. */ void setup() { 2 // initialize the digital pin as an output. // Pin 13 has an LED connected on most Arduino boards: pinMode 3 (13, OUTPUT 4); 5 }
Arduino: The Brains of an Embedded System
25
Structure of “Hello World” void loop() { 6 digitalWrite(13, HIGH); 7 // set the LED on delay(1000); 8 // wait for a second digitalWrite(13, LOW); 9 // set the LED off delay(1000); // wait for a second }
Let’s review each section of the code. 1 A slash followed by an asterisk (/*) opens a block of comments (they are ended by a */). Two forward slashes (//) indicate that the rest of the line is a comment. Comments consist only of information for the user; Arduino does not react to them. Comments will be removed during the compiling process, and they are not uploaded into the Arduino. This comment states the title and purpose of the program (known as a sketch). Most comments describe the purpose of a specific line or block of code. 2 The setup() function executes once in the beginning of a program, where one-time declarations are made. The setup() function will be called automatically immediately after the Arduino is powered or has been programmed. Calling a function consists of a return value of a function (void), the name of the function (setup), and a list of parameters in parentheses. This function does not take parameters, so there is nothing inside the parentheses. This function does not return values, so its type is void (empty). When calling a function, commands are listed within a block of code, which is enclosed in curly braces ({}). An easy way to familiarize yourself with new functions is to highlight the name of the function (always in orange text), right-click, and select “Find in Reference.” Here you will find an explanation of usage, syntax, and an example.
3 This setup() function includes only one command, which sets the pin connected to the LED into output mode. It calls the pinMode() function, which is defined in the Arduino libraries. 4 OUTPUT is a constant defined within Arduino. When using digital pins on an Arduino, you must always declare them to be in either OUTPUT or INPUT mode. 5 As is required by the C language (which Arduino is based on), a line ends with a semicolon. 6 The majority of the execution time of the program will repeat the loop() function. The loop() function is called automatically (and repeatedly) after the setup() function finishes. 7 To address a digital pin that has been set to be an OUTPUT, we use the digitalWrite() function. Set digital pin 13 on HIGH (which means +5V). Digital pin 13 is unique in that Arduino has a built-in LED and resistor attached to it, specifically for debugging purposes. The LED will light up. The pin remains on HIGH until we address the pin with another call from digitalWrite(). 8 Wait 1,000 milliseconds (1,000 one-thousandths of a second, totaling one full second). The LED is lit all the time. 9 Switch the pin off (LOW) and wait again for a second. This is the last command in the loop() function, which means that the execution of the loop() function ends.
26
Chapter 2
Arduino Uno
The program will call the loop() function over and over again automatically. The execution will continue from the first line of the loop() function, which sets the LED (ledPin) on HIGH. The execution of the program continues by repeating the loop() function until it is stopped by disconnecting power from the Arduino.
Arduino Uno The Arduino Uno (Figure 2-9) is a good choice for your first Arduino. It is inexpensive and reliable. The Uno will use the power provided by the USB cable when the cable is connected to a computer. If necessary, it can also be powered by an external power supply such as a battery. When you upload code from your computer, the program is saved to the microcontroller itself. This means you can disconnect the Arduino and allow it to function as an independent device. The Uno’s pins have female headers that enable you to connect wires without soldering. This speeds up the building of simple prototypes but is not a very good longer-term solution, because cables can fall off relatively easily.
Figure 2-9. Arduino Uno
Arduino: The Brains of an Embedded System
27
Arduino Nano
Arduino Nano The Arduino Nano (Figure 2-10) is considerably smaller than the Uno mentioned earlier. It also has pins that you can connect straight onto a prototyping breadboard. These allow you to easily construct even quite complex circuits without soldering.
Figure 2-10. Arduino Nano
Nano is more expensive and sensitive than Uno. For example, a certain kind of short circuit will break Nano permanently. Another downside is that it is harder to read the markings on the pins, making it easier to misplace wires. With the addition of a mini breadboard and retractable USB cable, the Arduino Nano becomes part of a handy travel pack (see Figure 2-11). The Arduino Pro Mini (see Chapter 8 for more details) is even smaller than the Nano. It does not include a USB-serial adapter onboard, so you need to use a separate USB-serial adapter to program it. However, it is extremely small and lightweight, and comes in low-power variants, which makes it an ideal choice for projects where weight is a significant issue, such as airborne drones or remote-controlled vehicles.
Now you know the basics to get started with your first project. In the next chapter, you will try Arduino in practice while building the Stalker Guard. Figure 2-11. Arduino travel pack
28
Chapter 2
Stalker Guard
In this project, you will build a Stalker Guard (Figure 3-1), a simple alarm device that measures the distances of objects behind you and vibrates when something comes too close (see Figure 3-2). You will also learn a program you can easily modify for other projects that works by monitoring data sent by a sensor and reacting when specific conditions are filled.
3 In this chapter What You’ll Learn Tools and Parts Solderless Breadboard Jumper Wire Ping Ultrasonic Sensor Vibration Motor Combining Components to Make the Stalker Guard
Over the course of this project, you will learn the basics of Arduino programming, distance measurement with ultrasonic sensors, and motor control. The Stalker Guard can be easily customized into new variations by changing the values of the ultrasonic sensor, replacing the sensor with another one, or replacing the motor with, say, a speaker. With little effort, you can develop the circuit further—for example, by turning it into a height meter.
Making the Motor Vibrate Providing Power from a Battery What’s Next? Making an Enclosure
Figure 3-1. The finished Stalker Guard
29
Tools and Parts
Before starting the project, you’ll need to install the Arduino development environment and make sure you can run the “Hello World” program from Chapter 2. The project is organized in steps, so you can always return to the previous step if you find that something isn’t functioning correctly. Remember that you can download the complete code examples for each stage at http://BotBook .com/ or http://examples.oreilly.com/0636920010371, which allows you to test their functions before reading the individual descriptions of each program.
Figure 3-2. The Stalker Guard in action
What You’ll Learn In this chapter, you’ll learn how to: • Measure distance using the PING))) ultrasonic sensor • Use a motor • Apply the principles of Arduino programming • Power Arduino from a battery
Tools and Parts You’ll need the following tools and parts for this project (Figure 3-3). Manufacturer part numbers are shown for: • Maker SHED (US: http://makershed.com): SHED • Element14 (International and US; formerly Farnell and Newark, http://element-14 .com): EL14 • SparkFun (US: http://sparkfun.com): SFE
30
Chapter 3
Solderless Breadboard
Figure 3-3. Parts and tools used in this chapter
1. Solderless breadboard (SHED: MKEL3; EL14: 15R8319; SFE: PRT-00112). 2. Arduino Nano (SHED: MKGR1; http://store.gravitech.us; or http://store .gravitech.us/distributors.html). 3. PING))) ultrasonic sensor (SHED: MKPX5; http://www.parallax.com/Store/). 4. Vibration motor (SFE: ROB-08449). If you can’t find a vibration motor anywhere, you can replace it with an LED. These motors can also often be salvaged from broken cell phones. 5. Jumper wires, at least three colors (SHED: MKEL1; EL14: 10R0134; SFE: PRT00124). 6. Wire strippers (EL14: 61M0803; SFE: TOL-08696). 7. Diagonal cutter pliers (EL14: 52F9064; SFE: TOL-08794). 8. 9V battery clip (EL14: 34M2183; SFE: PRT-00091).
Solderless Breadboard To prototype circuits without soldering, you can use a solderless breadboard. To connect components, you’ll push their legs (leads) into holes on the board. In Figure 3-4, two pairs of bus strips (power rails) are connected horizontally on the top and bottom of the board. These are typically used for providing power (positive voltage, often labeled VCC; and ground, labeled GND) access to the entire board. The terminal strips (center area of the board) are connected vertically. This area is where you will mount most components. You can remove parts from the board by gently pulling them out, which makes it easy to change circuits.
Stalker Guard
31
Solderless Breadboard
Note the power terminals at the left end of the breadboard in Figure 3-4. These are useful, but you won’t find them in every breadboard. If your breadboard does not have power terminals, you can push power leads into any of the holes in the appropriate horizontal row to provide power to all the other leads along that row. We won’t use power terminals in this project.
Figure 3-4. The holes in the prototyping breadboard are connected to each other by vertical rows; bus strips for power are connected horizontally
Figure 3-5 shows the breadboard with its bottom cover removed (don’t do this to your breadboard, as it tends to damage it) so you can see more clearly how the rows are connected to each other.
Figure 3-5. View of the bottom of the prototyping breadboard; metal plates conduct current from hole to hole
So how would you connect the Arduino Nano’s D5 (digital pin 5) pin to the second wire of a motor? Insert the Arduino into the middle of the prototyping board (also known as the “gutter”), so that it is straddling the vertical rows between both sides of the Arduino legs. This prevents us from short-circuiting the opposite side legs of the Arduino. Push the Arduino into the board so that the Arduino’s D5 pin is connected to each hole of the strip on the same side of the gutter (highlighted in Figure 3-9, which appears later in this chapter). Then simply insert the motor wire in the hole next to the Arduino D5 pin. Now the motor wire and Arduino pin D5 are connected to each other.
Figure 3-6. Small prototyping breadboard
It’s always a good idea to begin a project by building circuits on a prototyping breadboard. Once you’re sure that a project functions correctly, you can then consider a more permanent place for it. Even in the next stage, you don’t need to solder things together. For example, you can leave the more expensive parts, such as the Arduino itself, on a smaller prototyping breadboard (Figure 3-6). This way, you can easily use it in other projects and then put it back in its original place.
32
Chapter 3
Ping Ultrasonic Sensor
Jumper Wire We’re going to use jumper wire—a thin, single-strand electrical wire—to make connections to the prototyping breadboard. You’ll need an adequate amount of visible conductive metal to connect the jumper wire deeply enough; about ¼ inch of exposed wire is sufficient. Jumper wires (also known as hookup wire) are sold in ready-cut assortments (Figure 3-7), but you can also cut and strip them yourself from suitable wire. Generally, 22AWG solid-core wire is best. Using a different-colored wire for each distinct purpose can be useful. For example, use a red wire for power, and black for ground (0V, GND). Wires hooked to data pins often use other colors, such as blue or green. This method makes the circuit easier to comprehend.
Ping Ultrasonic Sensor An ultrasonic sensor functions on the same principles as radar: it transmits a high-frequency signal and, based on the echo, determines the proximity of a specific object. The frequency is outside the human audible range, so you won’t hear a thing. Ultrasonic sensors can measure the distance of an object accurately at a minimum of 2 centimeters and a maximum of 3 meters from the device. This type of sensor functions well when you need to know not just whether something exists in front of an object, but also at what proximity. Lights don’t affect ultrasonic sensors, so the sensors can function in complete darkness. On the other hand, there is a chance the sensor won’t detect reflective surfaces or objects located at steep angles, because the sound wave won’t bounce back from them. Also, very small or soft objects might reflect back such minuscule amounts of the sound that the sensor will not detect them. The Parallax Ping))) Ultrasonic Sensor is shown in Figure 3-8.
Figure 3-7. Jumper wires
Metric Units Because this book started life as a Finnish book, and because Arduino is an international phenomenon, we stuck with metric (also known as Système International, or SI) measurements throughout. For easy conversions, you can type any measurement into a Google search, along with the desired output unit, such as “23 cm in inches,” and the conversion calculation will appear in the results. In fact, you can get fancy, such as “29.112 microseconds per centimeter in mph,” for which Google returns “768 389768 mph” (which is in fact roughly the speed of sound at 20 degrees C). For a wider variety of calculations, check out Wolfram Alpha (http:// www.wolframalpha.com/), which can not only tell you “speed of sound at 20 degrees Celsius,” but will also let you tweak the results to take into account air pressure and humidity!
Figure 3-8. The PING))) ultrasonic sensor
Stalker Guard
33
Ping Ultrasonic Sensor
One negative aspect of ultrasonic sensors is their relatively high price ($30 at the time of this writing).
Measuring Distance with the Ping Ultrasonic Sensor To begin creating the program, first connect the Arduino and upload the Blink code shown in Chapter 2. This brief exercise is always useful when you’re starting a new project, because it confirms that the development environment still functions properly. Insert the Arduino Nano in the middle of the prototyping breadboard, as shown in Figure 3-9. Pressing the Nano in all the way might require a bit of force, so be careful not to bend its pins. Next, connect the PING))) Ultrasonic Sensor to the Arduino by inserting it into the prototyping breadboard and connecting the Arduino pins to its pins with jumper wires.
Figure 3-9. Arduino Nano on a breadboard
As shown in Figure 3-10, assignments for the three pins under the sensor are marked on the front of the sensor board. Remember, GND is an acronym for ground, or 0V. When a circuit includes a GND symbol, it is the same as connecting that part of the circuit to the negative terminal of the power supply. All grounds of a circuit are connected to one another and have the same voltage potential. Voltage always refers to a difference in electrical potential between two parts of a circuit. If only one voltage is given—for example, the +5V of the positive terminal of a power source such as a battery, or (in this case) an Arduino powered from USB—this voltage is compared to ground (GND), the negative terminal of the power source. The sensor’s GND pin will be connected to the Arduino’s GND pin. The sensor’s 5V pin will receive power, and it is connected to the 5V pin of the Arduino.
34
Chapter 3
Ping Ultrasonic Sensor
Figure 3-10. Arduino Nano pin assignments
The last leg of the sensor is marked with the abbreviation SIG (for signal). This pin will be connected to the Arduino digital pin 2 (D2). Through this connection, the Arduino will receive the data sent by the sensor and control it. Insert the PING))) ultrasonic sensor into the prototyping breadboard close to the Arduino (see Figure 3-11), but don’t let the Arduino and PING))) overlap any pins. Since the holes in the prototyping board are separated vertically, you must attach the pins of the PING))) sensor to the breadboard horizontally, each in its own vertical row. Now the Arduino and PING))) sensors are connected to the prototyping breadboard, but they’re not connected to each other yet. Let’s do that now:
Be careful when you connect wires to the Nano, because the pin assignments are printed so close together that it is easy to misread them. Each pin’s label is printed above it, except the upper pins, which have their assignments written next to them. As of the Arduino Nano 3.0 release, all labels are printed adjacent to the pins and are much easier to read.
1. Connect the Arduino ground pin (GND) and PING))) ground pin to each other. To do this, connect one end of a black jumper wire to the same vertical row as the Arduino’s GND pin. Connect the other end of the black jumper wire to the same vertical row as the PING))) GND pin. 2. Connect the Ping sensor’s positive pin (5V) and the Arduino +5V to each other. To do this, connect one end of a red jumper wire to the same vertical row with the Arduino +5V pin. Connect the other end of the red jumper wire to the same vertical row as the Ping sensor’s +5V pin. 3. Connect the PING))) SIG (signal) data pin and the Arduino digital pin D2 to each other. To do this, connect one end of a green (or blue, or yellow—anything but red or black) jumper wire to the same vertical row with the Ping SIG pin. Connect the other end of the green jumper wire to the same vertical row as the Arduino D2 pin.
Stalker Guard
35
Ping Ultrasonic Sensor
Figure 3-11 shows how the breadboard should appear; Figure 3-12 shows this circuit’s schematic.
Figure 3-11. A connected ultrasonic sensor
Figure 3-12. Schematic for the ultrasonic sensor circuit
Distance-Measuring Program Open the Arduino development environment and connect the Arduino to the computer’s USB port. Feed the following code to Arduino by saving it into a new sketch and clicking Upload. This code is based on the Ping example that comes with the Arduino IDE (File→Examples→6. Sensors→Ping). /* Ping))) Sensor This sketch reads a PING))) ultrasonic rangefinder and returns the distance to the closest object in range. To do this, it sends a pulse to the sensor to initiate a reading, then listens for a pulse to return. The length of the returning pulse is proportional to the distance of the object from the sensor. The circuit: * +V connection of the PING))) attached to +5V * GND connection of the PING))) attached to ground * SIG connection of the PING))) attached to digital pin 2 http://www.arduino.cc/en/Tutorial/Ping created 3 Nov 2008 by David A. Mellis modified 30 Jun 2009 by Tom Igoe modified Nov 2010 by Joe Saavedra */ const int pingPin = 2; 1 long duration, distanceInches, distanceCm; 2
36
Chapter 3
Ping Ultrasonic Sensor void setup() { Serial.begin(9600); 3 } void loop() { pinMode(pingPin, OUTPUT); 4 digitalWrite(pingPin, LOW); delayMicroseconds(2); digitalWrite(pingPin, HIGH); delayMicroseconds(5); digitalWrite(pingPin, LOW); pinMode(pingPin, INPUT); 5 duration = pulseIn(pingPin, HIGH); distanceInches = microsecondsToInches(duration); 6 distanceCm = microsecondsToCentimeters(duration); Serial.print(distanceInches); 7 Serial.print('in, '); Serial.print(distanceCm); Serial.print('cm'); Serial.println(); delay(100); } long microsecondsToInches(long microseconds) { return microseconds / 74 / 2; 8 } long microsecondsToCentimeters(long microseconds) { return microseconds / 29 / 2; 9 }
Let’s look at what the code does: 1 This constant won’t change. It’s the pin number of the sensor’s output. 2 Establish variables for the duration of the ping and the distance result in inches and centimeters. 3 Initialize serial communication at 9,600 bits per second. 4 The PING))) is triggered by a HIGH pulse of 2 or more microseconds. Give a short LOW pulse beforehand to ensure a clean HIGH pulse. 5 The same pin is used to read the signal from the PING))): a HIGH pulse whose duration is the time (in microseconds) from when the ping is sent to when its echo off an object is received. 6 Convert the time into a distance. 7 Print the calculations for inches and centimeters to the Serial Monitor. 8 According to Parallax’s datasheet for the PING))), there are 73.746 microseconds per inch (i.e., sound travels at 1,130 feet per second). This gives the distance traveled by the ping, outbound, and return, so we divide by Stalker Guard
37
Ping Ultrasonic Sensor
2 to get the distance of the obstacle. The next function, microseconds ToCentimeters(), is explained next. It performs this calculation using metric (also known as SI) units, which is used worldwide. 9 The speed of sound is 340 m/s or 29 microseconds per centimeter. The ping travels out and back, so to find the distance of the object we take half of the distance traveled. See http://www.parallax.com/dl/docs/prod/ acc/28015-PING-v1.3.pdf.
Adjusting for Air Temperature The PING))) example included with Arduino includes the number 29 in its microsecondsToCentimeters() function. Where does this number come from? Let’s take a look: The speed of sound in meters per second (m/s) at a given air temperature t (degrees Celsius) is calculated with this formula: 331.5 + 0.6 * t With a temperature of 20° C, that’s: 331.5 + 0.6 * 20 = 331.5 + 12 = 343.5 m/s Let’s convert speed to microseconds per centimeters. Start by converting to centimeters per second: 343.5 *100 = 34350 cm/s In microseconds (μs), that’s: 34350 / 1000000 = 0.03435 cm/us Speed can be expressed as a pace, that is, how much time it takes to travel a given distance. Let’s convert speed to pace, us/cm: 1/0.03435 = 29.112 If you plan to use this sketch in a location where the air temperature is much different than 20° C, you should perform these calculations with the appropriate air temperature. Note that if you intend to use the microsecondsTo Inches() function, you will need to convert your results accordingly.
Once you have uploaded the code successfully, the green light in front of the ultrasonic sensor should start blinking. Click the Arduino development environment’s Serial Monitor button (shown in Figure 3-13). Now the distance values measured by the sensor should appear in the console on the bottom of the page. Move your hand in front of the sensor and observe how the values change.
38
Chapter 3
Vibration Motor
Figure 3-13. The Arduino development environment’s Serial Monitor button
Vibration Motor A vibration motor, shown in Figure 3-14, is an ordinary DC (direct current) motor with an asymmetric weight. Asymmetrical rotation of the motor makes it vibrate. Think of the last time you had to balance a load of laundry in a washing machine: as long as that wet towel is stuck to one side of the drum, the washing machine is going to shake. The vibration motor works the same way.
Troubleshooting If you were not able to measure any values from the sensor, find out where the problem occurred before moving forward. First, try uploading the Blink code from Chapter 2 into the Arduino. This way, you can make sure that none of the cables is disconnected and that the data is still communicating properly. Next, make sure all the jumper wires are connected to appropriate pins in the Nano and in the sensor. If this still does not help, test the jumper wires by measuring their resistance, using the method described in “Measuring Resistance with a Multimeter” in Chapter 5.
Figure 3-14. Vibration motor
You can find vibration motors at electronics stores (see “Parts,” earlier in this chapter) or salvage one from an old device, such as a cell phone or video game controller with force feedback. The motor in Figure 3-14 came from a PlayStation controller. It is also helpful to keep the frame the motor was attached to. Not all vibration motors have a visible rotating part; in some, the part is packed inside a casing.
Stalker Guard
39
Vibration Motor
Connect the red motor wire to the Arduino digital pin number 5 (D5). Connect the black motor wire to the GND terminal row of the Arduino. If the wire is a multistrand (stranded) wire, it might fit too loosely in the prototyping breadboard. You can create a more secure attachment by using pliers to stick a small piece of metal wire into the same breadboard holes as the motor wire. Alternatively, you can solder the stranded wire to a piece of 22AWG solid-core wire and insert this into the breadboard. Figure 3-15 shows the vibration motor connected to the Arduino, and Figure 3-16 shows the schematic.
Figure 3-15. Connecting a vibration motor
Figure 3-16. Vibration motor schematic
You can get the motor to turn by uploading the following code into the Arduino: // dcMotor.pde - Run DC-motor to one direction // (c) Kimmo Karvinen & Tero Karvinen http://botbook.com int motorPin = 5; 1 void setup() 2 { pinMode(motorPin, OUTPUT); 3 digitalWrite(motorPin, HIGH); 4 } void loop() 5 { delay(100); }
When the motor receives enough power, it will start rotating. Let’s go through the code: 1 Set the value of the motorPin variable to 5. This makes the code easier to read because, instead of using a number, you can use the motorPin variable in certain parts of the code. 40
Chapter 3
Making the Motor Vibrate
2 Execute the setup() function only once, in the beginning. This function does not return any value; therefore, its type is empty (void). 3 Switch pin D5 to an OUTPUT state. An Arduino pin can be set either as an output or an input. Because you would like to send power to the motor, choose OUTPUT. 4 Turn on power to the D5 pin. The pin remains HIGH unless we set it to LOW, which makes the motor rotate continuously. 5 The tasks your sketch performs will be written within the loop() function. An ordinary Arduino sketch spends most of its time repeating a loop. Since there is nothing to do in the loop() function, it is empty.
Combining Components to Make the Stalker Guard Now you know how the necessary components work by themselves. Next, you will combine them to create the Stalker Guard. First, connect the circuits for the motor and the ultrasonic sensor. Figure 3-17 shows them connected on the breadboard, and Figure 3-18 shows the schematic. Once you’ve connected them, it is wise to retry the code tested earlier for both devices, so you are testing only a single component at a time. This way, you ensure that you built the circuit correctly, which also makes troubleshooting easier in later stages.
Figure 3-17. Vibration motor and ultrasonic sensor connected
If you can’t find a vibration motor, you can use an LED as an indicator; replace the motor with an LED and a 220Ω resistor. (The Greek omega, Ω, is the symbol for ohm, the unit of resistance.)
Figure 3-18. Vibration motor and ultrasonic sensor schematic
Making the Motor Vibrate Now you will combine both of the sketches. To find out when a target is close, we’ll make the vibration motor react when the distance to a target is below the specified limit: /* stalkerguard.pde - Shake if something comes near (c) Kimmo Karvinen & Tero Karvinen http://botbook.com updated 2010 – Joe Saavedra; based on code by David A. Mellis and Tom Igoe */
Stalker Guard
41
Making the Motor Vibrate const int pingPin = 2; const int motorPin = 5; long int duration, distanceInches, distanceCm; int limitCm = 60; 1 void setup() { pinMode(motorPin, OUTPUT); } void loop() { pinMode(pingPin, OUTPUT); digitalWrite(pingPin, LOW); delayMicroseconds(2); digitalWrite(pingPin, HIGH); delayMicroseconds(5); digitalWrite(pingPin, LOW); pinMode(pingPin, INPUT); duration = pulseIn(pingPin, HIGH); distanceInches = microsecondsToInches(duration); distanceCm = microsecondsToCentimeters(duration); checkLimit(); 2 delay(100); } void checkLimit() { if (distanceCm < limitCm){ 3 digitalWrite(motorPin, HIGH); } else { 4 digitalWrite(motorPin, LOW); } } long microsecondsToInches(long microseconds) { return microseconds / 74 / 2; } long microsecondsToCentimeters(long microseconds) { return microseconds / 29 / 2; }
This program combines the earlier ultrasonic sensor and vibration motor code to determine whether the readings from the ultrasonic sensor are less than the defined threshold distance. If the distance is shorter, the motor will switch on. 1 Add this variable to define the distance; when something gets this close, we will start the motor. 2 Now that we have our current distance calculated, call the checkLimit function. 3 If the distance is shorter than the defined threshold distance, switch on power to the motor pin. 42
Chapter 3
What’s Next?
4 Otherwise, switch off the motor pin.
Providing Power from a Battery Wearing the Stalker Guard when it’s connected to a USB cable is a bit difficult, but you can free the Arduino from cables by attaching a battery to power it. After uploading your code to the board, attach the positive (red) wire of the battery clip to the Arduino voltage in pin (VIN) terminal row of the prototyping breadboard. Similarly, attach the negative (black) wire to any Arduino GND pin. See Figures 3-19 and 3-20. Now the device will function via battery without the USB cable power. (When running off an external power source, the Nano needs at least 7V, but you should not give it more than 12V.)
Figure 3-19. A battery connected to the Arduino
Figure 3-20. Arduino battery connection schematic
What’s Next? Now you have built your first prototype with the Arduino. Next, we will present one possible way to create an enclosure for the Stalker Guard. The proposed enclosure methods will give you suggestions on how to construct devices until the prototyping phase. Devices functioning on a prototyping breadboard do not necessarily make a major impression, except maybe on the most die-hard geeks. If your fingers are already itching to start building the next project, you can skip the enclosure stage and move on to the Robot Insect in Chapter 4.
Ideas for Future Projects In the meantime, you can apply the skills you have learned to other projects. Here are a few ideas:
Stalker Guard
43
Making an Enclosure
• Posture Watchdog: a device that warns you if you are leaning too close to your computer screen • A distance-measuring device • A robot that vibrates when you get close to it (see Figure 3-21)
Figure 3-21. Vibrating robot, still lacking the ultrasonic sensor
Making an Enclosure In the following sections, we’ll outline one way to enclose the Stalker Guard to attach it to your waist and use it without wires. You don’t need to follow these instructions exactly. Use them as a guideline and supply your own creativity and available parts.
Utilizing an Ammo Pouch Figure 3-22. The ammo pouch used as the basis for our enclosure
As a starting point, we took an old ammo pouch purchased from an army surplus store (Figure 3-22). The pouch is made of rubber-coated linen fabric, which is relatively easy to perforate and cut. And its price—one euro—did not damage our budget.
44
Chapter 3
Making an Enclosure
Start by punching a hole for the screw, which will be used to attach the vibration motor cradle (Figure 3-23). If you do not have a ready-made cradle for the vibration motor, you can hot-glue it to the enclosure. The easiest way to create the hole is to use an electric drill, but you can also use a punch tool.
Figure 3-23. Hole created for attaching the vibration motor
Screw the cradle in place and attach the motor to it, as shown in Figures 3-24 and 3-25.
Figure 3-24. The motor cradle in place
Figure 3-25. The vibration motor in place
Mark the position for the ultrasonic sensor on top of the enclosure (Figure 3-26). You can create the holes with a mini-drill sanding bit, as shown in Figure 3-27. If you don’t have a sanding bit, you can use a carpet knife.
Stalker Guard
45
Making an Enclosure
Figure 3-26. The marked positions for the ultrasonic sensor
Figure 3-27. Holes created with a mini drill
Hot-glue the ultrasonic sensor in place, as shown in Figures 3-28 and 3-29. If you would like to be able to detach it later for other uses, you could use duct tape instead of glue.
Figure 3-28. A hot-glued ultrasonic sensor
Figure 3-29. The ultrasonic sensor in place
Wiring Up the Circuit The circuits presented earlier in this chapter were made with a small prototyping breadboard. At this stage, you could also consider soldering the device into one single package. However, we did not want to permanently attach the Arduino and the ultrasonic sensor to the Stalker Guard, so we went with a simpler solution: a servo extension cable (Figure 3-30), which has a female connector suitable for attaching to the sensor pins at each end. Cut off the connector from one end (Figure 3-31) and solder single-strand jumper wires to the wire, as shown in the next section, “Soldering Basics.” By using an extension cable, you can attach the sensor tightly without destroying it.
46
Chapter 3
Making an Enclosure
Figure 3-30. Servo extension cable
Figure 3-31. Connector removed from one end of the servo extension cable
You should also solder single-strand jumper wires to the vibration motor to make it stay connected better.
Soldering Basics The projects in this book require a few simple solderings, such as attaching wires to each other and to the battery clip. While doing the exercises, you’ll get more experience with soldering, and possible mistakes won’t end up causing major problems like broken components. As with any tool, you must use eye protection when working with solder. Regardless of what you are soldering, always try to position parts so that they are in place as securely as possible. This makes working so much easier. If you are connecting two wires to each other, twist together the ends to be soldered, as shown in Figures 3-32 and 3-33. If you’re using stranded wire, twist the strands together first. It is harder to solder strands that point in all directions.
The Make: Electronics Deluxe Toolkit includes everything you need to get started, including hand tools, soldering iron, soldering stand and sponge, as well as wire, solder, and a multimeter: http://www. makershed.com/ProductDetails. asp?ProductCode=MKEE1.
Figure 3-32. Wires with ends twisted together
Stalker Guard
47
Making an Enclosure
Figure 3-33. Wires supported solidly by a holder
Swipe the hot soldering iron with a wet sponge to get rid of any excess solder, as shown in Figure 3-34. Do not flick solder off the iron.
Figure 3-34. Swiping the soldering iron with a wet sponge
Heat the parts to be soldered quickly—no more than one second (Figure 3-35). Heating for too long can damage parts that are not meant to be heated, destroying sensitive components or melting plastic parts. It is good to practice with wire first.
48
Chapter 3
Making an Enclosure
Figure 3-35. Heating the parts for approximately one second
With the soldering iron still touching the parts to be soldered, push the solder so that a suitable amount of it flows in (Figure 3-36); this should take one second. Take the solder away from the joint and then remove the soldering iron as well (Figure 3-37). When you have had enough practice, the whole process will take only two or three seconds.
Do not touch the wire or component that you are soldering. It can get hot enough to hurt or give you a small burn.
Figure 3-36. Adding solder
Stalker Guard
49
Making an Enclosure
Figure 3-37. Removing the soldering iron and solder from the joint
It is important to heat the parts to be soldered to a high enough temperature to melt the solder. If hot solder is just dropped on the wires to be soldered, you will create a cold solder joint. Cold solder joints might not conduct electricity properly and will probably fail. Figure 3-38 shows the finished solder joint. Figure 3-39 shows wires attached to the servo extension cable.
Figure 3-38. Finished soldered joint
Figure 3-39. Single-strand wires soldered to the servo extension cable and to the vibration motor
50
Chapter 3
Making an Enclosure
Soldering might be a bit uncomfortable at first, but just like with all other mechanical tasks, practice makes perfect.
Using a Switch to Save Batteries This section shows you how to install a switch between the negative cable of the battery clip and the Arduino, so that the Arduino won’t consume batteries while the device is not in use (Figure 3-40). This way, you won’t have to open the enclosure to operate the Stalker Guard; you can just turn it on and off with the switch. Connect the positive (red) wire of the battery clip to the Arduino +5V pin in the prototyping breadboard. Connect the negative (black) wire from the battery clip to one pin of the switch. Connect the other pin of the switch to the Arduino GND pin. Now Arduino gets its power from the battery when the switch is in the correct position. Drill a hole in the enclosure for screwing in the switch. It is helpful to place heat-shrink tubing (rubber tubing that will shrink in diameter by 50% when heated) on the soldered wires (Figure 3-41) to avoid shorts inside the enclosure. Put a piece of heat-shrink tubing over the area and heat it with a heat gun or hair dryer, as shown in Figure 3-42. Be sure to do this in an area with adequate ventilation.
Figure 3-40. A switch attached to the enclosure
If your heat gun uses an external flame, be careful to not overheat the tubing or you may melt the insulation of the wires or damage nearby components.
Figure 3-41. All components attached together; heat-shrink tubing covers the soldered points
Figure 3-42. Attaching the heat-shrink tubing
You can place larger heat-shrink tubes over smaller ones to keep the wires neatly together, as shown in Figure 3-43.
Stalker Guard
Figure 3-43. All components attached together; larger heat-shrink tubing neatly covers the individual soldered points covered by smaller heat-shrink tubing
51
Making an Enclosure
You can purchase heat-shrink tubing from electronic suppliers, including many RadioShack stores. If you don’t have any heat-shrink tubing immediately available, electrical or insulating tape can also get the job done, but don't use a heat gun on the tape.
Figure 3-44 shows the Stalker Guard schematic, Figure 3-45 shows the finished Stalker Guard, and Figure 3-46 shows the device in use.
Figure 3-44. Connection diagram
Figure 3-45. Finished Stalker Guard
Figure 3-46. The Stalker Guard in use
52
Chapter 3
Insect Robot
In this project, you’ll create an Insect Robot (see Figure 4-1) that walks forward on four legs. Using ultrasound, the robot can see in the dark, just like a bat. When it detects an obstacle, it takes a few steps back, turns, and continues forward. The robot can walk over small obstructions, and it looks more human than its wheeled relatives. Once you’ve learned the techniques in this chapter, you can easily extend your Insect Robot with new tentacles and sensors—and new code.
4 In this chapter What You’ll Learn Tools and Parts Servo Motors Constructing the Frame Programming the Walk Avoiding Obstacles Using Ultrasound What’s Next?
Before starting this project, you should know what ultrasonic sensors are, and make sure that the “Hello World” Blink code from Chapter 2 is working properly with your Arduino. You’ll learn about some new things, including servo motors (motors that can be manipulated to rotate to a specific angular position), in this chapter. We will build a body for the insect by gluing two servos together and shaping legs for it from a wire clothes hanger. Arduino will turn the two servos one at a time, which moves each pair of metal legs like real legs so the insect can crawl forward. We will also make holders for the battery and Arduino so our insect can behave autonomously. The insect will need eyes to react to its environment. We will connect an ultrasonic sensor to the insect’s head to enable the robot to precisely measure its distance from objects in front of it. Finally, we will teach the insect to react to an obstacle by backing up and turning. The distance of the obstruction will trigger a series of commands that make the robot back up several steps, turn, and move forward several more steps. Then our Insect Robot will be able to move and avoid obstructions on its own. After you have spent some time observing your new pet’s movements, you can add new sensors to the robot or teach it new tricks.
Figure 4-1. The completed Insect Robot
53
Tools and Parts
What You’ll Learn In this chapter, you’ll learn: • How to control servos • The basics of robot walking • How to construct mechanical structures for prototypes • How to build a walking robot based on two servos
Tools and Parts Manufacturer part numbers are shown for: • Maker SHED (US: http://makershed.com): SHED • Element14 (International and US; formerly Farnell and Newark, http://element-14.com): EL14 • SparkFun (US: http://sparkfun. com): SFE
You’ll need the following tools and parts for this project (Figure 4-2).
Figure 4-2. The tools and parts you need to build the insect
1. 9V battery clip (EL14: 34M2183; SFE: PRT-00091). 2. Two small metal rods. You could salvage these from other devices, such as an old typewriter. If you have metal snips and a small amount of sheet metal, you could also cut them yourself (but be sure to use a metal file or metal sandpaper to smooth the edges, which will be extremely sharp). 3. Heat-shrink tubing (14cm) for the feet (EL14: 90N7288). Hot glue works well, too. 4. 28cm and 25cm pieces from a wire clothes hanger. 5. Two pairs of pliers. 6. Wire strippers (EL14: 61M0803; SFE: TOL-08696). 54
Chapter 4
Servo Motors
7. Multihead screwdriver. 8. Diagonal cutter pliers (EL14: 52F9064; SFE: TOL-08794). 9. Two large servo motors (SFE: ROB-09064; http://parallax.com/Store: 90000005). 10. Two nuts and bolts. 11. 9V battery. 12. Servo extension cable (SFE: ROB-08738; http://parallax.com/Store: 80500002). 13. Red, black, and yellow (or some other color) jumper wire (SHED: MKEL1; EL14: 10R0134; SFE: PRT-00124). 14. A butane torch or a cigarette lighter. 15. Thin metal wire. 16. Hot-glue gun and hot glue (available at craft stores or office supply stores). 17. PING ))) ultrasonic sensor (SHED: MKPX5; http://www.parallax.com/Store/). 18. Arduino Uno (SHED: MKSP4; EL14: 13T9285; SFE: DEV-09950). The older Duemilanove is pictured here and would work just as well. 19. Hook-and-loop fastener tape, such as Velcro.
Servo Motors Servo motors (Figure 4-3) come in different sizes and prices and are based on different technologies. In this context, we are talking about hobby servos, the kind used in remote-control cars, for example. Servo motors have a servo controller that directs the position of the motor wherever we want.
Figure 4-3. Two servo motors
Insect Robot
55
Servo Motors
The motor itself is a DC (direct current) motor with gears. Servo motors usually rotate rather slowly and with a relatively strong torque. You can buy hobby servo motors with either limited rotation or continuous rotation. Limited rotation models work for most purposes, and you can control their movement quite precisely by degrees of rotation. In continuous rotation servos, you can control only speed and direction. We’ll cover continuous rotation servos in Chapter 8. The colors can vary depending on the motor, but generally, the darkest color is GND, red is power, and the next lightest color is the data wire.
Wiring Up the Circuit Connect the servo to the Arduino by attaching the servo’s black wire to any of the Arduino GND pins. The red wire indicates positive voltage; connect it to the Arduino +5V pin. The white or yellow data wire controls the servo; you will connect it to one of the digital pins. For this project, connect the data wire to the first available digital pin: digital pin 2 (D2). Figure 4-4 shows the connection, and Figure 4-5 shows the schematic. Arduino uses D0 and D1 to communicate over USB or serial, so it’s best to use those to connect things to your project only when absolutely necessary.
Figure 4-5. Arduino/servo schematic
Figure 4-4. Servo connection
Using the Servo Library in Arduino Libraries are collections of subroutines or classes that let us extend the basic functionality of a platform or language such as Arduino. There are many different libraries that help us interpret data or use specific hardware in much simpler and cleaner ways. You can explore the libraries available for Arduino at http://arduino.cc/en/Reference/Libraries. As these libraries are meant to extend 56
Chapter 4
Servo Motors
our code only when needed, we must declare each library in any sketch where one will be used. We do this with a single line of code. Here’s how to include the library for controlling servo motors: #include
Now we can reference methods and objects from within that library at any time in our sketch. We will be using the Servo library to interface with our motors in this chapter. The Servo library comes with a standard installation of Arduino and can support up to 12 motors on most Arduino boards and 48 motors on the Arduino Mega. For each servo motor we are using, we must create an instance of the Servo object with Servo myServo;. In the setup() function, we must associate this instance of Servo to a specific pin, the same pin to which the data wire of our motor is attached, using the command myServo.attach(2);. Now talking to our motor is easy. There are several functions for communicating with it, including read(), write(), detach(), and more, all of which you can explore in the library reference at http://arduino.cc/en/Reference/Servo. For this chapter, when talking to our motors, we will use only the write() function, which requires a single argument: degree of rotation.
Centering the Servo This example shows how we use the Servo library to connect a single servo motor and rotate it toward the absolute center point. // servoCenter.pde - Center servo // (c) Kimmo Karvinen & Tero Karvinen http://BotBook.com // updated – Joe Saavedra, 2010 #include 1 Servo myServo; 2 void setup() { myServo.attach(2); 3 myServo.write(90); 4 } void loop() { delay(100); }
1 Import the library. 2 Create an instance of Servo and name it myServo. 3 Attach myServo to pin 2. 4 Tell the servo to rotate 90 degrees.
Insect Robot
57
Servo Motors
Because we want the motor to move to one position and stay there, we can include all our code in the setup() function. The loop() function must be declared for Arduino to compile, but because we don’t need to do anything in the loop, it can remain empty. When we write() to a servo, we set it to a specific position. Limited rotation servo motors can turn from 0 to 180 degrees, so setting ours to 90 turns it exactly half of its maximum rotation. The servo is now perfectly centered, and it will remain that way until given further instruction.
Moving the Servo Let’s write a small program that will rotate the servo first to the center, then to the maximum angle, back to center, and then to the minimum angle. // moveServo.pde - Move servo to center, maximum angle // and to minumum angle // (c) Kimmo Karvinen & Tero Karvinen http://BotBook.com // updated - Joe Saavedra, 2010 #include Servo myServo; int delayTime = 1000; 1 void setup() { myServo.attach(2); } void loop() { myServo.write(90); 2 delay(delayTime); 3 myServo.write(180); 2 delay(delayTime); myServo.write(90); delay(delayTime); myServo.write(0); delay(delayTime); }
These are the only differences between this and the previous servo code: 1 The variable delayTime variable (set to 1,000 milliseconds, or 1 second) determines how much time to wait between each rotation. 2 Set the servo to a new angle of rotation, and then wait for a specified duration of time. 3 The duration, named delayTime, must also take into account how quickly the motor can turn. In this case, we must wait a minimum of 375 milli­seconds before sending the motor a new command. It takes this much time for the motor to rotate 90 degrees. Play with the value of this variable. You will notice that any value less than 375ms is not enough time for the motor to reach its destination, so it will begin to ­malfunction. 58
Chapter 4
Constructing the Frame
4 Similarly, you can rotate the servo to other positions simply by changing the values written to myServo. Any value between 0 and 180 will function properly, because this is our range of rotation. In this example, these values are hardcoded, meaning they are written explicitly on each line. In the future, we’ll store these values in variables for more complicated and efficient applications. Now you can rotate servos. Which objects would you like to move in your own embedded systems?
Constructing the Frame Now it’s time to build the frame for the robot.
Making the Legs Cut two pieces from a wire clothes hanger: 28cm for the rear legs and 25cm for the front legs, as shown in Figure 4-6. Bend the legs with pliers, as shown in Figure 4-7. It’s important to make the legs long enough and to make sure that the feet point backward, which lets them act as hooks and enables the robot to climb over obstacles. At this stage, don’t worry too much about the shape of the legs. You can adjust them later (and will likely need to).
Figure 4-7. Front legs (top) and rear legs (bottom)
Figure 4-6. Pieces of a wire hanger
The legs will have a better grip if you cover them with heat-shrink tubing, as shown in Figure 4-8. Heat-shrink tubing is rubber tubing that will shrink in diameter by 50% when heated, for example, with a heat gun or hair dryer. Cut two 7cm pieces of the tubing and shrink them to fit around the back legs, as shown in Figure 4-9.
Insect Robot
59
Constructing the Frame
Figure 4-8. Heat-shrink tubing
Figure 4-9. Attaching the heat-shrink tubing
Next, attach the legs to the servos. The servos come with one or more plastic attachments that will connect to the servo axis. Attach the legs by pulling metal wires through the servo holes, and secure each leg by tightening the metal wire, as shown in Figure 4-10. Cut any excess wire to keep it from 60
Chapter 4
Constructing the Frame
hindering the motor’s movement. Finally, add hot glue to the underside to stabilize the legs, as shown in Figure 4-11, but do not fill the center screw hole with glue.
Figure 4-10. Attaching with metal wire
Figure 4-11. Securing with hot glue
Assembling the Frame The frame of the walker consists of two connected servo motors. Before gluing them together, you need to remove a small plastic extension (meant for mounting the servos) from both of the servo motors. Remove the extension next to the servo arm from the front-facing servo, and remove the opposite
Insect Robot
61
Constructing the Frame
part from the rear-facing servo. You can do this easily with a utility knife, as shown in Figure 4-12. It’s a good idea to smooth out the cutting seam with a small file to make sure that the glued joints do not become uneven and weak.
Figure 4-12. Removing the obstructing plastic
Spread the hot glue evenly on the rear servo, as shown in Figure 4-13, and immediately press the servos together (Figure 4-14), holding them steady for a while to give the glue time to set. The servos are connected so that the frontfacing servo arm points forward and the rear-facing arm points down. The top sides of the motors should be placed evenly, to make it easier to attach them to the Arduino. If you make a mistake gluing the parts together, it’s easy to separate them without too much force. (Hot-gluing is not necessarily ideal for building sturdy devices, but it is a quick and easy way to attach almost anything, and it works well with simple prototypes.)
Figure 4-13. Spreading the hot glue
Figure 4-14. Gluing the servos together
62
Chapter 4
Constructing the Frame
Making the Holder for the Arduino We will use two metal strips to build a holder on top of the robot that will make it easy to attach and detach the Arduino. Cut two 10cm metal pieces. Bend the sides of each strip so that the space in the middle is equal to the width of the Arduino (Figure 4-15) and glue both strips to the servos, as shown in Figure 4-16.
Figure 4-15. Bent attachments
Figure 4-16. Gluing attachments
The Arduino Duemilanove (and later models, such as the Uno) used in this project is 5.2cm wide. If the metal you’re using is flexible enough, it is helpful to bend the corner inward slightly. This way, you can snap the Arduino in place sturdily and, when you are finished, remove it painlessly for other uses. Insect Robot
63
Constructing the Frame
Attaching a Battery We’ll use Velcro tape to make an attachment system in the rear of the robot for the 9V battery. Cut a 16cm strip from the Velcro tape and attach the ends together. Make holes for two screws in the middle of the tape. Attach the Velcro tape with screws to the servo’s extension part in the rear of the robot, as shown in Figure 4-17.
Figure 4-17. Velcro tape for the batteries
Assembly Now you can place the Arduino board on top of the servos. Attach the legs to the servos, but don’t screw them in tightly yet. We’ll connect the servos to each other and to the Arduino with jumper wires. First, connect the servos’ black (GND) wires using a black jumper wire from one servo to the other, and then use another black jumper wire to connect one of the servos to an Arduino GND pin. You might have to use a bit of force to insert two wires into one of the servo connection headers. Next, connect the red power wires in a similar manner, first from one servo to the other and then to the Arduino’s 5V power pin. Use white (the actual color may vary) jumper wires to control each servo, and connect a yellow jumper wire from the rear servo to Arduino pin 2 and from the front servo to Arduino pin 3.
64
Chapter 4
Constructing the Frame
Figure 4-18 shows the connections being made. Note that only one of the yellow wires is connected; you’ll need to connect one yellow wire to each servo. The schematic (Figure 4-19) shows this in more detail.
Figure 4-18. Connections
(front legs)
(rear legs)
Figure 4-19. Servo connection schematic
Insect Robot
65
Constructing the Frame
Earlier in the chapter, in the “Centering the Servo” section, you ran a sketch to center a single servo. If you run this code again to center the servo, you will be able to attach the leg in the correct position. But there are now two servos, so let’s alter the previous centering code to turn both servos toward the center: //twoServosCenter.pde - Center two servos // (c) Kimmo Karvinen & Tero Karvinen http://BotBook.com //updated - Joe Saavedra, 2010 #include Servo frontServo; Servo rearServo; 1 void setup() { frontServo.attach(2); rearServo.attach(3); 2 frontServo.write(90); 3 rearServo.write(90); } void loop() { delay(100); }
The only difference between this and the earlier centering code is the addition of two servo objects named frontServo and rearServo: 1 Define an instance of the Servo object for the rear servo. 2 Within setup, attach the rearServo to pin 3. 3 Send pulses to both of the motors, making them turn toward the center.
Screwing the Legs in Place Now that the servos are centered, you can screw the legs into place. It’s also a good idea to attach the battery now, because the additional weight will affect the way the robot walks. However, it is not necessary to connect the battery wires to the Arduino; you can take power straight from the USB cable while you are programming and testing the device. Figure 4-20 shows the finished robot.
Figure 4-20. The finished robot frame
66
Chapter 4
Programming the Walk
Attaching Wires More Securely Wires rarely fit perfectly tight to Arduino ports or servo extension cables. Constantly disconnecting wires makes building painful. To attach a wire more securely, bend its end into a small curve, as shown in Figure 4-21.
If you really want to make sure that the wires stay where they should, use the ScrewShield. The ScrewShield adds “wings” with terminal blocks to both sides of Arduino. Terminal blocks have screws so you can attach one or more wires firmly to any pin. SHED: MKWS1, SFE DEV-09729, http:// adafruit.com: 196, and on http://store.fungizmos.com.
Figure 4-22. The ScrewShield Figure 4-21. Bending the wire
Programming the Walk Now we can program the Arduino to walk.
Walking Forward If you power up the Arduino running the code that swings only one servo (see the earlier “Moving the Servo” section), it will start rocking on either its front or rear legs. Walking forward will require coordination between both front and rear legs. When the servos move at the same tempo, but in opposite directions, the robot starts to walk. Here is some code that will make the robot walk forward: // walkerForward.pde - Two servo walker. Forward. // (c) Kimmo Karvinen & Tero Karvinen http://BotBook.com // updated - Joe Saavedra, 2010 #include Servo frontServo; Servo rearServo; int centerPos = 90; 1 int frontRightUp = 72; 2 int frontLeftUp = 108; 3 int backRightForward = 75; 4 int backLeftForward = 105;5 void moveForward() 6 { frontServo.write(frontRightUp);
Insect Robot
67
Programming the Walk rearServo.write(backLeftForward); delay(125); frontServo.write(centerPos); rearServo.write(centerPos); delay(65); frontServo.write(frontLeftUp); rearServo.write(backRightForward); delay(125); frontServo.write(centerPos); rearServo.write(centerPos); delay(65); } void setup() { frontServo.attach(2); rearServo.attach(3); } void loop() { moveForward(); 7 delay(150); //time between each step taken, speed of walk }
Let’s have a look at the code: 1 This is the center position for the servos. Ninety degrees is precisely half of 180 possible degrees of rotation. 2 Maximum position the right front leg will rise to. 3 Maximum position the left front leg will rise to. 4 Maximum position the right rear leg will bend to. 5 Maximum position the left rear leg will bend to. 6 The moveForward function turns the servos first to opposite directions. The variables defined in the preceding lines set how far each of the servos will rotate. Before we turn in another direction, we will tell the servos to rotate toward a predefined center point for a short span of time. This ensures that the servos don’t start drifting out of sync. We return to the center point at the end of every step to make the walk more elegant and efficient. 7 Call the moveForward function repeatedly within the loop, which will make our robot move one step forward. The subsequent delay controls how long the robot waits before taking its next step. Removing the delay is the equivalent of having the robot run as fast as it can.
Walking Backward When walking forward works, walking backward is easy. This time, the servos move at the same pace and in the same direction: // walkerBackward.pde - Two servo walker. Backward. // (c) Kimmo Karvinen & Tero Karvinen http://BotBook.com // updated - Joe Saavedra, 2010
68
Chapter 4
Programming the Walk #include Servo frontServo; Servo rearServo; int centerPos = 90; int frontRightUp = 72; int frontLeftUp = 108; int backRightForward = 75; int backLeftForward = 105; void moveBackward() 1 { frontServo.write(frontRightUp); rearServo.write(backRightForward); delay(125); frontServo.write(centerPos); rearServo.write(centerPos); delay(65); frontServo.write(frontLeftUp); rearServo.write(backLeftForward); delay(125); frontServo.write(centerPos); rearServo.write(centerPos); delay(65); } void setup() { frontServo.attach(2); rearServo.attach(3); } void loop() { moveBackward(); 2 delay(150); //time between each step taken, speed of walk }
Let’s see what has changed from the previous code: 1 The moveBackward() function is similar to moveForward(), but this time the right front leg will rise up when the right rear leg moves forward, and the left front leg rises when the left rear leg moves forward. 2 Now moveBackward() is called in the loop() function.
Turning Backward Moving the robot forward and backward is not enough if we want it to avoid obstacles. The preferred outcome is for the robot to detect the obstacle, turn in another direction, and continue to walk. Naturally, it could just back up and turn after that, but the turn would be more efficient if the robot first backs up to the right and then turns to the left.
Insect Robot
69
Programming the Walk
The robot can turn to the right as it walks backward if you alter the center point of the servo and the threshold levels a bit toward the right side. This will also change the balance of the robot, which can easily lead to one of its front legs rising higher than the other. You can solve this problem by adding a bit of movement to the lowered leg, raising it into the air: // walkerTurnBackward.pde - Two servo walker. Turn backward. // (c) Kimmo Karvinen & Tero Karvinen http://BotBook.com // updated - Joe Saavedra, 2010 #include Servo frontServo; Servo rearServo; int centerPos = 90; int frontRightUp = 72; int frontLeftUp = 108; int backRightForward = 75; int backLeftForward = 105; void moveBackRight() 1 { frontServo.write(frontRightUp); rearServo.write(backRightForward-6); delay(125); frontServo.write(centerPos); rearServo.write(centerPos-6); delay(65); frontServo.write(frontLeftUp+9); rearServo.write(backLeftForward-6); delay(125); frontServo.write(centerPos); rearServo.write(centerPos); delay(65); } void setup() { frontServo.attach(2); rearServo.attach(3); } void loop() { moveBackRight(); delay(150); //time between each step taken, speed of walk }
The new moveBackRight() function is similar to the moveBack() function in the previous example. 1 The movement of the rear servo is reduced by 6 degrees, which will move its center point to the right. As we noted earlier, the changed rear servo position will likely change the balance of the entire robot. To account for this, we add 9 degrees to the frontLeftUp value. If any of your own robot’s legs stay in the air or drag, you can increase or decrease these values as needed.
70
Chapter 4
Programming the Walk
Turning Forward A turn forward resembles otherwise normal forward walking, but now the center points of both servos are changed. The movement of one front leg must also be adjusted to keep it from rising too high. // walkerTurnForward.pde - Two servo walker. Turn forward. // (c) Kimmo Karvinen & Tero Karvinen http://BotBook.com // updated - Joe Saavedra, 2010 #include Servo frontServo; Servo rearServo; int centerTurnPos = 81; 1 int frontTurnRightUp = 63; 2 int frontTurnLeftUp = 117; 3 int backTurnRightForward = 66; 4 int backTurnLeftForward = 96;5 void moveTurnLeft() 6 { frontServo.write(frontTurnRightUp); rearServo.write(backTurnLeftForward); delay(125); frontServo.write(centerTurnPos); rearServo.write(centerTurnPos); delay(65); frontServo.write(frontTurnLeftUp); rearServo.write(backTurnRightForward); delay(125); frontServo.write(centerTurnPos); rearServo.write(centerTurnPos); delay(65); } void setup() { frontServo.attach(2); rearServo.attach(3); } void loop() { moveTurnLeft(); 7 delay(150); //time between each step taken, speed of walk }
1 Create a new variable that will define the center point of the servos during a turn (the center turn position). Notice we are 9 degrees away from the motor’s halfway point. 2 Calculate the maximum position to which the right front leg will rise by deducting 18 degrees from the center turn position. 3 Calculate the position of the left front leg in the same way as the right front leg, but instead of adding 18 degrees to the center turn position, add 36 (to balance the front legs). 4 Calculate the position for the right rear leg by deducting 15 degrees from the center turn position. Insect Robot
71
Avoiding Obstacles Using Ultrasound
5 Add the same distance to the left rear leg. 6 The MoveTurnLeft() function is similar to all the other walks. This time, the variables just mentioned are used for turning, and both servos are centered to a different position than when walking forward.
If you have any problems with how your robot walks, it is best to adjust only one walking direction at a time.
7 Repeat turning forward in the loop and the delay to control the speed of our walk. Now you have made your robot walk in different directions and turn while moving forward and backward. Next, we will combine all the movements into one sketch and coordinate movement with an ultrasonic range sensor.
Avoiding Obstacles Using Ultrasound Now it’s time to add an ultrasonic sensor to our robot.
Attaching the Ultrasonic Sensor Use the servo extension cable to attach the sensor. You can remove the plastic part covering the cables from one end to expose the wires, as shown in Figure 4-23. If you’d like, you could cut the connector off and solder three single-strand wires as you did with the Stalker Guard enclosure in Chapter 3.
Figure 4-23. One end of the servo extension cable disassembled
Using hot glue, we connected the other end of the extension cable to the front of the robot with the connector part pointing down (if you’d rather have it pointing up, that will work, too). If the connector doesn’t snap in easily, you can glue the wires in place. The pin assignments are marked on top of the pins. After the glue has dried, you can snap the ultrasonic sensor into place on the connector. Connect the other end of the servo extension cable to the Arduino. The red goes to the Arduino 5V port, black to the GND port, and white to the digital pin 4. Since the Arduino does not have many free pins left, you can connect the black and red wires in the same holes as the servo cables. Figure 4-24 shows the sensor connected to the robot (the schematic is shown in Figure 4-25). 72
Chapter 4
Avoiding Obstacles Using Ultrasound
Figure 4-24. Ultrasonic sensor in place
(front legs)
(rear legs)
Figure 4-25. Ultrasonic sensor and servo connection schematic
Code Next, we’ll connect all previously tested movements into one program. In addition, we’ll add some ultrasonic code for detecting obstacles (see Chapter 3 for the Stalker Guard project, which uses similar code). // walkerForwardComplete.pde - Two servo walker. // Complete code with obstacle avoidance // (c) Kimmo Karvinen & Tero Karvinen http://BotBook.com // Updated by Joe Saavedra, 2010 #include Servo frontServo; Servo rearServo;
Insect Robot
73
Avoiding Obstacles Using Ultrasound /* Servo motors - global variables */ 1 int centerPos = 90; int frontRightUp = 72; int frontLeftUp = 108; int backRightForward = 75; int backLeftForward = 105; int walkSpeed = 150; // How long to wait between steps in milliseconds int centerTurnPos = 81; int frontTurnRightUp = 63; int frontTurnLeftUp = 117; int backTurnRightForward = 66; int backTurnLeftForward = 96; /* Ping distance measurement - global variables */ int pingPin = 4; long int duration, distanceInches; long distanceFront=0; //cm int startAvoidanceDistance=20; //cm long microsecondsToInches(long microseconds) { return microseconds / 74 / 2; } long microsecondsToCentimeters(long microseconds) { return microseconds / 29 / 2; } long distanceCm(){ 2 pinMode(pingPin, OUTPUT); digitalWrite(pingPin, LOW); delayMicroseconds(2); digitalWrite(pingPin, HIGH); delayMicroseconds(5); digitalWrite(pingPin, LOW); pinMode(pingPin, INPUT); duration = pulseIn(pingPin, HIGH); distanceInches = microsecondsToInches(duration); return microsecondsToCentimeters(duration); } void center() { frontServo.write(centerPos); rearServo.write(centerPos); } void moveForward() 3 { frontServo.write(frontRightUp); rearServo.write(backLeftForward); delay(125); frontServo.write(centerPos); rearServo.write(centerPos); delay(65);
74
Chapter 4
Avoiding Obstacles Using Ultrasound frontServo.write(frontLeftUp); rearServo.write(backRightForward); delay(125); frontServo.write(centerPos); rearServo.write(centerPos); delay(65); } void moveBackRight() { frontServo.write(frontRightUp); rearServo.write(backRightForward-6); delay(125); frontServo.write(centerPos); rearServo.write(centerPos-6); delay(65); frontServo.write(frontLeftUp+9); rearServo.write(backLeftForward-6); delay(125); frontServo.write(centerPos); rearServo.write(centerPos); delay(65); } void moveTurnLeft() { frontServo.write(frontTurnRightUp); rearServo.write(backTurnLeftForward); delay(125); frontServo.write(centerTurnPos); rearServo.write(centerTurnPos); delay(65); frontServo.write(frontTurnLeftUp); rearServo.write(backTurnRightForward); delay(125); frontServo.write(centerTurnPos); rearServo.write(centerTurnPos); delay(65); } void setup() 4 { frontServo.attach(2); rearServo.attach(3); pinMode(pingPin, OUTPUT); } void loop() 5 { distanceFront=distanceCm(); 6 if (distanceFront > 1){ // Filters out any stray 0.00 error readings 7 if (distanceFront 0) { 7 char inByte = myPort.readChar(); print(inByte); // Displays the character that was read } }
122
Chapter 5
Connecting Arduino with Processing
Let’s look at the key lines of code in this sketch: 1 Processing comes with a number of libraries that extend its capabilities. This line loads the serial library, which lets Processing talk to serial ports. 2 Declare an object named myPort of the type Serial. 3 The setup() method functions just like the setup() method in Arduino: it’s called once per sketch. 4 Create a window of the specified width and height (in pixels). 5 You’ll see something similar to these bold lines in many Processing sketches that talk to the serial port. The first line prints out all the serial ports that Processing can find (see Figure 5-25). On most computers, the Arduino will be connected to the first serial port; this is because most modern computers don’t have any built-in serial ports, so the lowestnumbered port is almost always the one Arduino is using. If this is not the case, examine the output in the Processing window and change the 0 on the last bold line to the index of the correct serial port.
Figure 5-25. Processing listing its serial ports
6 The draw() function is similar to Arduino’s loop() in that it’s called continuously. Because Processing draws graphics and animations, this function is called a certain number of times per second (60) unless you change the frame rate with the frameRate() function. 7 This block checks to make sure there is some text to read from the serial port. If so, it reads it and prints it out (see Figure 5-26). Interactive Painting
123
Processing Code for the Painting
Figure 5-26. Processing displaying the characters it reads from the Arduino over the serial port
Processing Code for the Painting The following Processing program, like our earlier Python program, displays images based on the instructions arriving via the serial port. The images change when a user slides them by waving his hand. You must put some images (JPEG, GIF, or PNG) into the sketch’s data directory. To find this directory, choose Sketch→Show Sketch Folder. If it does not exist, create a directory, call it data, and put your images in it.
When F is read from the serial port, the images are moved to the right for the width of the painting (screen width). B will likewise move images to the left, for the same number of pixels. When the user is about to move past the final image, the program moves to the opposite end of the image queue. This will make it appear to the user as if the images are continuing endlessly in both directions. // http://BotBook.com import processing.serial.*; int slideStep = 75;
// how many pixels to slide in/out 1
// The current image and the next image to display PImage currentImage, nextImage; 2 // The index of the current image. int imgIndex = 0; 3 // Keeps track of the horizontal slide position. A negative number // indicates sliding in from the left.
124
Chapter 5
Processing Code for the Painting int slideOffset; 4 // All the image files found in this sketch’s data/ directory. String[] fileList; 5 // A serial port that we use to talk to Arduino. Serial myPort; // This class is used to filter the list of files in the data directory // so that the list includes only images. class FilterImages implements java.io.FilenameFilter { 6 public boolean accept(File dir, String fname) { String[] extensions = {'.png', '.jpeg', '.gif', '.tga', '.jpg'}; // Don’t accept a file unless it has one of the specified extensions for (int i = 0; i < extensions.length; i++) { if (fname.toLowerCase().endsWith( extensions[i])) { return true; } } return false; } } // This loads the filenames into the fileList void loadFileNames() { 7 java.io.File dir = new java.io.File(dataPath(')); fileList = dir.list(new FilterImages()); }
// The Processing setup method that’s run once void setup() { size(screen.width, screen.height); // Go fullscreen loadFileNames();
// Load the filenames
/* This centers images on the screen. To work correctly with this mode, we'll be using image coordinates from the center of the screen (1/2 of the screen height and width) . */ imageMode(CENTER); 8 // Load the current image and resize it. currentImage = loadImage(dataPath(') + fileList[0]); 9 currentImage.resize(0, height); println(Serial.list()); bk myPort = new Serial(this, Serial.list()[0], 9600); bl } // Go to the next image void advanceSlide() { bm imgIndex++; // go to the next image if (imgIndex >= fileList.length) { // make sure we're within bounds imgIndex = 0; }
Interactive Painting
125
Processing Code for the Painting slideOffset = width; // Start sliding in from the right } void reverseSlide() { imgIndex--; // go to the previous image if (imgIndex < 0) { // make sure we're within bounds imgIndex = fileList.length - 1; } slideOffset = width * - 1; // Start sliding in from the left } void draw() { // Put up a black background and display the current image. background(0); image(currentImage, width/2, height/2); bn // Is the image supposed to be sliding? if (slideOffset != 0) { bo // Load the next image at the specified offset. image(nextImage, slideOffset + width/2, height/2); if (slideOffset > 0) { // Slide from the right (next) bp slideOffset -= slideStep; if (slideOffset < 0) { slideOffset = 0; } } if (slideOffset < 0) { // Slide from the left (previous) slideOffset += slideStep; if (slideOffset > 0) { slideOffset = 0; } } if (slideOffset 0) { bq currentImage = nextImage; } } else { // If we're not sliding, read the serial port. if (myPort.available() > 0) { char inByte = myPort.readChar(); print(inByte); // Displays the character that was read if (inByte 'F') { // Forward advanceSlide(); br } if (inByte 'B') { // Backward reverseSlide(); } // Load and resize the next image nextImage = loadImage(dataPath(') + fileList[imgIndex]); bs nextImage.resize(0, height); } } }
126
Chapter 5
Processing Code for the Painting
Let’s examine the code one piece at a time: 1 When the user slides to the next or previous image, the program gradually draws the new image over the current one. This variable determines how many pixels at a time the new image will slide in. A value of 1 appears very smooth, but it’s slow. You can try changing this value to get something you like. 2 Processing uses a data type called PImage to represent images. This program uses two of these objects: one to represent the current image, and the other to represent the one that’s sliding into view. 3 This is an index of the list of images, so we know which image we’re currently on. 4 Because the draw() function is called many times a second, we can’t waste any time inside it. So we’ll be sliding new images into view over a series of calls to draw(). This variable keeps track of where we are in that process. A positive value here indicates images are sliding from the right; a negative value indicates they slide from the left. 5 This contains the list of all files found in the sketch’s data directory. 6 This is a function that’s used by the loadFileNames() function to eliminate any files in the data directory that aren’t images. 7 Load all the files in the data directory into the fileList array. 8 Tell Processing that it needs to center any images that it draws to the screen. As a result, we won’t be using 0,0 as the starting point for drawing images. Instead, we’ll use dead center: half of both the height and width. 9 List all the available serial ports. bk On some computers, Arduino will usually be connected to the first serial port. If not, change the 0 to the correct one (examine the output of the previous line of code to figure out which one to use). bl Load the first image, which is at index 0 in the array of files. Because we have only the filenames in this array, we use the dataPath() function to insert the path of the sketch’s data folder, much as we did with Python and the os.path.join() function. bm Get the sketch ready to move to the next slide by incrementing the image index. Additionally, this section avoids running past the end of the list of images by starting at 0 each time a user tries to go beyond the last image. Finally, it sets the slideOffset to begin sliding in from the rightmost column of pixels onscreen. The next function does the same, but in reverse (decrements the image index, wraps around to the end if the index goes below 0, and prepares slideOffset to slide in from the left). bn Display the current image at the center of the screen. bo If the slideOffset is something other than 0, it means the program is in the middle of sliding the image. The code in this block moves to the next step of the animation. Interactive Painting
127
Creating an Enclosure
bp This code slides the image in from the right, one step at a time. First it shifts the slideOffset, and then it makes sure it hasn’t reached or gone past 0 (if it has gone beyond, this code resets it to 0). The next block does the same for sliding from the left. bq If the slideOffset just reached 0, it’s time to swap the images; the current image is replaced by the one we’ve been sliding in. The sketch will now show the current image until it gets another F or B command from the Arduino. br If the Processing sketch reads an F from the Arduino, it’s time to move forward. Similarly, if it receives a B, it’s time to move back. bs Load the next image (the one that’s sliding into view) into the nextImage object, so the program can gradually draw it over the current image.
The Finished Painting Wave your hand in front of the painting, and the frog will change into a landscape. What kind of exhibition could use this type of user interface? In which other Processing programs could you use Arduino’s sensors? You now have an image presentation solution that you can control by waving your hand in front of a computer. If you want to make this into a more permanent device, follow the instructions in the next section.
Creating an Enclosure Use a metal saw to cut a 25cm piece of PVC tube (Figure 5-27) with a 7.5cm diameter and then smooth the edges with sandpaper (we used P240-grade paper). With a marker, draw a horizontal center line in the pipe; we’ll use this as the center point of the ultrasonic sensors. Mark a center point on the tube at 12.5cm. Put the first ultrasonic sensor at that position and trace it on the tube with a marker. Trace the next two sensors the same way, so that their center points are 6.5cm from the center point of the tube (Figure 5-28).
Figure 5-27. A 25cm piece of PVC tube
128
Chapter 5
Creating an Enclosure
Figure 5-28. The marked ultrasonic sensor positions
Drill holes with a 16mm bit and smooth their edges with sandpaper (Figure 5-29). Test whether the sensors fit in the holes (Figure 5-30). You might have to trim the holes a bit with the sandpaper, a file, or a mini drill. Clean and dry the surface of the tube. Paint the tube with matte black spray paint (Figure 5-31). (In practice, any regular spray paint works here.) Paint several thin layers to get an even end result.
Figure 5-29. The 16mm holes for the sensors
Figure 5-30. Test that the sensors fit in the holes
Interactive Painting
129
Creating an Enclosure
Figure 5-31. Painted tube
Attaching the Sensors with Servo Extension Cables We’ll attach the ultrasonic sensors with servo extension cables. Remove the end of the extension cable that fits into the servo connector (Figure 5-32). You can crack and break it with the pliers. This way, you do not have to solder singlestrand wires to replace the multistrand ones.
Do not cut the extension cable, because the wires under the plastic have metal ends that fit straight into the prototyping breadboard.
Figure 5-32. A dismantled extension cable
130
Chapter 5
Creating an Enclosure
Connect a black wire from the Arduino GND pin to the top horizontal line of the prototyping breadboard, and from the +5V pin to the bottom horizontal line. Connect the black and red wires of the extension cable to these rows. Place the wires in different sides of the Arduino to prevent them from accidentally creating a short. Connect the signal wires of the ultrasonic sensors to the same pins used in “Detecting Motion Using Ultrasonic Sensors” (Figures 5-33 and 5-34).
Figure 5-33. Ultrasonic sensors attached to the prototyping breadboard
(left)
(center)
(right)
Figure 5-34. Connection diagram
Attach the ultrasonic sensors to their holes and push the prototyping breadboard inside the tube.
Interactive Painting
131
Building a Frame
After the sensors and the electronics are attached, the tube probably doesn’t point where you want it to point. You can fix this by gluing something heavy inside the tube to balance it. For example, a strip of bitumen carpet, available from automotive supply stores, can serve this purpose perfectly (Figure 5-35).
Figure 5-35. Strip of bitumen carpet for balancing the tube
Figures 5-36 and 5-37 show the parts placed inside the tube and the completed enclosure, respectively.
Figure 5-36. Parts in place inside the tube
Figure 5-37. Completed enclosure
Building a Frame You can use an old, retired laptop computer to build a wall-mounted picture frame. First, turn the laptop around so that the display points in one direction and the keyboard in the opposite direction. In the case of our laptop, this required a small cutting operation for the frame of the computer so that the display cables would reach as far as needed.
132
Chapter 5
Building a Frame
Our next challenge was to build a sturdy frame that would handle the weight of the laptop and that you would dare to mount on the wall. You’ll find similar projects on the Internet, but many of them have weak supporting frames. Figures 5-38 through 5-41 show a mechanical drawing of the frame and an image sequence of the construction process.
Figure 5-38. Mechanical design
Figure 5-39. Steps 1–4
Interactive Painting
133
Building a Frame
1. The old flat iron we chose as the frame material. 2. Sanding down rust and unevenness with an edge sander. 3. The sanded surface. 4. Hammering an arch to the frame with a sledgehammer to fit the portable computer.
Figure 5-40. Steps 5–8
5. Bending the metal in the other direction. 6. The finished bends. 7. Cutting away the excess metal (extra pieces were used for making two metallic beams attached to the frame). 8. Making holes for the upper parts for tightening screws. The laptop will fit tightly in the frame with them. We made a hole in the middle of the upper horizontal beam for wall mounting.
134
Chapter 5
Building a Frame
Figure 5-41. Steps 9–12
9. The lower cross plate, ready to be welded. 10. Welding the upper cross plate. 11. The finished frame. 12. The laptop inside the frame. By itself, the frame is quite plain, so as a final step you may want to paint or decorate it to work with your interior design.
Interactive Painting
135
Boxing Clock
A round starts. You throw a left jab, followed by a right hook. The crowd is cheering. After a few minutes, the round ends. Saved by the bell! The slowly filling timer shows you how much time you have before the next round. During the break, you remember how you learned to program cell phones while coding the Boxing Clock (Figure 6-10) in this project.
6 In this chapter What You’ll Learn Tools and Parts Android Software Installation Creating a Boxing Clock in Android What’s Next?
In this project, you will become familiar with cell phone programming, including how to install and run programs on a cell phone. The program you’ll create includes the basic components necessary for many other programs. You will learn how to create a graphical user interface, how to draw on the screen, and even how to animate. You will also play MP3 sounds on a cell phone. We will explain all the code, line by line, but our purpose is not to turn you into a cell phone programming professional. Rather, the goal is to familiarize you with yet another embedded system that can be used in prototyping. As with most programming platforms, the most difficult phase is the installation of the development environment and the first “Hello World” code. After that, it’s easy to gather more information and move on to more complex programs. In a later project, (the Soccer Robot in Chapter 8), you’ll use a cell phone to communicate with Arduino. In this chapter, you’ll work with a cell phone only to create a round and break timer suitable for boxing matches or even chess boxing (http://en.wikipedia.org/wiki/Chess_boxing).
What You’ll Learn In this chapter, you’ll learn: • The basics of cell phone programming • How to install your own programs on a cell phone
Figure 6-1. The Boxing Clock on an Android phone
This chapter covers the Android version of the Boxing Clock. If you’d like the code for Nokia Series 60 and Symbian phones running PyS60 (Python for Series 60/Symbian phones), you can obtain it from http://examples.oreilly. com/0636920010371 or http:// BotBook.com/. We’re also planning to offer a short ebook for sale that includes a detailed walkthrough of all the Series 60/Symbian examples. For more information on this, check out the book’s website or get in touch with us (see “We’d Like to Hear From You” in the Preface).
• How to install necessary programming tools 137
Android Software Installation
Tools and Parts Here are the tools and parts you’ll need for this project, shown in Figure 6-2: • One of the following cell phones: —— Nokia Series 60–based cell phone, such as the Nokia N95. —— Android cell phone running Android version 2.2 or newer. We used the Google Nexus One and Sprint EVO 4G, both manufactured by HTC. Figure 6-2. Nokia N95 (left) and Google Nexus One (right)
• Data cable to connect the cell phone to your computer’s USB port.
Android Software Installation Android is a cell phone operating system developed by Google. It is based on Linux, but most applications are written in Java using the Android SDK provided by Google. Many manufacturers make Android cell phones, including HTC, Samsung, and Motorola. Programs created with Android are easy to distribute to consumers. For example, you can distribute your own Android application via a web page, or you can put it in the Android Market. Before learning how to program, you’ll need to install several software packages. In addition to Android development tools, you’ll need Java and Eclipse, an integrated development environment (IDE). To write applications in Eclipse, you will also need to install an Android extension for it. Eclipse offers a text editor that adds color syntax highlighting to source code; project management capabilities; integrated documentation; and all kinds of other features. You can also extend Eclipse to support many other programming languages in addition to Java. You’ll also need to install Apache Ant, an optional component that will let you compile Java applications, including Android applications, from the command line. After installation, you’ll give the new development environment a try by executing a “Hello World” program, created quickly using an application template supplied with the Android SDK. You’ll test the program directly within your computer by using an Android emulator.
Debugging a program like this can be tricky. See the appendix for information on using Android’s logging facility to monitor the program’s condition.
When the final Boxing Clock program is ready, you’ll transfer the code into a real cell phone. This section provides installation instructions for Ubuntu 10.04, Windows 7, and Mac OS X. If you use another supported operating system, you should be able to adapt the instructions to your situation.
Ubuntu Linux Installation First, you’ll need to install Ant, Java, and Eclipse. Although you can get these files from other sources, it is helpful to use the operating system’s built-in package management. 138
Chapter 6
Android Software Installation
These instructions are specific to Ubuntu Linux, but might work with other Debian-based Linux distributions. You should be able to find these packages for other Linux distributions. If you didn’t do so in earlier chapters, switch on the Universe repository, which includes some extra open source programs. Run this command at the Terminal shell prompt: $ sudo software-properties-gtk --enable-component=universe
Run these commands at the Terminal shell prompt to refresh the available packages and install Ant, Java, and the Eclipse IDE: $ sudo apt-get update $ sudo apt-get install --yes ant openjdk-6-jdk $ sudo apt-get install --yes eclipse
If you have a 64-bit operating system, you will still need the compatibility library, ia32-libs. Trying to install this on 32-bit systems won’t harm anything, but the package will install only into a 64-bit operating system: $ sudo apt-get install --yes ia32-libs
Now it’s time to install the Android development tools. Download the installation program from http://developer.android.com/sdk/index.html and save the package file in the directory (such as your home directory) where you wish to extract it. At the Terminal shell prompt, uncompress the package (in geek language, this is known as a tarball because it is archived with the tar utility): $ tar -xf android-sdk_r09-linux_x86.tgz
Replace 09 with the actual version of the SDK (note that the filename may vary by more than the version number in future releases). This should create a directory called android-sdk-linux_x86, although the directory name could change in future releases of the SDK. You can confirm that the package unpacked correctly by running the command ls androidsdk-linux_x86 and making sure you see several files and directories in there: $ ls -l android-sdk-linux_x86/ total 16 drwxr-xr-x 2 user user 4096 2010-08-30 drwxr-xr-x 2 user user 4096 2010-08-30 -rw-r--r-- 1 user user 828 2010-08-30 drwxr-xr-x 4 user user 4096 2010-08-28
15:24 15:24 15:24 20:43
add-ons platforms SDK Readme.txt tools
Windows 7 Installation You’ll need to download and install several packages on Windows.
Boxing Clock
139
Android Software Installation
Java 6 for Windows Go to http://www.oracle.com/technetwork/java/javase/downloads/index.html and download the latest version of the Java Platform, Standard Edition (at the time of this writing, that’s Java Platform, Standard Edition, JDK 6 Update 23). If you are given a choice between JDK and JRE, choose JDK. Run the installer. If you are running 64-bit Windows and choose the 64-bit version of the JDK, be sure to choose the 64-bit version of Eclipse in the next step.
Eclipse Be sure you are logged in as a user with administrative privileges. If you’re not, you’ll need to have the username and password of an admin user handy, because the installers might prompt you for both when you try to install the software. Once you are finished with the administrative tasks, log in again as a normal, nonadministrative user.
Visit http://www.eclipse.org/downloads/ and download the latest Eclipse IDE for Java Developers for Windows. Extract the zip file to any location on your hard drive, such as C:Eclipse. You will need to add a shortcut to the Eclipse application manually; you can put it on your desktop, Start menu, or both.
Android SDK Download the latest Windows version of the Android SDK from http:// developer.android.com/sdk/index.html. Open the zip file and copy the top-level folder to any location on your hard drive (such as C:android-sdk-windows).
Configure your Path You should configure your Windows Path environment variable (see Figure 6-3) so that you can run Android tools from the command prompt: 1. Open the Start menu, right-click Computer, and choose Properties. 2. Click the link for Advanced System Settings. 3. Click the Environment Variables button on the Advanced tab. 4. Choose Path from the System Variables list and click Edit. 5. Append the following to the end of the current value (do not erase any of the existing values): ;C:android-sdk-windows. (If you put the SDK somewhere else, use that location here instead.) 6. Close any open command prompt windows and reopen them. Figure 6-3. Adding the folder into the Windows Path variable
Mac OS X Installation You’ll need to download and install two packages on Mac OS X.
Eclipse Visit http://www.eclipse.org/downloads/ and download the latest Eclipse IDE for Java Developers for Mac OS X. Extract the tar.gz file by double-clicking it, and copy the Eclipse folder to any location on your hard drive, such as /Applications/Eclipse.
140
Chapter 6
Android Software Installation
Android SDK Download the latest Mac OS X version of the Android SDK from http:// developer.android.com/sdk/index.html. Extract the zip file by double-clicking it, and copy the android-sdk-mac_* directory to any location on your hard drive (such as /Users/yourname/android-sdk-mac_x86).
Configuring the Android SDK Run the Android SDK and AVD manager. On Linux and Mac OS X, you can run it by typing the path to the file. If you’ve installed the Android SDK in your home directory, type this command at a Terminal shell prompt (the $ is the shell prompt itself; type everything after it):
If you are using a non-US keyboard, the ~ key may be difficult to access. You can also type $HOME/androidsdk-*/tools/android.
$ ~/android-sdk-*/tools/android
On Windows, type android at a command prompt or from the Run dialog (Windows-R). Press Enter to run it. Next, the Android SDK and AVD Manager appears. You should install a version of the Android SDK that targets the broadest number of phones. Android maintains a list of current distributions at http://developer.android.com/resources/ dashboard/platform-versions.html, which you can use to guide your decision. At the time of this writing, 86% of the devices were running Android 2.1 or later, so we use Android 2.1 in this section. Let’s install Android SDK 2.1 API level 7 and its instructions and example programs. Click the tab labeled “Available packages,” and check the box next to Android Repository. After a moment, a list of available tools will open, as shown in Figure 6-4. Select the Android SDK Platform tools as well as the SDK for the version you want to use, such as SDK Platform Android 2.1, API 7. You might also want to select the samples for that version of the SDK.
Figure 6-4. Downloading SDK components
Boxing Clock
141
Android Software Installation
If you are running Windows, you must also select the USB Driver Package (it is not needed on Linux or Mac OS X). This driver provides support for several phones: • • • •
T-Mobile G1 (also released as the Google ADP1) T-Mobile myTouch 3G (also released as the Google Ion) Verizon Droid Nexus One
If you have a similar device on another carrier, the driver might work with them. If not, you will need to obtain a driver from the cell phone carrier or manufacturer. See http://developer.android.com/sdk/win-usb.html for more information.
Click Install Selected, and follow the instructions on the screen that appears. You will need to accept all of the license agreements before you can install the packages. When you’re done with the installation, you can quit the Android SDK and AVD Manager.
Installing the Android Plug-in for Eclipse Launch Eclipse. The first time you run it, Eclipse will ask you where to store all your projects. Choose a folder you prefer. If you don’t want Eclipse to ask you every time you run it, select “Use this as the default and do not ask again.” The Eclipse welcome screen appears first (Figure 6-5). You can dismiss it and reveal the rest of the Eclipse IDE by clicking the close (X) button on the right of the welcome screen’s tab at the top left.
Figure 6-5. Closing the Eclipse welcome screen
142
Chapter 6
Android Software Installation
Now you can see the Eclipse main screen (Figure 6-6). This is the IDE you will use to develop Android applications. Now you need to install the Android plug-in: 1. Choose Help→Install New Software, as shown in Figure 6-6. 2. Type https://dl-ssl.google.com/android/eclipse/ in the “Work with” field and press Enter. 3. You will see a Developer Tools option in the list. Check the box next to it and click the triangle next to the checkbox to expand the view. Confirm that Android DDMS, Android Developer Tools, and Android Hierarchy Viewer are selected, as shown in Figure 6-7, and press Next.
Figure 6-6. Installing new software via the Help menu
Figure 6-7. Installing Eclipse Developer Tools
4. When the install wizard shows a list of the components to be installed, click Next. 5. You must accept the terms of the licenses to proceed, as shown in Figure 6-8. Although they look long and tiresome, these are open source licenses that are actually pretty exciting as far as software licenses go, in that they permit the software to be modified and redistributed under a given set of conditions. For example, as of this writing, the two Android packages used a combination of the Apache 2.0, BSD, and Eclipse plug-in licenses.
Boxing Clock
143
Android Software Installation
Figure 6-8. Long list of licenses
6. Wait a few minutes for the packages to download. Partway through the download, the program will ask for permission to install unsigned packages, as shown in Figure 6-9. You will need to permit this (click OK) to proceed.
Figure 6-9. A warning related to unsigned packages
7. At the end of the installation, Eclipse will offer to restart itself. You should restart your computer to make sure all of the new components are activated properly.
Configuring the Android Plug-in for Eclipse After Eclipse restarts, you need to configure the Android plug-in: 1. On the Mac, click Eclipse→Preferences. On Windows or Linux, choose Window→Preferences. 2. Click Android from the list on the left. The first time you do this, you’ll see a dialog box with a welcome message that gives you the option to send usage statistics to Google. Make your selection and click Proceed. 3. Click the Browse button to the right of the SDK Location field (see Figure 6-10) and navigate to the directory where you installed the Android SDK earlier. Select that directory and click OK, as shown in Figure 6-11. 4. Click OK to close the Preferences dialog.
144
Chapter 6
Creating a Boxing Clock in Android
Figure 6-10. Pointing Eclipse to the Android SDK directory
Figure 6-11. Locating the Android SDK in the home directory where you downloaded it
Creating a Boxing Clock in Android Before we start writing the Boxing Clock in Android, let’s make sure that the most bare-bones “Hello World” project compiles and runs.
Beginning with “Hello World” Select File→New→Project, and open the Android section from the dialog that appears (Figure 6-12). Choose Android Project and click Next. The New Android Project appears. Fill in the information as follows, shown in Figure 6-13. 1. Enter ChessBoxing in the “Project name” field. This will also determine the directory name that’s created under your Eclipse workspace folder.
Figure 6-12. Starting a new Android project with a wizard
2. Under Build Target, choose the version of Android, such as 2.1 (API level 7), that you want to set as the minimum Android version this app will run under. 3. Under Properties, enter Chess Boxing in the “Application name” field. This is the name that’s displayed when the program is running. 4. Next, specify the package name in the traditional Java style by writing your own domain name backward, with the name of the program in the end. For example, our domain is sulautetut.fi, so we will start with fi.sulautetut. We used android.chessboxing for the program name, so the package name we used was fi.sulautetut.android.chess boxing. We use this name throughout the chapter, so it will be easier if you use our name. If you replace this with your own name, be sure to use that name wherever you see fi.sulautetut.android.chessboxing. Figure 6-13. New project info
Boxing Clock
145
Creating a Boxing Clock in Android
5. Check the Create Activity box to define a class to describe the first (and so far the only) mode of the program. Name this activity ChessBoxing. Since activities are classes, we follow Java’s CamelCase naming convention by using an initial capital for each word in the name (no spaces or underscores). 6. Under Min SDK Version, pick the same level (7) that you picked for the build target.
Figure 6-14. Configuring the Build Path for the Android version you’re using to compile the project
7. Right-click the project (as shown in Figure 6-14), and choose Build →Configure Build Path. Go to the tab labeled “Order and Export” and select the Android library (for example, “Android 2.1-update1,” as shown in Figure 6-15). Click OK.
Figure 6-15. Choosing the Android version you prefer
8. Run the program by clicking the green Play button in the toolbar or by choosing Run→Run (Ctrl-F11), as shown in Figure 6-16. 9. Select Android Application from the dialog that appears and click OK. If you’ve never defined an emulator image to run your programs, and if you don’t have a phone connected in debugging mode, Eclipse will warn you that it couldn’t find a device or emulator to run under and ask, “Do you wish to add new Android Virtual Device?” Click Yes to begin creating a new emulator image.
Figure 6-16. Running the project
The Android SDK and AVD Manager appears (this is the same program we used earlier for loading the SDK): 1. Click New. 2. Select your installed Android version as the target (e.g., 2.1, API 7). Give it a descriptive name, such as “seven.”
146
Chapter 6
Creating a Boxing Clock in Android
3. Leave everything else at its default and click Create AVD, as shown in Figure 6-17. 4. Close the Android SDK and AVD Manager. Now Eclipse will ask you which virtual device you would prefer to use. Click the “Launch a new Android Virtual Device” option, click Refresh (as shown in Figure 6-18), and choose the virtual device “seven” you just created.
Figure 6-17. Creating a new emulator
Figure 6-18. Choosing the emulator to be used (remember to refresh the view)
The Android emulator will take some time to start, but while you are waiting you can admire the Android logo (Figure 6-19). In our system, starting the emulator took several minutes, so it might be best not to close it until you’re done working on the program. Once the emulator is up and running, you can deploy new versions of your program into it by clicking Run. Finally, the “Hello World” program starts. The title bar shows the name of the program (Chess Boxing), and the screen displays the text “Hello World, ChessBoxing!”, as shown in Figure 6-20.
Figure 6-19. The emulator starting
That one simple Hello World program took a lot of tweaking. Using this program as a starting point, we’ll modify it for the next couple of examples in this chapter so you won’t have to go through all the steps until later in the chapter (and even then, you won’t have to go through all of them).
Creating a User Interface First, we’ll use the ChessBoxing project we just created as the basis for a simple user interface that displays text to the user. Figure 6-20. ”Hello World” in the emulator, indicating a successful installation
Boxing Clock
147
Creating a Boxing Clock in Android
Within Android, you can’t print straight to the console with commands familiar to Java programmers such as System.out.println. Instead, we’ll create a text box directly in the graphical user interface and print to it: // bca01helloGui - Create a simple graphical user interface // (c) Kimmo Karvinen & Tero Karvinen http://BotBook.com package fi.sulautetut.android.chessboxing; 1 import android.app.Activity; 2 import android.os.Bundle; import android.widget.TextView; public class ChessBoxing extends Activity { 3 @Override public void onCreate(Bundle savedInstanceState) { 4 super.onCreate(savedInstanceState); TextView tv = new TextView(this); 5 tv.append('Welcome to BotBook.com Chess Boxing!'); 6 setContentView(tv); 7 } }
Before we run this code, let’s go over it: 1 Declare which package the program belongs to. A package will allow you to group together different Java files if needed. If you entered a different reverse domain name and app name when you created the project in the “Beginning with Hello World” section, replace fi.sulautetut.android .chessboxing with the name you used. 2 Import the classes needed to support the Android APIs you need. It can be hard to remember which packages a certain API comes from, so you can just write your code and press Ctrl-Shift-O on Linux or Windows (or Command-Shift-O on the Mac) to update the list of imports. 3 Define a class. Use the same class name you chose when you set up the project, because this project will look for an Activity named ChessBoxing. Just as with the code that was generated when you created a new project, your ChessBoxing class extends the class Activity. This class is your custom version that controls what happens when a new ChessBoxing activity is created. 4 Override the onCreate() method that is defined in Android’s Activity class. If you examine the code that was generated at the creation of the project, you’ll see the @override decorator; leave that in here. At the start of the method, call the parent class’s onCreate() method with super.onCreate(). ChessBoxing is the main activity of our program. It will start automatically when the program starts. Main activity methods (functions) resemble the Arduino functions setup() and loop(), even though Android and Arduino are otherwise quite different systems. ChessBoxing.onCreate() will run once at the beginning of the program, just like Arduino’s setup(). Later, you’ll create a timer to run your own function evenly five times per second, just like the Arduino’s loop(). 148
Chapter 6
Creating a Boxing Clock in Android
5 Make a user interface within the onCreate() method. This differs from the program generated earlier, in which the user interface was defined using an XML file named main.xml. That file will continue to exist in the project, but you will no longer be using it. Make a new text view (TextView) and assign it the name tv. 6 We will add text to the TextView using its append() method. 7 Finally, use the setContentView() method to display the view on the screen. A view in Android is a rectangular area of the cell phone screen. In our case, TextView tv can manage the entire screen of our program. A view is an object you can control using different methods.
You might be wondering how to quit the program. There is no way to do this within Android. If the user presses the home button and runs a different program, this program will remain in the background. However, if there is not enough memory available to run multiple programs, the operating system might terminate those in the background.
Now you’ve seen how to create a user interface and display text straight from program code. You also know something about the structure of an Android program. Let’s run the program. The ChessBoxing app has just one Java file, ChessBoxing.java. Find this file in Eclipse by going to the list on the left side of the screen and opening the src group under the ChessBoxing project. Next, open the package name (such as fi.sulautetut.android.chessboxing) and double-click ChessBoxing.java to open the file in the editor (see Figure 6-21). Replace all the code in ChessBoxing .java with the code in the previous listing. Click Run or press F11 to run ChessBoxing.
Figure 6-21. Cutting and pasting the program code over the Java file
When ChessBoxing.onCreate() runs, a text box displays “Welcome to BotBook .com Chess Boxing!” on the screen, as shown in Figure 6-22. Boxing Clock
Figure 6-22. The custom graphical user interface within the emulator
149
Creating a Boxing Clock in Android
The program does not override any other Activity class methods, which means that it does not respond to any events, such as user input.
Using a Timer for the Main Loop Repeat this, repeat this, repeat this. Animations, games, and many other programs are all based around a main loop. In fact, you’ve been programming Arduino this way: most of your execution time has been spent within the loop() function. Computers and cell phones usually execute many programs at the same time, so we can’t hog all the processing time as we do on Arduino; we have to share resources with other programs. Therefore, we will execute our main loop by triggering it periodically—for example, five times a second. That way, when the operating system needs to let another task do something, our program will know how to behave. The following program creates a timer that prints “Bling!” on the screen every 10 seconds. In a later example, we will use the same basic approach, but it will update an animation instead of printing text. // bca02chessBoxing - Timed events // (c) Kimmo Karvinen & Tero Karvinen http://BotBook.com package fi.sulautetut.android.chessboxing; import import import import
android.app.Activity; android.os.Bundle; android.os.Handler; android.widget.TextView;
public class ChessBoxing extends Activity { private Handler handler; 1 private TextView tv; 2 @Override public void onCreate(Bundle savedInstanceState) { 3 super.onCreate(savedInstanceState); tv = new TextView(this); tv.append('Welcome to BotBook.com Chess Boxing! '); setContentView(tv); handler = new Handler(); 4 handler.removeCallbacks(update); 5 handler.postDelayed(update, 50); 6 } private Runnable update = new Runnable() { 7 public void run() { 8 tv.append('Bling! '); handler.removeCallbacks(update); handler.postDelayed(this, 10*1000); 9 } }; }
Figure 6-23. Chess Boxing program printing “Bling!” in 10-second intervals
150
Copy the contents of this program into ChessBoxing.java and run it. Figure 6-23 shows the output.
Chapter 6
Creating a Boxing Clock in Android
This program has many similarities with the previous example. The only new feature in the code is the timer. You will define a new class variable of the type Handler for the entire ChessBoxing object. In the beginning of the program, within the onCreate() method, you will store a new Handler object and ask the timer to call the update() function, which is essentially your main loop. 1 Anything that appears in this part of the class declaration will be visible to all methods within the same class. For example, here the Handler variable is usable within the onCreate() method as well as in the update() method. We’ve specified the visibility of the variables as private. Within Java, any objects, methods, and variables marked as public are meant for use by other classes. If you are not building an API for other classes, you might as well set the visibility to private. The Handler class will define the timers, and we will create a Handler type of a variable called handler. 2 The previous program wrote to a text view, tv, from only one method, onCreate(). Now, the update() method must also access the text view, so the program must declare tv in the beginning of the class. 3 In the beginning of the program, call the default activity’s onCreate() method. 4 Create a new Handler object as the variable handler. A Handler class constructor does not take any parameters. 5 When you’re setting up a new timer, it is useful to remove old timers to ensure that they do not accrue on top of each other. We have to tell the removeCallBacks() method which timers are removed. Since this program only times update objects, the program gives update as its parameter. 6 Set the first timer, asking the timer handler to call our function after 50 milliseconds. Time is defined in milliseconds (ms), one thousandth of a second. Android’s Handler class is designed in such a manner that the function it invokes is called update.run(). We will define the update variable itself as a Runnable class object, which means we’ll have to define the run() method. Since a millisecond is one thousandth of a second, 50ms equals 0.050s, or one-twentieth of a second (50ms / 1,000ms = 5/100 = 1/20). A human being can’t really perceive a time period this short. 7 Create an update object in a special way that allows us to specify the class definition and instantiate it all at once. (Later, you will see a more traditional example of defining classes.) Here, we define a new object of type Runnable that’s visible to the rest of the class. 8 The content of our own run() method runs the task that the timer invokes. Here, the program just prints “Bling!” to the text view, but in a game or animation, all the other main loop code would go here. Boxing Clock
151
Creating a Boxing Clock in Android
Finally, call the timer again. Just as we did when creating the first timer, remove all existing timers to prevent them from accumulating. 9 Finally, request to call the update.run() again after 10 seconds. Time is given in milliseconds. To make it easier to read, it is helpful to specify the time as a multiplication (10 seconds * 1,000 milliseconds). Now the program will continue forever—or at least, until the phone is restarted (or unless the program runs unused in the background for a time and the OS decides to terminate it).
Adding Sound to the Boxing Clock When you’re boxing, it can be hard to check your cell phone to see if the round has ended. It’s time to add sounds to the Boxing Clock to make notification easier. The first version of the program will simply play back an MP3. Create a new project just as you did earlier in the chapter (close any open files from other projects first). From within Eclipse, choose File→New: Project→Android project. Set the properties shown in Table 6-1 and click Finish. Table 6-1. Project settings for the MP3 playback example
Only lowercase letters, numbers, and underscores can be used in the filenames (if you’re familiar with regular expressions, this class is represented as [a-z0-9_]).
Setting
Value
Project name
HelloMp3
Build Target
2.1
Application name
Hello mp3!
Package name
fi.sulautetut.android.hellomp3
Create Activity
HelloMp3
Min SDK Version
7
Run the program to make sure it works. When prompted to specify how to run it, choose Android Application. Now you can customize it to play an MP3. First, add the fightsound.mp3 MP3 file (download it from http://examples.oreilly .com/0636920010371) into the project resources. Find the res folder in the left side of the Eclipse window, create a new folder (right-click on res, choose New→Folder), and name the new folder raw. Drag fightsound.mp3 into this folder. When prompted to specify how to copy the file, choose Copy Files. This MP3 file will not appear on the cell phone’s filesystem. Instead, the Eclipse project will automatically create and import a class named R that exposes resources to your code. Open up the main activity HelloMp3.java (it’s under src/fi.sulautetut.android .hellomp3). Add the sound handling in the end of its onCreate() method like so: // bca03helloMp3 - Play a sound // (c) Kimmo Karvinen & Tero Karvinen http://BotBook.com package fi.sulautetut.android.hellomp3;
152
Chapter 6
Creating a Boxing Clock in Android import android.app.Activity; import android.media.MediaPlayer; 1 import android.os.Bundle; public class HelloMp3 extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); MediaPlayer fightSound = MediaPlayer.create(this, R.raw.fightsound); 2 fightSound.start(); 3 setContentView(R.layout.main); 4 } }
Let’s break down this code: 1 If you want to try out an interesting feature of the Eclipse IDE, remove this line from the code. You can add it back in automatically in Step 2. 2 Load the sound from the R object and play it. First, create a new variable of the MediaPlayer type and choose fightSound as its name. Instead of a regular constructor, call the MediaPlayer.create() factory method. The first parameter is its context (this, referring to the current class) and the second is the fully qualified name of the resource (fightsound). If you’d like to see the power of Eclipse, try deleting R.raw.fightsound from the code. Then start typing R., and Eclipse will give you a list of choices (if it does not, press Ctrl-Space). Double-click raw, type a period (.), and choose fightsound from the next list of choices (note that there is no filename extension, such as .mp3). If you deleted the import line back in Step 1, press Ctrl-Shift-O (Linux and Windows) or Command-Shift-O (Mac) and watch it reappear. 3 The Sound object’s method start() plays the sound.
Figure 6-24. HelloMp3 playing the MP3 file
4 This line establishes a full-screen view (see Figure 6-24) that is defined in an XML file, res/layout/main.xml (this file was generated when you created the project). You won’t use this in later projects (you’ll just create user interfaces with code). Now you can play sounds.
Creating a Ringing Boxing Clock Now let’s make the Boxing Clock play a sound every 10 seconds. (If you are eager to get to actual practice, you can change this to a full minute.) A ringing Boxing Clock can easily be used in sweatier games than chess. This project combines the timer and MP3 playback features you have already learned.
This program plays the sound only once. In fact, if you try to run the program again, Eclipse might inform you that it’s already running (“Activity not started, its current task has been brought to the front”). If you’d like to make sure it restarts each time, choose Run→Debug instead of Run→Run.
From within Eclipse, choose File→New: Project→Android project (close any other source code files first). Set the properties shown in Table 6-2 and click Finish.
Boxing Clock
153
Creating a Boxing Clock in Android Table 6-2. Ringing clock project settings
Setting
Value
Project name
LoudBoxing
Build Target
2.1
Application name
Boxing Clock
Package name
fi.sulautetut.android.loudboxing
Create Activity
LoudBoxing
Min SDK Version
7
As in the previous example, create a new folder called raw under res, and drag fightsound.mp3 into it. // bca04loudBoxing - Signal rounds with a gong sound. // (c) Kimmo Karvinen & Tero Karvinen http://BotBook.com package fi.sulautetut.android.loudboxing; import import import import import
android.app.Activity; android.media.MediaPlayer; android.os.Bundle; android.os.Handler; android.widget.TextView;
public class LoudBoxing extends Activity { private Handler handler; private TextView tv; MediaPlayer fightSound; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); tv = new TextView(this); tv.append('Welcome to BotBook.com Chess Boxing! '); setContentView(tv); fightSound= MediaPlayer.create(this, R.raw.fightsound ); 1 handler = new Handler(); handler.removeCallbacks(update); handler.postDelayed(update, 50); } private Runnable update = new Runnable() { public void run() { fightSound.start(); 2 tv.append('Bling! '); handler.removeCallbacks(update); handler.postDelayed(this, 10*1000); 3 } }; }
Here’s a closer look at the code: 1 As in the previous example, create a MediaPlayer object for the fight sound.
154
Chapter 6
Creating a Boxing Clock in Android
2 Play the sound from within the main loop. 3 Repeat the action, using the same technique used in “Using a Timer for the Main Loop.” Now your clock rings once every 10 seconds (Figure 6-25). Go jump rope or play a few rounds of chess; you deserve a little break. Even if you tap the home button, you’ll still hear the sound in the background. If you want to stop the program, go to the home screen, tap the Menu button, choose Applications→Manage Applications, and select Boxing Clock. Scroll to the bottom of the screen that appears and tap Force Stop.
Figure 6-25. LoudBoxing dinging loudly every 10 seconds
Setting Separate Rounds and Breaks Now we’ll set round and break times to be different lengths for cases when you might want to practice for two or three minutes and then have a oneminute break. You can either create a new project or modify the previous one. This program code uses different sounds for a round and a break, so in addition to fightsound .mp3, you’ll need to copy breaksound.mp3 into res/raw. // bca05roundAndBreak - Round and break with different length and sound. // (c) Kimmo Karvinen & Tero Karvinen http://BotBook.com package fi.sulautetut.android.loudboxing; import import import import import import
android.app.Activity; android.media.MediaPlayer; android.os.Bundle; android.os.Handler; android.os.SystemClock; android.widget.TextView;
public class LoudBoxing extends Activity { private Handler handler; private TextView tv; private MediaPlayer fightSound; private MediaPlayer breakSound; 1 private boolean fight=false; 2 private long pieStarted; // round or break, ms 3 private long pieEnds; private long fightLen=10*1000; // ms 4 private long breakLen=5*1000; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); tv = new TextView(this); tv.append('Welcome to BotBook.com Chess Boxing! '); setContentView(tv); fightSound= MediaPlayer.create(this, R.raw.fightsound ); breakSound= MediaPlayer.create(this, R.raw.breaksound );
Boxing Clock
155
Creating a Boxing Clock in Android handler = new Handler(); handler.removeCallbacks(update); handler.postDelayed(update, 50); } private Runnable update = new Runnable() { public void run() { 5 if (pieEnds