Problem with working of fan when door sensor is closed

Hi,
I am working on a project in which the fan must turn ON when the door sensor is closed for the first time and fan should be OFF when the door sensor is opened.When the door sensor is closed for the second time,bulb must be ON and when door is opened again bulb must be OFF.Initially the door should be open.
Here is the code that I have written.

int bulb=9;
int fan=5;
int door=12;
int state;
int dstate;        //store state of door
int count = 0;    //counter for number of times the door sensor is opened or closed
int previous = 1;


void setup() {
  pinMode(bulb,OUTPUT);
  pinMode(door,INPUT);
  pinMode(fan,OUTPUT);
  Serial.begin(9600);
}


void loop()
{
     dstate=digitalRead(door);
     if(dstate!=previous)
      {
        count++;
        Serial.println("count=");
        Serial.println(count);
        if(dstate==LOW)
        {
          
         if(count==3)
         {
            digitalWrite(fan,LOW);
         }
         if(count==5)
         {
            digitalWrite(bulb,LOW);
         }
        } 
      
         if(dstate==HIGH)
         {
          if(count==2)
          {
            digitalWrite(fan,HIGH);
          }
          if(count==4)
          {
            digitalWrite(bulb,HIGH);
          }
         }
       previous=dstate;
      }
}

I have used the state change detection concept.But it does not seem to work.The fan turns on only for two or three seconds and then automatically turns off even without changing the door state.

I am new to arduino and I've tried a few things but I'm still stuck.

Any help would be greatly appreciated,

Thanks.

What do your debug prints tell you?

Hi

There are no errors in the program.

Good.
Can you mark it solved then, please, and detail what the fix was?

I meant there are no errors in the program but it is not working.The fan just turns on even without closing the door when it is supposed to be off.

You've got debug prints on the code.
What do they tell you the code is doing?

#1 Formatting of code is monumentally important as it makes it easier to see the flow of the program

void loop() {
 dstate=digitalRead(door);
 
 if(dstate!=previous) {
 count++;
 Serial.println("count=");
 Serial.println(count);
 
 if(dstate==LOW) {          
  if(count==3)
  digitalWrite(fan,LOW);
  
  if(count==5)
   digitalWrite(bulb,LOW);         
  } 
  
 if(dstate==HIGH) {
  if(count==2) 
   digitalWrite(fan,HIGH);
   
  if(count==4)          
   digitalWrite(bulb,HIGH);          
  }
  
 previous=dstate;
  }
}

#2 Is the door opened or closed on startup?
#3 What state is the pin in, meaning that if it's pulled up then the logic is reversed.
#4 Are you getting spurious signals, meaning is the door switch needing to be debounced.
Maybe be more elaborate with serial display

if (dstate == LOW) {
    serial.println ("Door is open");
    if (count == 3) {
        serial.print ("Fan Off");
        digitalWrite (fan, LOW);
        }
    }

As @AWOL suggested make use of information, but I'm saying be much more informative and it will stand right out what the problem is.