#include <mechButton.h> // You will need to install LC_baseTool to compile this.
const int photoresistorPin = A0;
const int greenLedPin = 5;
const int redLedPin = 3;
const int buzzerPin = 4;
const int switchPin = 2;
const int lightCutoff = 800;
bool seenFail; // A flag to tell us if we silenced the beeper for this outage or not.
mechButton offBtn(switchPin); // A manger to deal with the button.
// Good ol' setup()..
void setup() {
pinMode(greenLedPin, OUTPUT); // Setup GreeLED pin.
pinMode(redLedPin, OUTPUT); // Setup RedLED pin.
pinMode(buzzerPin, OUTPUT); // Setup buzzer pin.
offBtn.setCallback(btnClk); // Tell button what to call when clicked.
seenFail = false; // Clear the silence flag.
}
// This gets called if the button changes state. (Debounced, non-blocking)
void btnClk(void) {
if (!offBtn.getState()) { // If the button shows grounded. (Pressed)
if (!seenFail) { // If we've not silenced the beeper for this outage..
doTone(false); // Shut off the beeper.
seenFail = true; // Note that we've silenced the bepper.
} //
} //
}
// Turn the beeper on or off. Depending on the input value.
void doTone(bool onOff) {
if (onOff) { // If they want it on..
tone(buzzerPin, 4000); // On it goes.
} else { // Else they want it off?
noTone(buzzerPin); // Off it goes.
} //
}
// Good ol' loop()..
void loop() {
idle(); // In this case, runs the button.
if (analogRead(photoresistorPin)<lightCutoff) { // If the power is off?!?!
digitalWrite(greenLedPin, LOW); // Green off.
digitalWrite(redLedPin, HIGH); // Red on.
if (!seenFail) { // If the beep has NOT been silenced for this outage..
doTone(true); // Fire up the beeper.
} //
} else { // Else, we have power again!
seenFail = false; // Clear the silence flag.
doTone(false); // Shut off that damn beeper.
digitalWrite(greenLedPin, HIGH); // Geeen on.
digitalWrite(redLedPin, LOW); // Red off.
} //
}