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.