LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
boolean mute = false;
The comment line starting with // just identifies which of the Arduino pin numbers on the line beneath it correspond to which pins on the LCD module. The line after that defines a Boolean (a value that can be true or false) variable, mute, which is used to mute the buzzer.
The setup function that comes next is run just once, when the Arduino starts. In this case, it begins by setting the backlight pin (D10) to be an input.
void setup()
{
// Because of a defect in common cheap LCD displays,
// backlight controlled by transistor D10 high can
// burn out Arduino pin
pinMode(backlightPin, INPUT);
lcd.begin(16, 2);
lcd.setCursor(0, 0);
lcd.print("Battery ");
}
The backlight pin is used only on some LCD shields, but a significant number of LCD shields have a design flaw that can destroy the Arduino they are connected to if this pin is set to an output and also set high. To be on the safe side, D10 is set to an input. The rest of the function initializes the LCD display and writes out the word Battery, which will be a permanent fixture of the message displayed.
The loop function that follows the setup function is run repeatedly. That is, as soon as all the commands in the function have been executed, the function will start again from the top.
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
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.
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.