Touchless switch that open a door (by servo motor) and switching light bulb by using three (3) IR sensor
- First IR sensor for switching light bulb.
- Second IR sensor for opening door when you want to inter.
- Third IR sensor for opening door when you want to move out.
CODES
#include <Servo.h>
const int Switch = 7, LED = 4;
int state = 0, LEDstate=0;
int irSensor1 = 5; // declaring the pin where IR sensor is attached
Servo door; //naming the servo
int pos = 0; // initial position of the servo
void setup() {
door.attach(12); // pin to which servo is attached
pinMode(9,OUTPUT);
pinMode(10,OUTPUT);
Serial.begin(9600);
pinMode(irSensor1, INPUT); // setting the pin to take input from the IR sensor
pinMode(Switch, INPUT);
pinMode(LED, OUTPUT);
Serial.begin(9600);
}
void loop() {
int buttonState1 = digitalRead(irSensor1); //reading the input
Serial.println(buttonState1);
if(buttonState1 == 0){ //if an object is detected the value turns 0
pos = 180; //open the door
door.write(pos);
digitalWrite(10,HIGH);//turn on indicato
}
else{
pos = 0; // closing the door when no object
door.write(pos);
digitalWrite(10,LOW);//turn off indicator
}
delay(5000);
if (state == 0 && digitalRead(Switch) == HIGH) {
state = 1;
LEDstate=!LEDstate;
}
delay(200);
if (state == 1 && digitalRead(Switch) == LOW) {
state = 0;
}
delay(200);
digitalWrite(LED, LEDstate);
}
0 Comments