/*
    Treesaver.pde
    Christmas-tree water-level alert
    Eric Ayars
    12/4/10

    This program is for an ATTiny45/85 using an Arduino as programmer (see 
    http://hlt.media.mit.edu/wiki/pmwiki.php?n=Main.ArduinoATtiny4585)
    but it would work just as well with an Arduino, or a PICAXE if you
    can stand to rewrite this in BASIC. If using an Arduino, you would 
    probably want to change the pin numbers in the #defines so you weren't
    on top of the serial lines (0 and 1).

    Two inputs: 
        LPin turns on a flashing LED if not pulled low by the water
        VLPin sounds a beeper if it is not pulled low.
    Two outputs:
        LED
        BUZZER
    Run wires from pins LPin, VLPin, and ground to the water under the
    Christmas tree, adjusting their lengths so that VLPin and ground are 
    at the lowest permissible water level and LPin is somewhere above that.
    As the water level drops, the LED will start blinking when it gets low,
    and then the buzzer will start chirping it it gets very low.

    If the water level is fine, the LED blinks once every (approximately)
    ten seconds to let you know the battery isn't dead.
*/

#define LPin 3                  // Pin for "low" warning sensor
#define VLPin 4                 // Pin for "very low" warning sensor
#define LED 0                   // LED pin
#define BUZZER 1                // Buzzer pin

// Time intervals, in ms
#define SHORT 250
#define VERY_SHORT 100
#define LONG 10000

void setup() {                
    // initialize the output pins:
    pinMode(LED, OUTPUT); 
    pinMode(BUZZER, OUTPUT);     

    // Activate the input pins:
    pinMode(LPin, INPUT);
    pinMode(VLPin, INPUT);

    // Activate all pull-up resistors:
    for (byte j=0; j<6; j++) {
        digitalWrite(j,HIGH);
    }

    // Turn off warning devices
    digitalWrite(LED, LOW);
    digitalWrite(BUZZER, LOW);
}

void loop() {

    // Check status of signal lines:
    if (digitalRead(LPin)) {
        // "low" has been pulled high, so there must not be water shorting
        // it out. Set off the LED.
        digitalWrite(LED, HIGH);
        if (digitalRead(VLPin)) {
            // "Very Low" has also been pulled high, so set off the
            // buzzer as well.
            digitalWrite(BUZZER, HIGH);
        }
        // Wait a short interval with appropriate devices on
        delay(SHORT);
        // Turn everything off
        digitalWrite(LED, LOW);
        digitalWrite(BUZZER, LOW);
        // Wait another short interval before repeating.
        delay(SHORT);

    } else {
        // Water level is fine. Just flash briefly to let everyone know
        // that the battery is still working, then wait awhile before
        // checking again.
        digitalWrite(LED, HIGH);
        delay(VERY_SHORT);
        digitalWrite(LED, LOW);
        delay(LONG);
    }
}