Programming Interactivity
A Designer’s Guide to Processing, Arduino, and openFrameworks
Joshua Noble
Programming Interactivity by Joshua Noble
Copyright © 2009 Joshua Noble. All rights reserved.
Printed in the United States of America.
Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.
O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://my.safaribooksonline.com). For more information, contact our corporate/institutional sales department: (800) 998-9938 or corporate@oreilly.com.
Editor:
Steve WeissProduction Editor:
Sumita MukherjiCopyeditor:
Kim WimpsettProofreader:
Sumita MukherjiProduction Services:
NewgenIndexer:
Ellen Troutman ZaigCover Designer:
Karen MontgomeryInterior Designer:
David FutatoIllustrator:
Robert RomanoPrinting History:
July 2009: First Edition.
Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly Media, Inc. Programming Interactivity, the image of guinea fowl, and related trade dress are trademarks 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.
While every precaution has been taken in the preparation of this book, the publisher and author assume no responsibility for errors or omissions, or for damages resulting from the use of the information con- tained herein.
TM
This book uses RepKover™, a durable and flexible lay-flat binding.
ISBN: 978-0-596-15414-1 [M]
Table of Contents
Preface . . . xv
Part I. Introductions
1. Introducing Interaction Design . . . 3
What This Book Is for 3
Programming for Interactivity 4
The Nature of Interaction 5
Messages and Interaction 7
Interfaces and Interaction 8
Languages of Interaction 10
Design and Interaction 12
Art and Interaction 13
Data Exchange and Exploration 15
Working Process 19
2. Programming Basics . . . 21
Why You’ll Read This Chapter More Than Once 22
The Nature of Code 22
Variables 23
Simple Types 24
Arrays 29
Casting 33
Operators 33
Control Statements 37
if/then 37
for Loop 38
while Loop 39
continue 40
break 40
Functions 41
Defining a Function 41
Passing Parameters to a Method 42
Some Suggestions on Writing Functions 44
Overloading Functions 44
Objects and Properties 46
Scope 49
Review 50
3. Processing . . . 53
Downloading and Installing Processing 54
Exploring the Processing IDE 54
The Basics of a Processing Application 56
The setup() Method 56
The draw() Method 57
The Basics of Drawing with Processing 60
The rect(), ellipse(), and line() Methods 60
RGB Versus Hexadecimal 62
The fill() Method 63
The background() Method 65
The line() Method 65
The stroke() and strokeWeight() Methods 65
The curve() Method 66
The vertex() and curveVertex() Methods 66
Capturing Simple User Interaction 67
The mouseX and mouseY Variables 68
The mousePressed() Method 69
The mouseReleased() and mouseDragged() Methods 70
The keyPressed and key Variables 73
Importing Libraries 77
Downloading Libraries 77
Loading Things into Processing 79
Loading and Displaying Images 79
Displaying Videos in the Processing Environment 81
Using the Movie Class 81
Reading and Writing Files 83
Running and Debugging Applications 85
Exporting Processing Applications 86
Conclusion 88
Review 89
4. Arduino . . . 91
Starting with Arduino 92
Installing the IDE 93
Configuring the IDE 96
Touring Two Arduino Boards 97
The Controller 97
Duemilanove Versus Mini 97
Touring the Arduino IDE 102
The Basics of an Arduino Application 105
The setup Statement 106
The loop Method 106
Features of the Arduino Language 108
Constants 109
Methods 110
Arrays 111
Strings 112
How to Connect Things to Your Board 115
Hello World 117
Debugging Your Application 122
Importing Libraries 124
Running Your Code 126
Running Your Board Without a USB Connection 126
Review 127
5. Programming Revisited . . . 129
Object-Oriented Programming 129
Classes 130
The Basics of a Class 131
Class Rules 132
Public and Private Properties 133
Inheritance 135
Processing: Classes and Files 137
C++: Classes and Files 139
.cpp and .h 140
A Simple C++ Application 142
Pointers and References 144
Reference 146
Pointer 146
When to Use Pointers 147
Large Data Objects 148
Pointers and Arrays 149
When Are You Going to Use This? 150
Review 151
6. openFrameworks . . . 153
Your IDE and Computer 154
Windows 154
Mac OS X 155
Linux 155
Taking Another Quick Tour of C++ 156
Basic Variable Types 157
Arrays 157
Methods 158
Classes and Objects in C++ 159
Getting Started with oF 160
Touring an oF Application 166
Methods 166
Variables 168
Creating “Hello, World” 168
Drawing in 2D 171
Setting Drawing Modes 172
Drawing Polygons 174
Displaying Video Files and Images 176
Images 176
Video 178
Importing Libraries 180
ofxOpenCv 181
ofxVectorGraphics 181
ofxVectorMath 182
ofxNetwork 182
ofxOsc 182
Compiling an oF Program 183
Compiling in Xcode 183
Compiling in Code::Blocks 184
Debugging an oF Application 184
Using the printf Statement 184
Using the GNU Debugger 185
Using the Debugger in Xcode 186
Using the Debugger in Code::Blocks 188
Review 188
Part II. Themes
7. Sound and Audio . . . 193
Sound As Feedback 194
Sound and Interaction 197
How Sound Works on a Computer 199
Audio in Processing 202
Instantiating the Minim Library 202
Generating Sounds with Minim 204
Filtering Sounds with Minim 208
Sound in openFrameworks 214
openFrameworks and the FMOD Ex Library 221
The Sound Object Library 228
The Magic of the Fast Fourier Transform 233
Physical Manipulation of Sound with Arduino 238
A Quick Note on PWM 239
Creating Interactions with Sound 242
Further Resources 242
Review 243
8. Physical Input . . . 245
Interacting with Physical Controls 245
Thinking About Kinetics 246
Getting Gear for This Chapter 247
Controlling Controls 248
The Button As an Electrical Object 248
The Button As an Interactive Object 248
The Button As a Value in Code 248
Turning Knobs 249
The Dial As an Interactive Object 249
Potentiometers 249
Using Lights 251
Wiring an LED 252
Detecting Touch and Vibration 253
Reading a Piezo Sensor 254
Getting Piezo Sensors 255
Communicating with Other Applications 259
Sending Messages from the Arduino 262
openFrameworks 263
Detecting Motion 265
PIR Motion Sensor 265
Reading Distance 267
Reading Input from an Infrared Sensor 269
Understanding Binary Numbers 270
Binary Numbers 270
Bits and Bit Operations 271
Why Do You Need to Know Any of This? 273
Detecting Forces and Tilt 273
Introducing I2C 278
What Is a Physical Interface? 283
What’s Next 284
Review 286
9. Programming Graphics . . . 289
The Screen and Graphics 289
Seeing Is Thinking, Looking Is Reading 292
Math, Graphics, and Coordinate Systems 293
Drawing Strategies 296
Use Loops to Draw 296
Use Arrays to Draw 298
Draw Only What You Need 303
Use Sprites 303
Processing and Transformation Matrices 303
Creating Motion 307
Shaping the Gaze 308
Setting the Mood 308
Creating Tweens 310
Using Vectors 315
Using Graphical Controls 325
ControlP5 Library 326
Event Handling 326
Importing and Exporting Graphics 328
Using PostScript in Processing 329
Using PostScript Files in oF 330
What’s Next 333
Review 334
10. Bitmaps and Pixels . . . 337
Using Pixels As Data 338
Using Pixels and Bitmaps As Input 340
Providing Feedback with Bitmaps 341
Looping Through Pixels 342
Manipulating Bitmaps 345
Manipulating Color Bytes 347
Using Convolution in Full Color 348
Analyzing Bitmaps in oF 349
Analyzing Color 350
Analyzing Brightness 351
Detecting Motion 353
Using Edge Detection 355
Using Pixel Data 361
Using Textures 368
Textures in oF 369
Textures in Processing 373
Saving a Bitmap 375
What’s Next 376
Review 377
11. Physical Feedback . . . 379
Using Motors 380
DC Motors 381
Stepper Motors 384
Other Options 386
Using Servos 386
Connecting a Servo 387
Communicating with the Servo 387
Wiring a Servo 388
Using Household Currents 392
Working with Appliances 393
Introducing the LilyPad Board 395
Using Vibration 397
Using an LED Matrix 404
Using the Matrix Library 404
Using the LedControl Library 407
Using the SPI Protocol 410
Using LCDs 412
Serial LCD 416
Using Solenoids for Movement 417
What’s Next 420
Review 421
12. Protocols and Communication . . . 423
Communicating Over Networks 425
Using XML 426
Understanding Networks and the Internet 429
Network Organization 429
Network Identification 430
Network Data Flow 431
Handling Network Communication in Processing 432
Client Class 432
Server Class 433
Sharing Data Across Applications 436
Understanding Protocols in Networking 441
Using ofxNetwork 442
Creating Networks with the Arduino 450
Initializing the Ethernet Library 451
Creating a Client Connection 452
Creating a Server Connection 453
Using Carnivore to Communicate 456
Installing the Carnivore Library 457
Creating a Carnivore Client 458
Communicating with Bluetooth 460
Using Bluetooth in Processing 461
Using the bluetoothDesktop Library 461
Using the Arduino Bluetooth 464
Communicating Using MIDI 467
Review 471
Part III. Explorations
13. Graphics and OpenGL . . . 475
What Does 3D Have to Do with Interaction? 475
Understanding 3D 476
Working with 3D in Processing 477
Lighting in Processing 478
Controlling the Viewer’s Perspective 480
Making Custom Shapes in Processing 484
Using Coordinates and Transforms in Processing 487
Working with 3D in OpenGL 489
So, What Is OpenGL? 489
Transformations 490
OpenGL in Processing 490
Open GL in openFrameworks 492
Using Matrices and Transformations in OpenGL 493
Using Vertices in OpenGL 496
Drawing with Textures in oF 496
Lighting in OpenGL 500
Blending Modes in OpenGL 501
Using Textures and Shading in Processing 506
Applying Material Properties 507
Using Another Way of Shading 508
What Does GLSL Look Like? 508
Vertex Shaders 508
Fragment Shader 509
Variables Inside Shaders 510
Using an ofShader Addon 510
What to Do Next 513
Review 514
14. Detection and Gestures . . . 517
Computer Vision 518
Interfaces Without Controls 519
Example CV Projects 520
OpenCV 521
Using Blobs and Tracking 521
Starting with ofxOpenCV 522
Tracking Blobs with ofxOpenCV 527
Using OpenCV in Processing 537
Exploring Further in OpenCV 542
Detecting Gestures 543
Using ezGestures in Processing 544
Using Gestures in oF 548
Implementing Face Recognition 550
Exploring Touch Devices with oF 554
TouchKit 554
Tuio 555
Touchlib 555
reacTIVision 555
What’s Next 556
Review 557
15. Movement and Location . . . 559
Using Movement As and in Interaction 559
Using Software-Based Serial Ports 561
Understanding and Using GPS 563
Storing Data 575
Logging GPS Data to an Arduino 577
Using the Breadcrumbs Library 578
Implementing Hardware-Based Logging 579
Sending GPS Data 580
Determining Location by IP Address 583
What to Do Next 589
Review 589
16. Interfaces and Controls . . . 591
Examining Tools, Affordances, and Aesthetics 592
Reexamining Tilt 593
Exploring InputShield 597
Understanding Touch 599
Exploring Open Source Touch Hardware 600
Nort_/D 600
Liquidware TouchShield 603
Drawing to the TouchShield Screen 607
Controlling Servos Through the TouchShield 609
Setting Up Communication Between Arduino and TouchShield 611
Communicating Using OSC 614
Using the Wiimote 616
Using the Wii Nunchuck in Arduino 616
Tracking Wii Remote Positioning in Processing 622
What’s Next 625
Review 626
17. Spaces and Environments . . . 627
Using Architecture and Space 627
Sensing Environmental Data 628
Using an XBee with Arduino 629
Creating a Simple Test 632
Configuring the XBee Module 634
Addressing in the XBee 635
XBee Library for Processing 637
Placing Objects in 2D 641
Using the X10 Protocol 651
Setting Up an RFID Sensor 654
Reading Heat and Humidity 659
What’s Next 664
Review 664
18. Further Resources . . . 667
What’s Next? 667
Software Tools 667
Construction Processes 670
Artificial Intelligence 671
Physics 677
Hardware Platforms 678
Bibliography 681
Interaction Design 681
Programming 682
Hardware 683
Art 683
Conclusion 684
Appendix: Circuit Diagram Symbols . . . 685
Programming Glossary . . . 687
Index . . . 693
Preface
This is a book about creating physical interaction with computer systems. It focuses on designing hardware and programming for systems that use either physical input or physical feedback. This book has been a dream of mine since I was an art student beginning to create interactive installations and finding that there was no simple in- troduction to the topics that I wanted to explore. At the time, I didn’t know what platforms, tools, and programming languages were available for creating interactive art, and I didn’t know where to find more information about these topics that a relative novice programmer could understand. As I began teaching, I was asked the same ques- tion again and again by students: “where do I begin?” Much has changed in the seven years since then, though, and now many excellent projects are helping beginners pro- gram, artists create, and programmers rapidly prototype applications. We’ll cover three of these projects in this book: Processing, Arduino, and openFrameworks. This book intends to answer the question “Where do I begin?” in as comprehensive a manner as possible. It is the intention of this book to be useful for almost any type of project. This book will provide technical advice, critical commentary for you to consider, code that you can use, hardware diagrams that you can use, and further resources for you to explore.
Ten years ago, the idea of artists or designers writing code or designing hardware was almost unheard of. Today, not only has it become commonplace, but it has become an important arena of expression and exploration. The dialogue between technology and design is a vital and vibrant one that shapes art and technology alike. I hope that this book can be, in some small way, another path into this conversation for more artists and designers.
Who This Book Is For
This book is aimed at designers, artists, amateur programmers, or anyone interested in
working with physical interaction in computing. No assumption is made about your
technical background or previous experience. The only assumption is that you are
interested in learning to program and build hardware. This book is an introduction to
a great number of topics, and throughout the book we list links to further resources so
you can expand your knowledge or explore a particular topic that interests you.
We encourage you to make as much use as possible of these resources and to use this book as a map for exploring a great number of technologies and techniques.
How This Book Is Organized
This book is broken into three parts. The first introduces the three projects that will be used throughout this book, the second introduces some of the most common themes in creating interaction in designs and applications, and the third introduces some of the more advanced topics that you may want to explore further. Also included with some of the chapters are interviews with programmers, artists, designers, and authors who work with the tools covered in this book. Covering such a massive range of topics means that this book doesn’t go into great depth about most of them, but it is filled with references to other books, websites, designers, and artists that you may find helpful or inspiring.
What Is—and Isn’t—in This Book
My excitement about the ideas and rapid growth of the field of interaction design is hard to contain. However, as exciting and far-reaching as interaction design is, the limitations of time and physical book size dictate that I be selective about what is and isn’t covered in this book.
What’s in
This book covers Processing, Arduino, and openFrameworks. To help novice pro- grammers, it covers some of the core elements of programming in C and C++ for Ar- duino and openFrameworks and also covers the Processing language. We introduce dozens of libraries for openFrameworks and Processing—too many to list here. Some of these are official libraries or add-ons for the two frameworks, and some are simply extensions that have been created for this book or provided by altruistic coders.
We also introduce some of the basics of electronics and how computer hardware functions, as well as many tools and components that you can use with an Arduino.
The Arduino and Processing IDEs are covered, as are two different IDEs for openFrameworks, namely, CodeBlocks, and Xcode. The Arduino Duemilanove and Mini are covered in depth, and we discuss other boards only briefly. We cover many electronic components that have designed expressly for the Arduino, called shields, in depth as well.
What’s Not in
While this book shows how to create some circuits, it doesn’t cover a great deal of the
fundamentals of electronics or hardware, how to create circuits, or electronics theory.
Chapter 18 lists some excellent tutorials and references. While the book does cover the Processing subset of the Java programming language, to conserve space and maintain focus, it doesn’t cover Java. The book doesn’t cover many aspects of C++, such as templates, inline functions, operator overloading, and abstract classes. Again, though, listed in Chapter 18 are several excellent resources that you can use to learn about these deeper topics in C++.
There are so many Arduino-compatible boards now that it’s almost impossible to cover them all in depth; the book mentions the Mega, the Nano, and several other boards only in passing and leaves out many of the Arduino-compatible boards that are not created by the Arduino team. Quite a few components and other tools that we would have liked to discuss in depth could not be included to maintain scope and to save space. A good camera for computer vision was not included either, though a glance at the openFrameworks or Processing forums will likely provide a more up-to-date dis- cussion than could have been given here.
Many topics that we would have liked to include have been left out because of space considerations: artificial intelligence, data visualization, and algorithmic music, among others. Though these are all potentially interesting areas for artists and designers, the focus of the book is on teaching some of the theory and techniques for interaction design as well as the basics of hardware and programming. The resources listed at the end of the book can provide the names of some materials that might help you explore these topics.
Companion Website
All the code included in this book are available for download from the book’s com- panion website, http://www.oreilly.com/catalog/9780596154141.
Typographical Conventions Used in This Book
The following typographical conventions are used in this book:
Italic
Indicates new terms, URLs, email addresses, filenames, file extensions, path names, and directories.
Constant width
Indicates direct references to code, text output from executing scripts, XML tags, HTML tags, and the contents of files.
Constant width bold
Shows commands or other text that should be typed literally by the user.
Constant width italic