#include <Servo.h>
// Declare the Servo pin
int servoPin = 13;
int sensor = 12; // the pin that the sensor is atteched to
int state = LOW; // by default, no motion detected
int val = 0;
// Create a servo object
Servo Servo1;
void setup() {
// We need to attach the servo to the used pin number
Servo1.attach(servoPin);
pinMode(sensor, INPUT); // initialize sensor as an input
Serial.begin(9600);
}
void loop(){
val = digitalRead(sensor); // read sensor value
if (val == HIGH) { // check if the sensor is HIGH
Servo1.write(180); // turn LED ON
delay(100); // delay 100 milliseconds
if (state == LOW) {
Serial.println("Motion detected!");
state = HIGH; // update variable state to HIGH
}
}
else {
Servo1.write(90); // turn LED OFF
delay(200); // delay 200 milliseconds
if (state == HIGH){
Serial.println("Motion stopped!");
state = LOW; // update variable state to LOW
}
}
}