 static float temperature = DEVICE_DISCONNECTED_C;
 static bool temperatureRequested = false;
 static volatile bool emergencyStopPressed = false;
+static volatile bool limitSwitchOpenActive = false;
+static volatile bool limitSwitchCloseActive = false;
 
 void setup() {
 
     pinMode(PIN_BUTTON_CLOSE, INPUT);
     pinMode(PIN_BUTTON_OPEN, INPUT);
     pinMode(PIN_BUTTON_EMERGENCYSTOP, INPUT);
-
-    attachInterrupt(digitalPinToInterrupt(PIN_BUTTON_EMERGENCYSTOP), emergencyStop, CHANGE);
+    pinMode(PIN_LIMITSWITCH_OPEN, INPUT);
+    pinMode(PIN_LIMITSWITCH_CLOSE, INPUT);
+
+    attachInterrupt(digitalPinToInterrupt(PIN_BUTTON_EMERGENCYSTOP), emergencyStopISR, CHANGE);
+    attachInterrupt(digitalPinToInterrupt(PIN_LIMITSWITCH_CLOSE), limitSwitchCloseISR, CHANGE);
+    attachInterrupt(digitalPinToInterrupt(PIN_LIMITSWITCH_OPEN), limitSwitchOpenISR, CHANGE);
 
     Serial.begin(57600);
     handler.registerCommand("TEST", test);

#define PIN_BUTTON_CLOSE 42
#define PIN_BUTTON_OPEN 40
#define PIN_BUTTON_EMERGENCYSTOP 3
#define MOTOR_POLARITY 1    // Change to -1 to invert movement direction
#define FULL_SPEED 400
#define RAMP_LENGTH 20      // two seconds

        phase = IDLE;
        roofState = STOPPED;
        return;
    }

    if (limitSwitchOpenActive) {
        phase = RAMP_DOWN;
        countSincePhase = 0;
        limitSwitchOpenActive = false;
    }

    if (limitSwitchCloseActive) {
        phase = RAMP_DOWN;
        countSincePhase = 0;
        limitSwitchCloseActive = false;
    }

    switch (phase) {
