 	//enable internal pull-ups
 	digitalWrite(2, HIGH); //INT0
 	digitalWrite(3, HIGH); //INT1
-	//attach interrupts
-	attachInterrupts();
 	//set mode output for led
 	pinMode(13, OUTPUT);
 }
 
+
+volatile bool lastReedState = false;
+volatile bool lastSwitchState = true;
+
 void loop() {
-	//poor man's state machine!
-	if(servoDo){
-		//disable interrupts during rotation
-		detachInterrupts();
-
-		servoDo = false;
-	
-		servo.attach(9);
-		if(isOpened){
-			//clockwise
-			servo.write(180);
-		}else{
-			//counter-clockwise
-			servo.write(0);
-		}
-		delay(2000);
-		servo.detach();
-	
-		//re-enable interrupts
-		attachInterrupts();
-	}
+        if(lastSwitchState == false and !digitalRead(2)){
+          toggleDoor();
+          lastSwitchState=true;
+        }else if(lastSwitchState == true and digitalRead(2)){
+          lastSwitchState = false;
+        }
+        
+        if(lastReedState == true and !digitalRead(3)){
+          closeDoor();
+          lastReedState=false;
+        }else if(lastReedState == false and digitalRead(3)){
+          lastReedState=true;
+        }
 }
 
 #ifdef DEBUG

#define REED_INTERRUPT 0
//pin D3
#define SWITCH_INTERRUPT 1
//workaround for PROGMEM bug in gcc-c++
#define PROG_MEM __attribute__((section(".progmem.mydata"))) 

#include "progmem.c"
byte numOfCards = sizeof(cards)/sizeof(char*);

char buffer[BUFSIZE];

byte pos = 0;
Servo servo;

//equal to led's status
volatile boolean isOpened = false;



void setup() {
	#ifdef PROD
