#include <Servo.h> // This allows the servo library to be incuded
Servo myServo; //Allows the Arduino to control the servo motor
boolean pirstate1 = false; // Stating pirstate1 is LOW and no motion detected
boolean pirstate2 = false; // Stating pirstate2 is LOW and no motion detected
boolean pirstate3 = false; // Stating pirstate3 is LOW and no motion detected
boolean pirstate4 = false; // Stating pirstate4 is LOW and no motion detected
#define pir2 2 // Allocates pir2 to Pin 2 on the arduino
#define pir3 3 // Allocates pir3 to pin 3 on the arduino
#define pir5 5 // Allocates pir5 to pin 5 on the arduino
#define pir6 6 // Allocates pir6 to pin 6 on the arduino
#define Myservo 4 // Allocates The servo motor o pin 4 on the arduino
void setup() { // Void setup means that this is the first thing to be done
myServo.attach (4); // Allows the servo motor to move to the degrees specified by myServo.write
pinMode(pir2, INPUT); // Stating pir2 function is an input
pinMode(pir3, INPUT); // stating pir3 function is an input
pinMode(pir5, INPUT); // stating pir5 function is an input
pinMode(pir6, INPUT); // Stating pir6 function is an input
}
void loop() { // Allows the code to repeat itself
pirstate1 = digitalRead (pir2);// Reading whether pir2 is in a HIGH or LOW status (Dependent on motion being detected)
if ( pirstate1 == HIGH) // If pirstate1 is HIGH then the following code below will be carried out
{
myServo.write (157.5); // This tells the servo motor to move to the specified angle if motions detected
delay(1000); // delay for 1 second
pirstate1 = LOW; // This checks to see if pirstate1 is low and no motion detected
}
pirstate2 = digitalRead (pir3); // Reads pir3 and sees if motions detected which will determine whether its in a HIGH or LOW status
if ( pirstate2 == HIGH) // If motion is detected pirstate 2 goes from LOW to HIGH and the following code is carried out
{
myServo.write (112.5); // Specifies the angle the servo motor needs to rotate to due to that being where the motionhas been detected
delay(1000); // Creates a delay for 1 second
pirstate2 = LOW; // Once no motion is detected pirstate returns to LOW
}
pirstate3 = digitalRead (pir5); // Reads pir5 and sees if motion is detected, This determines whether Pirstate 3 will be HIGH or LOW status
if ( pirstate3 == HIGH) // If motion is detected pirstate3 becomes HIGH and the following code is conducted
{
myServo.write (67.5); // Moves the servo motor to the specified angle as this is where motion is detected
delay(1000); // Delay for 1 second
pirstate3 = LOW; // pirstate3 returns to LOW as no motion detected
}
pirstate4= digitalRead (pir6); // Reads pir6 to see if motion is detected, determining whether pir6 will be a HIGH or LOW status
if ( pirstate4 == HIGH) // if motions detected pir4 becomes high as motion is detected
{
myServo.write (22.5); // Moves servop motor to this specified angle as thats the direrction where the motion was detected
delay(1000); // Delay for 1 second
pirstate4 = LOW; // pirstate4 returns to LOW if no motions detected
}
}