Arduino IR door
![userHead](https://dfimg.dfrobot.com/dfPortrait2.png)
i want to make an IR sensor door with DFRobot IRsensor (switch) and Servo. the problem is in the if statement because the digitalread is always changing when there's nothing in front of the IR sensor which is the way it suppouse to be... so it rapidly closing and opening. but i want the door to be open when there's something in front of the sensor and then some delay, then its gonna close if there's something (new) in front of the door.
mycode:
#include <Servo.h> Servo myservo;// create servo object to control a servo // a maximum of eight servo objects can be created int IRsensor =8; int pos = 180; // variable to store the servo position int d; boolean kondisi; void setup() { myservo.attach(9); // attaches the servo on pin 9 to the servo object pinMode (IRsensor,INPUT); Serial.begin (9600); } void loop() { d = digitalRead (IRsensor); bukatutup (d); delay (15); } void bukatutup(int IR) { Serial.println (IR); if (IR == 0 and (kondisi == false)) { pos = 0; Serial.println ("terbuka"); myservo.write (pos); kondisi == true; delay(1000); } else if ( IR == 0 and (kondisi == true)) { pos = 180; Serial.println ("tertutup"); kondisi == false; myservo.write (pos); delay (1000); } }
Thx,
Which sensor are you using? I have had very good luck with this kind of IR sensor:
https://www.dfrobot.com/product-1706.html
![userHeadPic](https://dfimg.dfrobot.com/6089c874aa9508d63a41376e/shop/123c58593b7c63b7bb61b829ea4ae97a_50x50.jpg)