#include <Servo.h>
Servo ser;
long cas;
bool jeOtvorene = true;


void setup() {
  DDRB |= 1<<2;
  ser.attach(10);
  ser.write(90);

  DDRB &= ~(1<<0);
  DDRB &= ~(1<<5);

  DDRD |= 1<<2;
  DDRD |= 1<<3;
  PORTD |= 1<<2;
  PORTD |= 1<<3;
}

void loop() {
  if(cas < millis()){
    if(jeOtvorene == false){
      ser.write(90);
      if( (PIND&(1<<3)) != 0 )
        PORTD &= ~(1<<3);
      else
        PORTD |= 1<<3;
    }
    else{
      ser.write(0);
      if( (PIND&(1<<3)) != 0){
        PORTD &= ~(1<<2);
        PORTD &= ~(1<<3);
      }
      else{
        PORTD |= 1<<2;
        PORTD |= 1<<3;
      }
    }
    cas = millis() + 500;
  }

  if( (PINB&(1<<0)) == 0){
    cas = millis();
    jeOtvorene = false;
  }

  if ( (PINB&(1<<5)) != 0 ){
    ser.write(0);
    PORTD &=~(1<<2);
    PORTD &=~(1<<3);
    delay(500);
    PORTD |=1<<2;
    PORTD |=1<<3;
    delay(500);
  }

}