You are on page 1of 11

B UILDING A B LAMINATR

232

Download Motors/Blaminatr/Blaminatr.pde

#include <Servo.h> const unsigned int MOTOR_PIN = 9; const unsigned int MOTOR_DELAY = 15; class Blaminatr { Team _team; Servo _servo; public: Blaminatr(const Team& team) : _team(team) {} void attach(const int sensor_pin) { _servo.attach(sensor_pin); delay(MOTOR_DELAY); } void blame(const char* name) { _servo.write(_team.get_position(name)); delay(MOTOR_DELAY); } };

A Blaminatr object aggregates a Team object and a Servo object. The constructor initializes the Team instance while we can initialize the Servo instance by calling the attach( ) method. The most interesting method is blame( ). It expects the name of the team member to blame, calculates his position, and moves the servo accordingly. Let’s put it all together now:
Download Motors/Blaminatr/Blaminatr.pde Line 1 5 10 15 -

const unsigned int MAX_NAME = 30; const unsigned int BAUD_RATE = 9600; const unsigned int SERIAL_DELAY = 5; char* members[] = { "nobody", "Bob", "Alice", "Maik", NULL }; Team team(members); Blaminatr blaminatr(team); void setup() { Serial.begin(BAUD_RATE); blaminatr.attach(MOTOR_PIN); blaminatr.blame("nobody"); } void loop() { char name[MAX_NAME + 1];
Report erratum

Download from Wow! eBook <www.wowebook.com>

this copy is (P1.0 printing, Janurary, 2011)

6. Of course. delay(SERIAL_DELAY). Serial. 7. The only difference is that we do not control a servo directly but call blame( ) in line 28. In the setup( ) function. you can use them to build robots running on wheels or similar devices.html Report erratum Download from Wow! eBook <www.com or http://makershed. } name[i] = 0.com/roboduino.” so we don’t have to deal with the rare edge case when nobody is to blame.5 What If It Doesn’t Work? Working with motors is surprisingly easy.println(" is to blame.print(name). Then we use members to initialize a new Team object and pass this object to the Blaminatr’s constructor. you can use motors for more serious projects. we initialize the Blaminatr by blaming “nobody.wowebook.available() && i < MAX_NAME) { const char c = Serial. Also. Compile and upload the software and start to blame. Attach them directly to the motor or—even better—put everything into a nice box.curiousinventor. } } 233 20 25 30 We define a list of member names that is terminated by a NULL pointer.com.” The loop( ) function is nearly the same as in Section 10.0 printing.3. The biggest problem is that motors consume a lot of power. http://store. Serial. First Steps with a Servo Motor . That’s it! You can now start to draw your own display and create your own arrow. So if you have a project in mind that needs a significant number of motors.com> this copy is (P1. Janurary. while (Serial. if (c != -1 && c != '\n') name[i++] = c.7 10. on page 225. The list’s first entry is “nobody. You can find them at http://adafruit. blaminatr."). we initialize the serial port and attach the Blaminatr’s servo motor to the pin we defined in MOTOR_PIN. For example. you should consider buying a motor shield6 or use a special shield such as the Roboduino.read(). because it is not meant for driving bigger loads. 2011) . but still a lot of things can go wrong.blame(name).W HAT I F I T D OESN ’ T W ORK ? if (Serial. But you cannot attach too many motors to a “naked” Arduino.available()) { int i = 0.

Pointing your Report erratum Download from Wow! eBook <www. but you might run into problems if you attach bigger and heavier things. the device turns the timer automatically. Also.E XERCISES 234 More Motors Projects Motors are fascinating.∗ It uses a servo motor to rotate a hypno disc—a rotating disk with a spiral printed on it that has an hypnotic effect.wowebook. Janurary. That’s all nice. The motor’s shaft always needs to move freely. and attach an AC or DC adapter to your Arduino if necessary. and the USB hourglass uses the signals from its optical sensor to generate random numbers. You also shouldn’t attach too much weight to your motor. 10. the faster it spins. and it observes the falling sand using an optical sensor. A useful and exciting project is the USB hourglass. ∗.† It uses an Arduino and a servo motor to turn a sand timer. but the device’s main purpose is to generate true random numbers. sending them to the serial port. †.flickr. http://www. Falling sand is a perfect basis for generating true randomness (see the sidebar on page 73).0 printing.com> this copy is (P1. be careful not to put any obstacles into the motor’s way. Some motors have to be adjusted from time to time. you cannot easily drive more than one motor.6 Exercises • Add an Ethernet shield to the Blaminatr so you can blame people via Internet and not only via the serial port. 2011) . and usually you have to do that with a very small screw driver. If your motor does not run as expected. check its specification. so the closer you get to the disc. Refer to the motor’s specification for detailed instructions.comcast. Also.net/~hourglass/ so you cannot simply attach every motor to an Arduino. Whenever all the sand has fallen through. especially not with the small amount of power you get from a USB port. An infrared rangefinder changes the motor’s speed. A fun project is the Arduino Hypnodisk. Moving an arrow made of paper is no problem.com/photos/kevino/4583084700/in/pool-make http://home. Search the ’net and you’ll find numerous projects combining the Arduino with them.

• Use an IR receiver to control the Blaminatr.wowebook.0 printing.42/blame/Maik should blame me.168. For example. Janurary. you could use the channel key of your TV set’s remote control to proceed the Blaminatr from one name to the other.1.6.com> this copy is (P1. you have to move an arrow that points to the current temperature.E XERCISES 235 Figure 10. Report erratum Download from Wow! eBook <www. 2011) .6: A motorized thermometer web browser to an address such as http://192. for example. that is. Its display could look like Figure 10. • Create a thermometer based on a TMP36 temperature sensor and a servo motor.

com> .Part III Appendixes Download from Wow! eBook <www.wowebook.

and you might not know how to calculate the resistor’s size for a given LED. you already know that you always have to put a resistor in front of an LED.1 Current. on the following page. In this appendix. you’ll learn more about resistors. and you’ll learn about Ohm’s law.wowebook.com> . Also. Voltage. For example. you’ll learn the basics of electricity. which is probably the most important law in electronics. But it’s a good idea to learn a bit about electricity and about soldering if you want to build bigger and more sophisticated projects. In Figure A. Download from Wow! eBook <www. and the higher the voltage. but you might not know exactly why. electrons flow in an electrical circuit. and resistance is all about. Let’s remedy that. you didn’t need to know much about electricity. you’ll need to understand what current. But at some point. An electrical circuit resembles a water circuit in many respects.Appendix A Basics of Electronics We didn’t need a lot of theory or background to create our first Arduino projects. Voltage is electricity’s equivalent of water pressure and is measured in volts (V). you can see a water circuit on the left and an electrical circuit on the right. and you’ll see that soldering isn’t as difficult as it might seem. and Resistance To build your first projects with the Arduino. the faster the current flows.1. While water flows in a water circuit. A. Voltage is the initial cause for a current. Isn’t it fascinating how similar they are and that you can even find a connection between them when you use a water-driven dynamo that acts as a power supply? Let’s take a closer look at their most important attributes. voltage.

In the past. Resistance is an important physical phenomenon that is closely related to current and voltage.com> this copy is (P1.0 printing. it’s the pipes the water is flowing through or perhaps a water mill. we measure current in ampere. current is the amount of electricity flowing through an electric line. He postulated the following form we call Ohm’s law today:1 • I (current) = V (voltage) / R (resistance) This is equivalent to the following: • R (resistance) = V (voltage) / I (current) • V (voltage) = R (resistance) × I (current) So. It is the equivalent of the actual flow of water in a water circuit.1: Water circuits and electrical circuits are similar. In an electrical circuit. While we measure the water flow in liters per minute.24 × 1018 electrons are flowing per second. We use I as the current’s letter for historical reasons. it is the wire or a light bulb. it stood for inductance. In a water circuit. AND R ESISTANCE 238 Current Current (I) Water Pump Water Mill Dynamo Power Supply Voltage (V) + Resistance (R) Pipe Wire Figure A. Every component in a circuit—be it water or electricity—resists some amount of current. One ampere means that approximately 6. and its official symbol is Ω. The German physicist Georg Ohm found out that current depends on voltage and resistance. V OLTAGE . We measure it in Ohms.wowebook. for two given values. Report erratum Download from Wow! eBook <www. In electronics. Janurary.C URRENT. you can calculate the third one. Ohm’s law is the only formula you’ll absolutely have to learn when learning electron1. 2011) .

02 A should pass the LED.wowebook.02 A) should flow through our resistor also. Also. This value is called voltage drop. The most important attribute of a resistor is its resistance value that is measured in Ohm. we want a maximum of 20 mA to flow through the LED.8V and 3. V OLTAGE . This implies that a maximum of 20 mA (0. Metal resistors are more precise and don’t create so much noise.5 volts are left for the LED. and it’s hard to read text Report erratum Download from Wow! eBook <www. That’s because we’d decrease the current even more: I = 2.5V / 220Ω = 11mA Resistors You’ll hardly ever find an electronics project that doesn’t need resistors. you’ll usually find two values: a forward voltage and a current rating.5V / 0. so only 2. We also assume that we have a power supply delivering 5 volts (as the Arduino does. use a bigger one such as 150Ω or 220Ω.02A = 125Ω This means we need a 125Ω resistor for our LED. for example). Now that we know that 2. because resistors are small parts.5V / 150Ω = 17mA I = 2.5 volts from the circuit. In simple circuits. If you look at a LED’s data sheet. The forward voltage usually is somewhere between 1. Let’s say we have an LED with a maximum of 2. and the maximum current often is 20 mA (milliamperes). So. Janurary. It will still protect the LED and only slightly decrease its brightness.5 = 2. What’s the right size of the resistor we need to put in front of the LED? We have to make sure that the resistor takes 5 – 2.com> this copy is (P1.0 printing.5 volts and a safe current of 20 mA. but carbon resistors are a bit cheaper. Usually you’ll use carbon or metal resistors.C URRENT. for example. we have the following: R = 2. it usually doesn’t matter which type you use. AND R ESISTANCE 239 ics. If you do not have a 125Ω resistor. you’ll need them often and should get familiar with them a bit more.5 V and 0.6V. we can use Ohm’s law to calculate the resistance R: R=V/I In our case. Only a few vendors actually print this value on the resistor. it helps you calculate the size of the resistor you need. 2011) . When working with LEDs.

on the next page. Report erratum Download from Wow! eBook <www. Using the remaining stripes. so the value is 12. In Figure A. SMD resistors don’t have them). red (2). red (2). 2011) .000Ω = 12. One of them is separated from the others by a gap (see Figure A. you can see three examples: • On the first resistor we find four stripes: brown (1).C URRENT. violet (7). on the next page).wowebook. green (5 zeros). The rightmost stripe—that is the third or fourth one— stands for an amount of zeros to be added to the preceding digits.2). So. silver (±10%).com> this copy is (P1. That means we have a resistor value of 150Ω. Janurary. and no stripe means ±20 percent. they use a trick and encode the value using colored stripes.2: Resistor values are encoded using colors. silver for ±10 percent.0 printing. AND R ESISTANCE 240 Color Code Zeros Black 0 Brown 1 0 Red 2 00 Orange 3 000 Yellow 4 0000 Green 5 00000 Blue 6 000000 Violet 7 0000000 Gray 8 00000000 White 9 000000000 Figure A. • The second resistor has four stripes again: yellow (4). V OLTAGE .3. Gold stands for an accuracy of ±5 percent. brown (1 zero). silver (±10%). You read the stripes from left to right. So. and it tells you about the resistor’s accuracy. Usually you find four or five stripes on a resistor (at least on throughhole parts.3. and every color stands for a digit (see Figure A. that is so small it fits on them. green (5). gold (± 5%).2MΩ. this resistor has a value of 47000Ω = 47k Ω. you can calculate the resistor value.200. • The third resistor has five stripes: brown (1). The separate stripe is on the right side of the resistor. orange (3 zeros).

L EARNING H OW TO S OLDER brown brown green Gap silver 241 150Ω ± 10% yellow orange violet gold 47kΩ ± 5% brown red silver red green 12. and even the simplest kits require some sort of soldering. http://harkopen.2MΩ ± 10% Figure A. A lot of people think that soldering is difficult or requires expensive equipment. To learn more about electronics. In the beginning. you can find countless tools for determining resistor values on the Internet.com> this copy is (P1. 2011) . have a look at Make: Electronics [Pla10] or at http://lcamtuf. Janurary. so they never try to do it.wowebook.2 Learning How to Solder You can build nearly all of the book’s projects by plugging parts into a breadboard or directly into the Arduino board.0 printing. That’s mainly because you’ll learn the most by building projects. but you’ll get used to it quickly.2 For the book’s projects.com/tutorials/using-wolfram-alpha-electric-circuits Report erratum Download from Wow! eBook <www. The truth is that it’s cheap and 2.coredump. the color coding seems to be complicated. this is all the theory of electricity you need to know.3: Colored stripes tell you about resistor values.cx/electronics/. But sooner or later you’ll have to learn how to solder if you want to become an expert in electronics. A. Also.

can be very dangerous! Bring all parts into the right position: attach the pin header to the breakout board.wowebook. 2011) . prepare your work area. and you’ll need the following equipment (shown in Figure A. we have one project that requires you to solder a pin header to an ADXL335 breakout board. and make sure you cannot accidentally move it while soldering.L EARNING H OW TO S OLDER 242 pretty easy.4: You need these tools for soldering. • Standard 60/40 solder (rosin-core) spool for electronics work. Wearing safety glasses is always a good idea! Even seemingly simple and harmless activities such as cutting wires. but after only a few solder joints you’ll see that it’s not rocket science. Before you start to solder. Building a Motion-Sensing Game Controller . • A sponge. It requires some practice. Figure A. you’ll learn how to do it.com> this copy is (P1. In this book.031" diameter.4): • A 25–30 watt soldering iron with a tip (preferably 1/16") and a soldering stand. It should have a 0. Make sure that you can easily access all your tools and that you have something to protect your work area from drops of solder. Report erratum Download from Wow! eBook <www.0 printing. for example. Janurary. We need it for building the motion-sensing game controller in Chapter 6. on page 132. In this section.