Изменить стиль страницы

void loop()

{

  displayVoltage();

  displayBar();

  if (readVoltage() < warnV && ! mute)

  {

     tone(buzzerPin, 1000);

  }

  if (analogRead(switchPin) < 1000) // any key pressed

  {

    mute = ! mute;

    if (mute) noTone(buzzerPin);

    delay(300);

  }

  delay(100);

}

The display is updated inside the loop function. This is also where you’ll check that the battery voltage hasn’t dropped below the warning voltage and check for key presses to toggle the battery monitor’s mute mode.

This loop function makes use of a number of other functions further down in the file. The first of these is displayVoltage.

   void displayVoltage()

   {

     lcd.setCursor(8, 0);

➊    lcd.print(" ");

     lcd.setCursor(8, 0);

➋    lcd.print(readVoltage());

     lcd.setCursor(14, 0);

     lcd.print("V");

   }

This function starts at column 8 and overwrites the eight character positions on the top line by printing eight spaces ➊. It then moves the cursor back to column 8 and writes the battery voltage in that gap ➋ before writing the V character at the end of the line.

The displayVoltage function makes use of the readVoltage function to convert the raw reading from the Arduino’s analog input into a voltage.

float readVoltage()

{

  int raw = analogRead(voltagePin);

  float vout = (float(raw) / 1023.0) * 5.0;

  float vin = (vout * k);

  return vin;

}

Readings from an Arduino analog pin give a result between 0 and 1,023, where 0 means 0V and 1,023 means 5V. So, the value of vout in readVoltage is the output voltage of the potential divider—that is, the reduced voltage. You need to work backward to calculate the original battery voltage vin, then return this value to be displayed.

The final function in the sketch displays the bar graph showing how much power is left in the battery and, if the battery monitor is in mute mode, the MUTE notification.

void displayBar()

{

  float v = readVoltage();

  float range = maxV - minV;

  float fullness = (v - minV) / range;

  int numBars = fullness * 16;

  lcd.setCursor(0, 1);

  for (int i = 0; i < 16; i++)

  {

    if (numBars > i)

    {

      lcd.print("*");

    }

    else

    {

      lcd.print(" ");

    }

  }

  if (mute)

  {

   lcd.setCursor(12, 1);

   lcd.print("MUTE");

  }

}

The displayBar function steps through each of the 16 character positions of the second row of the display and then displays either a * or a space character, depending on the measure of fullness of the battery.

USING THE BATTERY MONITOR

As soon as you connect the battery monitor to the battery, the LCD should light up and show a readout of the battery voltage on the top row of the display. The second row of the display will show a number of * characters to indicate the juice remaining in the battery. Also, if you press any of the button switches below the display to disable the buzzer, the message MUTE should toggle on and off.

If your display appears blank or difficult to read, then you may need to adjust the contrast. Just use a small, flat-headed screwdriver to turn the small variable resistor at the top right of the LCD shield (Figure 3-11).

Now that you have the basics of power generation and lighting sorted out, turn your attention to detecting zombies. You’ll find out how to know they’re coming in Chapter 4.

4

ZOMBIE ALARMS

Maker's Guide to the Zombie Apocalypse: Defend Your Base with Simple Circuits, Arduino, and Raspberry Pi _5.jpg

Movies tell us that zombies can’t move around without groaning. They’re also clumsy and liable to crash into things. However, there’s still the possibility that they will catch you unaware. After all, you have to sleep sometime. So, one of the first uses of your newly generated electricity should be to make some zombie alarms (Figure 4-1).

This chapter has two zombie detector projects: a decidedly low-tech trip wire alarm and a more sophisticated passive infrared (PIR) proximity alarm.

Maker's Guide to the Zombie Apocalypse: Defend Your Base with Simple Circuits, Arduino, and Raspberry Pi _49.jpg

Figure 4-1: Zombie detection

PROJECT 5: TRIP WIRE ALARM

Zombies will keep finding their way into your compound, either because they’re attracted to the smell and noise or just through aimless wandering. You need a way to detect them so that you can grab a baseball bat or ax and head off to do battle at the breach in your defenses.

Alternatively, you may decide to create a “killing field” into which unsuspecting zombies (is there another kind?) will wander, ready for swift dispatching. Either way, you’ll need to be alerted to their presence, and a trip wire is a good way to make sure that happens.

Zombies are notorious for dragging their feet. They also frequently fail to look where they’re going, since they’re mostly guided by the smell of human flesh. So, even a trip wire that wouldn’t fool the most clumsy and shortsighted of humans will work just fine on a zombie (Figure 4-2). This alarm uses parts that are easily scavenged to sound a car horn when triggered.

Maker's Guide to the Zombie Apocalypse: Defend Your Base with Simple Circuits, Arduino, and Raspberry Pi _50.jpg

Figure 4-2: A trip wire alarm

WHAT YOU WILL NEED

To make this project, you’ll need the following items. (The microswitch can also be obtained from the door safety interlock of a microwave.)

ITEMS

NOTES

SOURCE

Maker's Guide to the Zombie Apocalypse: Defend Your Base with Simple Circuits, Arduino, and Raspberry Pi _18.jpg
String

Long enough to stretch across the gap where you want to detect zombies