- •Acknowledgments
- •Preface
- •Who Should Read This Book
- •What’s in This Book
- •Arduino Uno and the Arduino Platform
- •Code Examples and Conventions
- •Online Resources
- •Complete Parts List
- •The Parts You Need
- •Starter Packs
- •1. Welcome to the Arduino
- •What You Need
- •What Exactly Is an Arduino?
- •Exploring the Arduino Board
- •Installing the Arduino IDE
- •Meeting the Arduino IDE
- •Hello, World!
- •Compiling and Uploading Programs
- •What If It Doesn’t Work?
- •Exercises
- •2. Creating Bigger Projects with the Arduino
- •What You Need
- •Managing Projects and Sketches
- •Changing Preferences
- •Using Serial Ports
- •What If It Doesn’t Work?
- •Exercises
- •3. Building Binary Dice
- •What You Need
- •Working with Breadboards
- •Using an LED on a Breadboard
- •First Version of a Binary Die
- •Working with Buttons
- •Adding Your Own Button
- •Building a Dice Game
- •What If It Doesn’t Work?
- •Exercises
- •4. Building a Morse Code Generator Library
- •What You Need
- •Learning the Basics of Morse Code
- •Building a Morse Code Generator
- •Fleshing Out the Morse Code Generator’s Interface
- •Outputting Morse Code Symbols
- •Installing and Using the Telegraph Class
- •Publishing Your Own Library
- •What If It Doesn’t Work?
- •Exercises
- •5. Sensing the World Around Us
- •What You Need
- •Measuring Distances with an Ultrasonic Sensor
- •Increasing Precision Using Floating-Point Numbers
- •Increasing Precision Using a Temperature Sensor
- •Creating Your Own Dashboard
- •What If It Doesn’t Work?
- •Exercises
- •What You Need
- •Wiring Up the Accelerometer
- •Bringing Your Accelerometer to Life
- •Finding and Polishing Edge Values
- •Building Your Own Game Controller
- •More Projects
- •What If It Doesn’t Work?
- •Exercises
- •7. Writing a Game for the Motion-Sensing Game Controller
- •Writing a GameController Class
- •Creating the Game
- •What If It Doesn’t Work?
- •Exercises
- •8. Generating Video Signals with an Arduino
- •What You Need
- •How Analog Video Works
- •Building a Digital-to-Analog Converter (DAC)
- •Connecting the Arduino to Your TV Set
- •Using the TVout Library
- •Building a TV Thermometer
- •Working with Graphics in TVout
- •What If It Doesn’t Work?
- •Exercises
- •9. Tinkering with the Wii Nunchuk
- •What You Need
- •Wiring a Wii Nunchuk
- •Talking to a Nunchuk
- •Building a Nunchuk Class
- •Using Our Nunchuk Class
- •Creating Your Own Video Game Console
- •Creating Your Own Video Game
- •What If It Doesn’t Work?
- •Exercises
- •10. Networking with Arduino
- •What You Need
- •Using Your PC to Transfer Sensor Data to the Internet
- •Registering an Application with Twitter
- •Tweeting Messages with Processing
- •Communicating Over Networks Using an Ethernet Shield
- •Using DHCP and DNS
- •What If It Doesn’t Work?
- •Exercises
- •Emailing from the Command Line
- •11. Creating a Burglar Alarm with Email Notification
- •What You Need
- •Emailing Directly from an Arduino
- •Detecting Motion Using a Passive Infrared Sensor
- •Bringing It All Together
- •What If It Doesn’t Work?
- •Exercises
- •What You Need
- •Understanding Infrared Remote Controls
- •Grabbing Remote Control Codes
- •Cloning a Remote
- •Controlling Infrared Devices Remotely with Your Browser
- •Building an Infrared Proxy
- •What If It Doesn’t Work?
- •Exercises
- •13. Controlling Motors with Arduino
- •What You Need
- •Introducing Motors
- •First Steps with a Servo Motor
- •Building a Blaminatr
- •What If It Doesn’t Work?
- •Exercises
- •Current, Voltage, and Resistance
- •Electrical Circuits
- •Learning How to Use a Wire Cutter
- •Learning How to Solder
- •Learning How to Desolder
- •The Arduino Programming Language
- •Bit Operations
- •Learning More About Serial Communication
- •Serial Communication Using Various Languages
- •What Are Google Chrome Apps?
- •Creating a Minimal Chrome App
- •Starting the Chrome App
- •Exploring the Chrome Serial API
- •Writing a SerialDevice Class
- •Index
What If It Doesn’t Work? • 143
-end
-print '0' * (8 - (image.columns % 8))
-puts ','
-end
20
- puts '};'
First, the program loads the rmagick library and imports the RMagick namespace. We do this to save some typing, because now we don’t have to fully qualify all classes that live in the RMagick namespace. In line 4, we read an image whose name we have to pass as a command-line argument. The image file’s format doesn’t matter, because ImageMagick understands nearly all image file formats. The image variable contains a representation of the image that doesn’t depend on the original file format any longer.
Next, we output the first three lines of the C++ file we’d like to generate. These lines are mostly static. Only the third line contains some variable parts—that is, the image’s width and the height.
Then we process the image’s pixels using two nested loops. The outer loop iterates through each row of the image, and the inner loop through each column. In line 13 we read the current pixel, and in the next line we use a cheap trick to determine whether the pixel is black or white. We know that our images consist only of black and white pixels, so it’s sufficient to check only one color component. If the red component is 0, the pixel has to be black. If it’s 1, the pixel has to be white. We transform every pixel into a bit value, and if the number of pixels in an image row isn’t divisible by 8 without a remainder, we fill the remaining bits with zeros.
You can run the program like this:
maik> ruby img2cpp.rb thermometer.png > thermometer.cpp
This call turns the thermometer.png file into a C++ file you can add to your Arduino project without any further modifications. That’s how software developers approach boring and error-prone tasks.
In the next chapter, you’ll learn how to connect a Wii Nunchuk to your Arduino, and we’ll use the TVout library to turn the Arduino into a video game console.
What If It Doesn’t Work?
Even if this chapter’s hardware is simple, a few things can still go wrong. If you don’t see a video signal at all on your TV set, make sure you’ve selected
report erratum • discuss
Chapter 8. Generating Video Signals with an Arduino • 144
the right input source. Usually its name is AV or Composite. When in doubt, try all of them.
Then check whether you’ve swapped the resistors. Connect the 470Ω resistor to pin D7 and the 1kΩ resistor to D9. Also, make sure the resistors have the right values.
If you see a distorted video signal, make sure you haven’t accidentally used NTSC instead of PAL or vice versa. Also, check all solder joints on the modified RCA cable. When in doubt, add more solder.
Exercises
•Use the TVout library to visualize some other sensor’s data. You can try to build the parking-distance control from Chapter 5, Sensing the World Around Us, on page 77, using the TVout library.
•Modify the TV thermometer so that it switches between degrees Celsius and degrees Fahrenheit every few seconds. Don’t just change the text; change the graphics, too.
report erratum • discuss
