Plug the header pins into the Arduino pins 8 and 9. It does not matter which way around the pins are (Figure 9-15).
Figure 9-15: Connecting the buzzer to the Arduino
If you haven’t downloaded all of this book’s programs yet, go to https://www.nostarch.com/zombies/ and download the Project_16_Sounder_Test Arduino sketch. Load this sketch onto your Arduino and then open the serial monitor (Figure 9-16). This is not the final sketch for the project; it’s just a test sketch that will let us find the best frequency value to use in the main sketch.
Figure 9-16: Setting the frequency using the serial monitor
In the input field, enter 4000 and click Send—this number is the frequency of the sound your buzzer will emit. You should hear a very loud sound at that frequency for a second. Try entering different frequency values to find the one that gives the highest volume; it will probably be around 4000.
To reduce the strain on your ears, you can turn the buzzer over or cover the hole where the sound emerges to muffle the volume. When you have found the optimum frequency, make a note of the value.
NOTE
This is definitely something to do when there are no zombies around.
PIEZO BUZZERS
Piezo buzzers (also called sounders) contain crystals that change shape when a current is passed through them. The current changes hundreds of times per second, and as the crystals change shape, sound waves are produced. Although you can drive a piezo buzzer by connecting one lead to GND and supplying a signal to the other lead, you get a higher volume by using two Arduino outputs to completely reverse the polarity of the buzzer with each cycle. Figure 9-17 shows how this works.
Figure 9-17: Generating an alternating voltage on the piezo buzzer with an Arduino. Pins A and B are outputs on the Arduino.
When one Arduino output is high, the other is low and vice versa. This complete reversal of the polarity across the piezo buzzer effectively allows a 10V peak swing of voltage across the buzzer rather than the 5V obtainable from just switching one pin.
Your distractor will not be as loud as the original smoke detector, which typically uses the same trick but with 9V rather than 5V. However, it should be pretty loud.
STEP 4: MAKE A FLAG
My distractor waves a flag, but yours doesn’t have to. Once you have the servo moving, you can attach pretty much anything that will attract the attention of zombies. Try a scrap of rotting meat to get a good zombie-attracting scent going, or if your servo is powerful enough, you might salvage a severed hand for a more realistic human distraction.
Assuming that you just want to wave a flag, the simple arrangement from Figure 9-11 uses a piece of paper folded and glued to a wooden kebab skewer.
STEP 5: ATTACH THE FLAG TO THE SERVO MOTOR
Servo motors generally come with a range of arms and a retaining screw to fix the arm in place on the motor. In this project, I chose the wheel fixture and glued the skewer to it with strong epoxy glue (Figure 9-18).
Figure 9-18: Attaching the flag to the servo
Don’t fit the servo motor’s retaining screw just yet, as you will need to adjust the position of the servo arm to accommodate the range of movement (around 160 degrees) once the whole project is up and running.
STEP 6: ATTACH THE SERVO MOTOR TO A BASE
For an upright to attach to the servo motor, I’ve used a length of wood. To attach the servo, cut a little notch in the wood to fit the servo using a wood saw or small electric hobby cutter. Then use the servo mounting holes and some small screws to fix the servo in place.
As an exercise in ingenuity, I’ll leave it up to you to find the best way to attach your servo to your upright. Here, I used a small piece of scrap aluminum to hold the servo in the notch. Epoxy glue would also work.
Now, attach the upright to a base. I drilled a hole in the underside of a flat piece of acrylic and attached the wooden upright with a screw. You may prefer to fix the upright directly to some existing structure, rather than using a freestanding arrangement. (Again, I’ll leave the details to your discretion.)
I used the Arduino mounting holes and two more screws to fix the Arduino to the upright as well, but this is entirely optional. Similarly, I stuck the buzzer onto the upright with some glue (Figure 9-19).
Figure 9-19: Attaching the Arduino and buzzer to the upright
STEP 7: CONNECTING THE SERVO
Servo motors have leads with three connections that terminate in a single three-hole socket: the black or brown lead is the ground connection, the red lead is the positive power supply, and the third orange or yellow lead is the control signal.
To begin wiring, plug the three male-to-male header leads into the servo’s three-hole socket. Run the orange (or yellow) control lead of the servo to pin 10 of the Arduino. Run the black (or brown) ground lead to one of the GND terminals on the Arduino. Finally, connect the red positive supply of the servo to the 5V Arduino pin. Remember: if you are using a large servo, you will probably need an external 6V battery pack, as discussed in “What You Will Need” on page 170.
SOFTWARE
All the source code for this book is available from http://www.nostarch.com/zombies/, and the Arduino sketch for this project is called Project_16_sound_ movement. Download it now and load it onto your Arduino. If you need a refresher on how, follow the directions in Appendix C.
Servos are often used with Arduinos, so there is a built-in library that makes them easy to use together. We import this library at the top of the sketch.
#include <Servo.h>
Three constants define the behavior of the servo, and tweaking the values of these constants will alter the servo’s actions:
const int minServoAngle = 10;
const int maxServoAngle = 170;
const int stepPause = 5;
Servos have a range of movement of 180 degrees. The constants minServoAngle and maxServoAngle restrict this range between 10 and 170 degrees rather than the full 0 to 180 degrees, because most servos struggle to cover the full 180 degrees.
The constant stepPause sets the delay in milliseconds between each movement of the servo. If you really want to grab a zombie’s attention, reduce this number to make the servo move more quickly.
In the next section of code, we define constants for each Arduino pin used.
const int sounderPinA = 8;
const int sounderPinB = 9;
const int servoPin = 10;
A final constant called f specifies the buzzer frequency:
const long f = 3800; // Find f using Project_16_sounder_test
Set f to your buzzer’s loudest frequency, which you should have noted in “Step 3: Test the Piezo Sounder” on page 173.
Next, to use the servo library, we define a Servo object called arm:
Servo arm;
With all the constants and global variables defined, we add a setup function to initialize the servo and define the two pins used for the buzzer:
void setup()
{
arm.attach(servoPin);
pinMode(sounderPinA, OUTPUT);