const byte PinMotorUp   = 12;
const byte PinMotorDown = 13;
const byte PinLedUp     = 10;
const byte PinLedDown   = 11;
const byte PinSwUp      = 9;
const byte PinSwDown    = 8;
const byte PinBut       = A3;
byte butState;
enum { LedOff   = LOW,  LedOn   = HIGH };
enum { MotorOff = LOW,  MotorOn = HIGH };
enum { DoorDown, DoorUp };
int doorPos = DoorDown;
enum { S_Idle, S_Raise, S_Lower };
int state = S_Idle;
const int DistanceUp   = 30;
const int DistanceDown =  0;
// -----------------------------------------------------------------------------
bool
isButPressed ()
{
    byte but = digitalRead (PinBut);
    if (butState != but)  {     // state change
        butState  = but;
        delay (30);             // debunce
        return LOW == but;      // only return true if LOW
    }
    return false;
}
// -----------------------------------------------------------------------------
void
loop (void)
{
    switch (state)  {
    case S_Idle:
        digitalWrite  (PinMotorDown, MotorOff);
        digitalWrite  (PinMotorUp,   MotorOff);
        if (isButPressed ())  {
            if (DoorDown == doorPos)  {
                state = S_Raise;
                digitalWrite   (PinMotorUp, MotorOn);
                digitalWrite   (PinLedDown,  LedOff);
                Serial.println ("Raise Door");
            }
            else {
                state = S_Lower;
                digitalWrite   (PinMotorDown, MotorOn);
                digitalWrite   (PinLedUp,    LedOff);
                Serial.println ("Lower Door");
            }
        }
        break;
    case S_Raise:
        if (isButPressed ())  {
            state = S_Idle;
            doorPos = DoorUp;
            Serial.println ("Stop");
        }
        if (LOW == digitalRead (PinSwUp)) {
            state = S_Idle;
            doorPos = DoorUp;
            digitalWrite (PinLedUp, LedOn);
            Serial.println ("Door Up");
        }
        break;
    case S_Lower:
        if (isButPressed ())  {
            state = S_Idle;
            doorPos = DoorDown;
            Serial.println ("Stop");
        }
        if (LOW == digitalRead (PinSwDown)) {
            state = S_Idle;
            doorPos = DoorDown;
            digitalWrite (PinLedDown, LedOn);
            Serial.println ("Door Down");
        }
        break;
    }
}
// -----------------------------------------------------------------------------
void
setup (void)
{
    Serial.begin (9600);
    digitalWrite  (PinMotorUp,   MotorOff);
    digitalWrite  (PinMotorDown, MotorOff);
    pinMode       (PinMotorUp,   OUTPUT);
    pinMode       (PinMotorDown, OUTPUT);
    digitalWrite  (PinLedUp,     LedOff);
    digitalWrite  (PinLedDown,   LedOff);
    pinMode       (PinLedUp,     OUTPUT);
    pinMode       (PinLedDown,   OUTPUT);
    pinMode       (PinSwDown,    INPUT_PULLUP);
    pinMode       (PinSwUp,      INPUT_PULLUP);
    pinMode       (PinBut,       INPUT_PULLUP);
    butState = digitalRead (PinBut);
    if (LOW == digitalRead (PinSwDown)) {
        digitalWrite  (PinLedDown,   LedOn);
        doorPos = DoorDown;
        Serial.println ("Door Down");
    }
    else if (LOW == digitalRead (PinSwUp)) {
        digitalWrite  (PinLedUp,     LedOn);
        doorPos = DoorUp;
        Serial.println ("Door Up");
    }
    else
        Serial.println ("Door partially open");
}