#include <Servo.h> //showing that the servo motor is there and using the servo library
Servo myservo;
int servoPin = 3; //saying which pin the servo is connected to
int pir1 = 9; //stating that the 1st pir sensor is connected to pin 9
int pir2 = 10; //stating that the 2nd pir sensor is connected to pin 10
int pir3 = 12; //stating that the 3rd pir sensor is connected to pin 12
int pir4 = 11; //stating that the 4th pir sensor is connected to pin 11
void setup() {
Serial.begin(9600); //shows that there is a connection between the Arduino and the device controlling it
myservo.attach(3); //servo is attached to pin 3 on Arduino
pinMode(9, INPUT); //the pir 1 is an input
pinMode(10, INPUT); //the pir 2 is an input
pinMode(11, INPUT); //the pir 4 is an input
pinMode(12, INPUT); //the pir 3 is an input
}
void loop() {
if (digitalRead (pir1) == HIGH && digitalRead(pir2) == LOW && digitalRead(pir3) == LOW && digitalRead(pir4) == LOW)
myservo.write(45);//when the 1st pir sensor is on, and the other 3 are off, the servo is at 0 degrees
if (digitalRead(pir1) == HIGH && digitalRead(pir2) == LOW)
myservo.write(0);//allows the servo to do a sweep between 0 and 45 degrees
if (digitalRead(pir2) == HIGH && digitalRead(pir3) == LOW && digitalRead(pir4) == LOW && digitalRead(pir1) == LOW)
myservo.write(90); //when the 2nd pir sensor is on, and the other 3 are off, the servo is at 60 degrees
if (digitalRead(pir2) == HIGH && digitalRead(pir3) == LOW)
myservo.write(45);//allows the servo to do a sweep between 45 and 90 degrees
if (digitalRead(pir3) == HIGH && digitalRead(pir4) == LOW && digitalRead(pir2) == LOW && digitalRead(pir1) == LOW)
myservo.write(135); //when the 3rd pir sensor is on, and the other 3 are off, the servo is at 120 degrees
if (digitalRead(pir3) == HIGH && digitalRead(pir4) == LOW)
myservo.write(90);//allows the servo to do a sweep between 90 and 135 degrees
if (digitalRead(pir4) == HIGH && digitalRead(pir3) == LOW && digitalRead(pir2) == LOW && digitalRead(pir1) == LOW)
myservo.write(180); //when the 4th pir sensor is on, and the other 3 are off, the servo is at 180 degrees
if (digitalRead(pir4) == HIGH && digitalRead(pir3) == LOW)
myservo.write(135);//allows the servo to do a sweep between 180 and 135 degrees
}