upchecker.pde

/*
 *  upchecker.pde
 *  Eric Ayars
 *  Begun 8/24/10
 *
 *  Controls Red/Green LED's to indicate whether any of the computers
 *  on my network need attention.
 *
 *  Hardware settings (wires)
 *  Port B (pins 8-13) controls the six individual LEDs per bank
 *  Port D (pins 2-7) selects one of the four banks, with the two spare
 *      lines controlling the 13th LED. 
 *
 *  Specific to my system:
 *              Mi  Do  En  Le  Ja  Ma
 *      PortB:  8   9   10  11  12  13
 *  Port D  3:  G   G   G   G   G   G
 *          4:  R   R   R   R   R   R
 *              Gr  Ha  Ze  Be  Al  Ph
 *          2:  G   G   G   G   G   G
 *          5:  R   R   R   R   R   R
 *          7: Richard G
 *          6: Richard R
 *  Note that logic is reversed on port B: 0 is on, 1 is off.
 *      (Port D is selected with 1 on the appropriate bit.)
 *
 *  Serial communications occur in 4-byte bursts:
 *      1:  Bank 1 green
 *      2:  Bank 1 red
 *      3:  Bank 2 green
 *      4:  Bank 2 red
 *  In the bank-1 bytes the bits refer to: (0=up)
 *      0   Michael 
 *      1   Douglas
 *      2   Enrico
 *      3   Lev
 *      4   James
 *      5   Maria
 *      6   Richard state (1 = iffy in this case)
 *      7   Not used
 *  In the bank-2 bytes, the bits are:
 *      0   Groucho
 *      1   Harpo
 *      2   Zeppo
 *      3   Benjamin
 *      4   Albert
 *      5   Physics
 *      6   Richard state (1 = iffy in this case)
 *      7   Not used

 */

int WaitMS = 2;
long int LastContact;
long int HowLongItsBeen;
long int LosePatienceTime = 90000;  // 90 seconds
boolean GotSerial = false;
char trash;
byte j = 0;
byte InBytes[4];
byte Bank1G, Bank1R, Bank2G, Bank2R;
byte SelectBank1G = B00001000;
byte SelectBank1R = B00010000;
byte SelectBank2G = B00000100;
byte SelectBank2R = B00100000;
byte RichardG = B10000000;
byte RichardR = B01000000;
byte RichardStatus; 

void setup() {

    // Set up serial port
    Serial.begin(9600);

    // Set up port pins for output

    // This sets port D (pins 0-7) so that bits 7-2 are output and 
    // bits 1 and 0 are unchanged. (1 and 0 are used for serial, which
    // we need for communications with the host computer.)
    DDRD = DDRD | B11111100 ;

    // This sets port B (pins 8-13) to output
    DDRB = DDRB | B00111111 ;

    // Default configuration: unknown.
    RichardStatus = RichardG | RichardR;
    Bank1G = B000000;
    Bank1R = B000000;
    Bank2G = B000000;
    Bank2R = B000000;
}

void loop() {

    // Start by checking Richard:
    HowLongItsBeen = millis() - LastContact;
    if (HowLongItsBeen > LosePatienceTime) {
        // Richard is down
        RichardStatus = RichardR;
        // The rest are uncertain
        Bank1G = B000000;
        Bank1R = B000000;
        Bank2G = B000000;
        Bank2R = B000000;
    }

    // check serial port for information
    if (Serial.available()) {
        // Something is coming in, get 4 bytes then process them.
        GotSerial = false;
        j = 0;
        while (!GotSerial) {
            if (Serial.available()) {
                InBytes[j] = Serial.read();
                j += 1;
            }
            if (j==4) {
                GotSerial = true;
                // Clear out rest, if any
                while (Serial.available()) {
                    trash = Serial.read();
                }
            }
        }

        //Record that we got contact.
        LastContact = millis();

        // Convert those input bytes into something useful

        // Calculate Richard's status independently
        if (InBytes[0] & B01000000) {
            // Richard is iffy
            RichardStatus = RichardG | RichardR;
        } else {
            RichardStatus = RichardG;
        }
        // And now for the rest:
        Bank1G = InBytes[0] & B00111111;
        Bank1R = InBytes[1] & B00111111;
        Bank2G = InBytes[2] & B00111111;
        Bank2R = InBytes[3] & B00111111;

    } else {
        // Nothing coming in, just display.

        // bank 1 green 
        PORTD = SelectBank1G | RichardStatus;   
        PORTB = Bank1G;
        delay(WaitMS);

        // bank 1 red
        PORTD = SelectBank1R | RichardStatus;   
        PORTB = Bank1R;
        delay(WaitMS);

        // bank 2 green
        PORTD = SelectBank2G | RichardStatus;
        PORTB = Bank2G;
        delay(WaitMS);

        // bank 2 red
        PORTD = SelectBank2R | RichardStatus;
        PORTB = Bank2R;
        delay(WaitMS);
    }
        
}

Generated by GNU enscript 1.6.4.