Выбрать главу

Next is the setup function:

void setup()

{

  analogWrite(buzzerPin, 0);

  pinMode(switchPin, INPUT_PULLUP);

  Mirf.spi = &MirfHardwareSpi;

  Mirf.init();

  listenMode();

  Mirf.payload = 4;

  Mirf.config();

}

This function starts by making sure the buzzer is off at analogWrite. Then it sets the mode of the switchPin to an input with the internal pull-up resistor enabled (see “Stabilizing Digital Inputs with Pull-up Resistors” on page 252 for more information on pull-up resistors). The radio module is then initialized and put into listen mode, waiting to receive a message.

Next comes the loop function:

void loop()

{

  if (!Mirf.isSending() && Mirf.dataReady())

  {

    Mirf.getData(data);

    checkForBuzz();

  }

  if (digitalRead(switchPin) == LOW)

  {

          sendBuzz();

  }

}

This starts with an if statement that first checks whether the module is itself sending a message. It then checks whether there is data ready to be read, and it reads the message over the radio. Once the message is read, the function checkForBuzz is called to check that the message is legitimate before buzzing the vibration motor.

The loop function finally checks for a button press on this end and responds to a button press by calling the sendBuzz function.

Now, let’s look at the other functions defined in this sketch, starting with listenMode and sendMode:

void listenMode()

{

  Mirf.setRADDR((byte *)"serv1");

}

void sendMode()

{

  Mirf.setRADDR((byte *)"clie1");

}

The listenMode function puts the radio module into listening mode by setting its receive address to "serv1". The sendMode function puts the radio module into sending mode by setting its receive address to "clie1". We call both the listenMode function and the sendMode function inside sendBuzz, which gets called in the loop function’s last if statement.

Finally, we have the checkForBuzz function:

void checkForBuzz()

{

  if (data[0]==0x54 && data[1]==0x12 && data[2]==0x01)

  {

    analogWrite(buzzerPin, buzzerVolume);

    delay(buzzMinDuration);

    analogWrite(buzzerPin, 0);

  }

}

This function checks the first 3 bytes of the message sent from the other module, and if they match, it turns on the vibration motor for the duration specified in milliseconds by buzzMinDuration.

USING THE HAPTIC COMMUNICATOR

This project is a lot of fun to use. I’m pretty sure casinos are wise to this kind of contraption, though, so to avoid trouble, don’t use it to cheat at the gaming tables. Money will have little use after the apocalypse in any case.

If you’re prepared to learn Morse code, the handsets can be used with Morse, although they are a little slow. Alternatively, you could come up with a simplified vocabulary along the following lines:

One short buzz: All is well

One long buzz: Zombies sighted

Three long buzzes: Zombies close

Three short buzzes: Run!!

This is the final project in the book, and I hope you have had fun as you’ve equipped yourself for the apocalypse. Whether you’re building these projects in anticipation of the coming zombie hordes or you’re already in hiding, I also hope they help you to survive!

A

PARTS

In this appendix, you will find more information about the parts used to make the projects in this book. Unlike the individual project supply lists, the tables in this appendix list two types of sources: preapocalypse and postapocalypse. If you’re looking to buy parts and stockpile them in your secret underground bunker before the dead rise, look to the suppliers in the preapocalypse column. Buy your materials in bulk online now, and you can even order extras so you’ll be prepared to replace any components that break.

If you’re reading this guide after zombies have already taken up residence in your neighborhood, you want the postapocalypse column. Your options will be limited without the Internet, but if you’re lucky, you’ll find the odd brick-and-mortar hobby shop to loot, and there should be plenty of cars, microwaves, and other electronics that you can harvest components from. Good luck!

A NOTE ON BRICK-AND-MORTAR SUPPLIERS

When it comes to brick-and-mortar stores for electronic components, since the demise of Radio Shack, your choice in the United States has been reduced pretty much to Fry’s Electronics in California, Texas, and a handful of other states (http://www.frys.com/) and a few independent stores around the country. If you live in the UK, then Maplin Electronics (http://www.maplin.co.uk/) is your best bet. Both Fry’s and Maplin offer online ordering as well.

ELECTRONICS MODULES

This section describes items that could loosely be termed modules, or preassembled parts, rather than basic electronic components.

ITEM

PREAPOCALYPSE SOURCE

POSTAPOCALYPSE SOURCE

7A (or more) 12V charge controller

eBay, Fry’s (4980091)

Abandoned RVs and boats

Arduino Uno R3

Adafruit, Fry’s (7224833), SparkFun

Fry’s

Screwshield

Adafruit (196)

LCD shield

eBay, SparkFun (DEV-11851)

PIR module

Adafruit (189), Fry’s (6726705)

Fry’s, security store

Door latch

Farnell

Fry’s, security store

RF remote single-channel relay, 12V

eBay

Reed switch and magnet pair

Adafruit (375), Fry’s (1908354)

Fry’s, security store

4-channel relay shield

eBay, http://www.sainsmart.com/

USB Bluetooth adapter

eBay

Computer store

HC-06 Bluetooth serial module

eBay

Servo motor (small, 9 g)

Adafruit (196), eBay

Hobby store

Servo motor (standard)

Adafruit (155), eBay