Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Maik Schmidt - Arduino A Quick-Start Guide, 2nd Edition (The Pragmatic Programmers) - 2015.pdf
Скачиваний:
145
Добавлен:
22.03.2016
Размер:
30.47 Mб
Скачать

Early praise for Arduino: A Quick-Start Guide, Second Edition

Buy this book only if you don’t mind being sucked into an amazing world of Arduino hacking, programming, games, controllers, motors, tweeting, networking, and lots of other mind-blowing things!

Kevin Beam

Software engineer, National Snow and Ice Data Center (NSIDC)

Maik Schmidt’s writing style is engaging and makes complex concepts accessible. When I finished the book, I was daydreaming about future Arduino projects I could create.

Matthew Sullivan

Senior Rails/Ruby developer, Paradigmisr

A very well-written, thorough introduction to the Arduino platform. The second edition is a nice refinement of the first, with much updated as a result of the changes to the platform since the initial release of the book.

Mike Riley

Author, Programming Your Home, Build an Awesome PC, and Developing Android on Android

Arduino: A Quick-Start Guide,

Second Edition

Maik Schmidt

The Pragmatic Bookshelf

Dallas, Texas • Raleigh, North Carolina

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 The Pragmatic Programmers, LLC was aware of a trademark claim, the designations have been printed in initial capital letters or in all capitals. The Pragmatic Starter Kit, The Pragmatic Programmer, Pragmatic Programming, Pragmatic Bookshelf, PragProg and the linking g device are trademarks of The Pragmatic Programmers, LLC.

Every precaution was taken in the preparation of this book. However, the publisher assumes no responsibility for errors or omissions, or for damages that may result from the use of information (including program listings) contained herein.

Our Pragmatic courses, workshops, and other products can help you and your team create better software and have more fun. For more information, as well as the latest Pragmatic titles, please visit us at https://pragprog.com.

All circuit diagrams were created with Fritzing (http://fritzing.org).

The team that produced this book includes:

Susannah Davidson Pfalzer (editor)

Potomac Indexing, LLC (indexer)

Cathleen Small (copyeditor)

Dave Thomas (typesetter)

Janet Furlow (producer)

Ellie Callahan (support)

For international rights, please contact rights@pragprog.com.

Copyright © 2015 The Pragmatic Programmers, LLC.

All rights reserved.

No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form, or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior consent of the publisher.

Printed in the United States of America.

ISBN-13: 978-1-94122-224-9

Encoded using the finest acid-free high-entropy binary digits.

Book version: P2.0—March 2015

Contents

 

Acknowledgments . . . . . . . . .

.

.

xi

 

Preface . . . . . . . . . . . .

.

.

xiii

 

The Parts You Need . . . . . . . . .

.

.

xix

 

Part I — Getting Started with Arduino

 

 

 

1.

Welcome to the Arduino . . . . . . . .

.

.

3

 

What You Need

 

 

4

 

What Exactly Is an Arduino?

 

 

4

 

Exploring the Arduino Board

 

 

6

 

Installing the Arduino IDE

 

 

10

 

Meeting the Arduino IDE

 

 

14

 

Hello, World!

 

 

16

 

Compiling and Uploading Programs

 

 

19

 

What If It Doesn’t Work?

 

 

21

 

Exercises

 

 

22

2.

Creating Bigger Projects with the Arduino . . .

.

.

23

 

What You Need

 

 

23

 

Managing Projects and Sketches

 

 

24

 

Changing Preferences

 

 

26

 

Using Serial Ports

 

 

28

 

What If It Doesn’t Work?

 

 

35

 

Exercises

 

 

36

Contents vi

Part II — Eleven Arduino Projects

3.

Building Binary Dice . . . . . . . .

.

.

.

39

 

What You Need

 

 

 

 

 

39

 

Working with Breadboards

 

 

 

 

 

40

 

Using an LED on a Breadboard

 

 

 

 

 

41

 

First Version of a Binary Die

 

 

 

 

 

45

 

Working with Buttons

 

 

 

 

 

48

 

Adding Your Own Button

 

 

 

 

 

53

 

Building a Dice Game

 

 

 

 

 

55

 

What If It Doesn’t Work?

 

 

 

 

 

59

 

Exercises

 

 

 

 

 

60

4.

Building a Morse Code Generator Library

. .

.

.

.

61

 

What You Need

 

 

 

 

 

61

 

Learning the Basics of Morse Code

 

 

 

 

62

 

Building a Morse Code Generator

 

 

 

 

 

62

 

Fleshing Out the Morse Code Generator’s Interface

 

 

 

64

 

Outputting Morse Code Symbols

 

 

 

 

 

65

 

Installing and Using the Telegraph Class

 

 

 

 

67

 

Publishing Your Own Library

 

 

 

 

 

71

 

What If It Doesn’t Work?

 

 

 

 

 

73

 

Exercises

 

 

 

 

 

73

5.

Sensing the World Around Us

. . . . .

.

.

.

77

 

What You Need

 

 

 

 

 

78

 

Measuring Distances with an Ultrasonic Sensor

 

 

 

78

 

Increasing Precision Using Floating-Point Numbers

 

 

 

84

 

Increasing Precision Using a Temperature Sensor

 

 

 

86

 

Creating Your Own Dashboard

 

 

 

 

 

92

 

What If It Doesn’t Work?

 

 

 

 

 

97

 

Exercises

 

 

 

 

 

98

6.

Building a Motion-Sensing Game Controller . .

.

.

.

99

 

What You Need

 

 

 

 

 

100

 

Wiring Up the Accelerometer

 

 

 

 

 

100

 

Bringing Your Accelerometer to Life

 

 

 

 

102

 

Finding and Polishing Edge Values

 

 

 

 

103

 

Building Your Own Game Controller

 

 

 

 

106

 

More Projects

 

 

 

 

 

109

 

 

 

Contents vii

 

What If It Doesn’t Work?

 

 

110

 

Exercises

 

 

110

7. Writing a Game for the Motion-Sensing Game Controller

.

111

 

Writing a GameController Class

 

 

112

 

Creating the Game

 

 

114

 

What If It Doesn’t Work?

 

 

124

 

Exercises

 

 

124

8.

Generating Video Signals with an Arduino . . . .

.

127

 

What You Need

 

 

128

 

How Analog Video Works

 

 

128

 

Building a Digital-to-Analog Converter (DAC)

 

130

 

Connecting the Arduino to Your TV Set

 

131

 

Using the TVout Library

 

 

133

 

Building a TV Thermometer

 

 

134

 

Working with Graphics in TVout

 

 

139

 

What If It Doesn’t Work?

 

 

143

 

Exercises

 

 

144

9.

Tinkering with the Wii Nunchuk

. . . . . .

.

145

 

What You Need

 

 

145

 

Wiring a Wii Nunchuk

 

 

146

 

Talking to a Nunchuk

 

 

147

 

Building a Nunchuk Class

 

 

148

 

Using Our Nunchuk Class

 

 

152

 

Creating Your Own Video Game Console

 

153

 

Creating Your Own Video Game

 

 

153

 

What If It Doesn’t Work?

 

 

162

 

Exercises

 

 

162

10.

Networking with Arduino . . . . . . . .

.

163

 

What You Need

 

 

164

 

Using Your PC to Transfer Sensor Data to the Internet

 

164

 

Registering an Application with Twitter

 

167

 

Tweeting Messages with Processing

 

 

168

 

Communicating Over Networks Using an Ethernet Shield

 

173

 

Using DHCP and DNS

 

 

177

 

What If It Doesn’t Work?

 

 

180

 

Exercises

 

 

181

 

 

 

Contents viii

11.

Creating a Burglar Alarm with Email Notification . .

.

183

 

What You Need

 

 

184

 

Emailing from the Command Line

 

 

184

 

Emailing Directly from an Arduino

 

 

189

 

Detecting Motion Using a Passive Infrared Sensor

 

193

 

Bringing It All Together

 

 

197

 

What If It Doesn’t Work?

 

 

200

 

Exercises

 

 

200

12.

Creating Your Own Universal Remote Control . . .

.

201

 

What You Need

 

 

202

 

Understanding Infrared Remote Controls

 

202

 

Grabbing Remote Control Codes

 

 

203

 

Cloning a Remote

 

 

207

 

Controlling Infrared Devices Remotely with Your Browser

 

212

 

Building an Infrared Proxy

 

 

216

 

What If It Doesn’t Work?

 

 

223

 

Exercises

 

 

223

13.

Controlling Motors with Arduino . . . . . .

.

225

 

What You Need

 

 

225

 

Introducing Motors

 

 

226

 

First Steps with a Servo Motor

 

 

227

 

Building a Blaminatr

 

 

230

 

What If It Doesn’t Work?

 

 

235

 

Exercises

 

 

235

 

Part III — Appendixes

 

 

A1.

Electronics and Soldering Basics . . . . . .

.

239

 

Current, Voltage, and Resistance

 

 

239

 

Electrical Circuits

 

 

239

 

Learning How to Use a Wire Cutter

 

 

243

 

Learning How to Solder

 

 

243

 

Learning How to Desolder

 

 

247

A2.

Advanced Arduino Programming

. . . . . .

.

249

 

The Arduino Programming Language

 

249

 

Bit Operations

 

 

251

 

 

 

 

Contents ix

A3.

Advanced Serial Programming . . . . .

.

.

.

253

 

Learning More About Serial Communication

 

 

 

253

 

Serial Communication Using Various Languages

 

 

 

255

A4.

Controlling the Arduino with a Browser . . .

.

.

.

267

 

What Are Google Chrome Apps?

 

 

 

267

 

Creating a Minimal Chrome App

 

 

 

269

 

Starting the Chrome App

 

 

 

270

 

Exploring the Chrome Serial API

 

 

 

271

 

Writing a SerialDevice Class

 

 

 

274

A5.

Bibliography . . . . . . . . .

.

.

.

281

 

Index . . . . . . . . . . .

.

.

.

283

Acknowledgments

Susannah Davidson Pfalzer was the editor of the first edition of this book. When planning the second edition, I hadn’t forgotten how difficult it was to write the first one, but I also remembered how great it was to work with her. Again, she turned this endeavor into a real pleasure. Thank you very much!

This is not the first book I’ve written for the Pragmatic Bookshelf, so I knew already how professional and nice everyone on the team is. Still, they get even better every time, and I’d like to thank everyone for making this book happen.

This book would not have been possible without the stunning work of the whole Arduino team. Thank you so much for creating Arduino!

A big thank you goes to all the people who contributed material to this book: Christian Rattat took all the book’s photos, Kaan Karaca created the Blaminatr’s display, and Kassandra Perch improved the JavaScript code in the

“Creating Your Own Universal Remote Control” chapter.

I created all circuit diagrams with Fritzing,1 and I’d like to thank the Fritzing team for making such a great tool available for free.

For the games I developed for this book, I needed some artwork, and I’ve found amazing things on the OpenGameArt website.2 I used some graphics contributed by www.kenney.nl3 and a great song created by Alex Smith4 for my breakout clone. Thank you for putting these into the public domain.

The background image of the browser game comes from ESA/Hubble, NASA, Digitized Sky Survey, MPG/ESO (acknowledgment: Davide de Martin). The image showing how raster scan works was created by Ian Harvey.

1.http://fritzing.org/

2.http://opengameart.org/

3.http://opengameart.org/content/puzzle-game-art

4.http://opengameart.org/content/awake-megawall-10

report erratum • discuss

Acknowledgments xii

For an author, there’s nothing more motivating and valuable than feedback. I’d like to thank my reviewers: Kevin Beam, Jessica Janiuk, Kassandra Perch, Mike Riley, Sam Rose, and Matthew Sullivan. This book is so much better because of your insightful comments and suggestions!

Finally, I have to thank my wonderful wife, Tanja, and my adorable son, Mika, for being patient and understanding whenever I had to write yet another page.

report erratum • discuss

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]