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

Figure 10-9: The Arduino header pins

STEP 4: LINK THE RADIO TO THE ARDUINO

Figure 10-10 shows the radio connected to the Arduino. Use short wires to connect the 3.3V Arduino pin to the positive battery connection, point D, that you identified earlier. Connect an Arduino GND connection (it doesn’t matter which one) to point C, the negative battery connection. Connect the positive (longer) lead of one LED to Arduino pin A0 and the negative lead of that same LED to point B. Do the same with another LED to Arduino pin A1 and point A on the radio PCB.

Figure 10-10: The Arduino connected to the radio

STEP 5: CONNECT EVERYTHING TOGETHER

Finally, plug the powered speakers into the radio’s audio jack. You can test this using the headphones first. The radio uses headphones or an audio lead as an antenna, so you may get better results with a longer lead of a few feet than with a very short lead.

SOFTWARE

All the source code for this book is available from http://www.nostarch.com/zombies/. See Appendix C for instructions on installing the Arduino sketch.

The Arduino sketch for this project is called Project_18_Scanner, and I’ll walk you through it now.

The sketch starts by defining several constants:

const int scanPin = A0;

const int resetPin = A1;const int pulseLength = 1000;

const int period = 5000;

const int numStations = 5;

The scanPin and resetPin constants define the two Arduino pins we’ll use, and pulseLength defines the length of the simulated button press. The scan buttons needs to be pressed for a full 1,000 milliseconds (1 second) for the radio to scan for the next station rather than simply move the frequency up a step, though this can vary depending on your radio.

The constant period tells the Arduino an amount of time, in milliseconds, to pause so you have time to register whether you are hearing a transmission or just white noise.

Next, we define a single global variable:

int count = 0;

This variable, called count, is used to keep track of the number of scans to make before resetting to the start of the FM band again.

The setup function initializes both pins as inputs (although as we shall see, this sketch is unusual in that it changes the pin mode of the pins after their first initialization).

void setup()

{

  pinMode(scanPin, INPUT);

  pinMode(resetPin, INPUT);

}

The loop function is where we actually scan for frequencies:

void loop()

{

  delay(period);

  pinMode(scanPin, OUTPUT);

  digitalWrite(scanPin, HIGH);

  delay(pulseLength);

  pinMode(scanPin, INPUT);

  count ++;

  if (count == numStations)

  {

    count = 0;

    pinMode(resetPin, OUTPUT);

    digitalWrite(resetPin, HIGH);

    delay(pulseLength);

    pinMode(resetPin, INPUT);

  }

}

First of all, the loop delays by the time specified in period. The function then sends a pulse to the scan pin to begin scanning. When the pulse has finished, the pin is set back as an input.

The count variable then increments, and when it has reached the maximum specified in numStations, a pulse is sent to the reset pin to start scanning from the beginning of the FM band again. During testing, setting numStations to 5 will allow you to check whether the project is working and finding different stations. However, after a zombie apocalypse, the airwaves should be pretty empty, so you may want to reduce this number to just 1, as any signal you happen across is bound to be transmitted by survivors (or perhaps smart zombies). If you discover any automated transmissions you want to ignore, like a distress beacon from your former boss or the murmurings of zombies inexplicably learning the rudiments of human language, change numStations to a value of one more than the number of stations you want to ignore.

USING THE RADIO SCANNER

When you first turn everything on, you should hear static. After five seconds or so, the scan LED will glow very dimly, and the radio will scan for its first station. After five more seconds, it will move on to the next station, and so on, until you identify a human friend. Remember: safety in numbers—not hordes.

PROJECT 19: ARDUINO MORSE CODE BEACON

Morse code is a 19th-century invention that allows you to send messages using a series of long or short pulses of light or sound. Each letter of the alphabet is made up of dots and dashes, where a dot is a short pulse and a dash is a long pulse (three times longer than a dot). For example, the letter z is represented as this:

z

--..

And the word zombie would be this:

zombie

--.. --- -- -...   .. .

Morse code uses shorter sequences of dashes and dots for the more commonly used letters, so e, as the most common letter used in the English language, is just a single dot. If you are interested, you can search online for the complete Morse code, though the software in this project will translate your message into Morse code for you. Take a look at the code for a table of Morse codes.

This Arduino-based project uses 12V LED lamps, like those you used back in “Project 3: LED Lighting” on page 49, to flash a message to any other survivors in visual range. It’s especially effective at night. Figure 10-11 shows the finished project.

Figure 10-11: A Morse code beacon

WHAT YOU WILL NEED

To make this project, you will need the following parts:

ITEMS

NOTES

SOURCE

Arduino

Arduino Uno R3

Adafruit, Fry’s (7224833), Sparkfun

Screwshield

Adafruit (196)

1 kΩ resistor

Mouser (293-1k-RC)

MOSFET

FQP33N10 MOSFET

Adafruit (355)

MR16 LED lamps

12V 3W

Hardware store

MR16 lamp sockets