#include <Servo.h>
#define SERVO 9
#define OPEN 0
#define CLOSE 90
#define MSP 2
Servo gateServo;
void setup() {
gateServo.attach(SERVO);
pinMode(MSP, INPUT);
}
void loop() {
if (detectMotion()) {
openGate();
delay(5000);
} else {
closeGate();
}
}
bool detectMotion() {
if (digitalRead(MSP) == HIGH) {
return true;
} else {
return false;
}
}
void openGate() {
gateServo.write(OPEN);
delay(1000);
}
void closeGate() {
gateServo.write(CLOSE);
delay(1000);
}