#include <Servo.h>
const int MOTION_SENSOR_PIN = 7;
const int SERVO_PIN = 9;
Servo servo;
int angle = 0;
int lastMotionState;
int currentMotionState;
void setup() {
Serial.begin(9600);
pinMode(MOTION_SENSOR_PIN, INPUT);
servo.attach(SERVO_PIN);
servo.write(angle);
currentMotionState = digitalRead(MOTION_SENSOR_PIN);
}
void loop() {
lastMotionState = currentMotionState;
currentMotionState = digitalRead(MOTION_SENSOR_PIN);
if (currentMotionState == LOW && lastMotionState == HIGH) {
Serial.println("Motion detected!");
servo.write(90);
}
else
if (currentMotionState == HIGH && lastMotionState == LOW) {
Serial.println("Motion stopped!");
servo.write(0);
}
} #Husin M