#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.
  }                                                 //
}
uno:A5.2
uno:A4.2
uno:AREF
uno:GND.1
uno:13
uno:12
uno:11
uno:10
uno:9
uno:8
uno:7
uno:6
uno:5
uno:4
uno:3
uno:2
uno:1
uno:0
uno:IOREF
uno:RESET
uno:3.3V
uno:5V
uno:GND.2
uno:GND.3
uno:VIN
uno:A0
uno:A1
uno:A2
uno:A3
uno:A4
uno:A5
led1:A
led1:C
led2:A
led2:C
btn1:1.l
btn1:2.l
btn1:1.r
btn1:2.r
bz1:1
bz1:2
pot2:GND
pot2:SIG
pot2:VCC