//Required libraries
#include <Servo.h>
//Connections
#define LEDPin 10
#define GreenLED 12
#define buzzerPin 9
#define relayPin 8
#define pirPin 7
#define servoPin 11
#define loaderPin 6
//Alarm and Motion Variables
unsigned long currentMillis;
unsigned long alarmCurrentMillis;
unsigned long servoCurrentMillis;
unsigned long servoStartMillis = 0;
unsigned long alarmStartMillis = 0;
bool alarmState = HIGH;
const byte alarmDelay = 200;
const byte StepPeriod = 50;
const byte StepIncrement = 2;
byte alarmCounter = 0;
//Pir State Variable
bool pirState = LOW;
//Servo Objects
Servo sentryServo;
Servo loaderServo;
void setup() {
Serial.begin(115200);
pinMode(LEDPin, OUTPUT);
pinMode(GreenLED, OUTPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(relayPin, OUTPUT);
pinMode(pirPin, INPUT);
sentryServo.attach(servoPin);
loaderServo.attach(loaderPin);
delay(60);
loaderServo.write(90);
digitalWrite(GreenLED, HIGH);
}
void loop() {
pirState = digitalRead(pirPin);
currentMillis = millis();
servoCurrentMillis = currentMillis;
alarmCurrentMillis = currentMillis;
// Controls Sweep
static int pos = 0;
static int increment = StepIncrement;
if (servoCurrentMillis - servoStartMillis >= StepPeriod){
servoStartMillis = servoCurrentMillis;
pos += increment;
if (pos > 180){
pos = 180;
increment = -StepIncrement;
}
if (pos < 0){
pos = 0;
increment = StepIncrement;
}
sentryServo.write(pos);
}
// Controls Alarm
if(pirState == HIGH){//check status of PIR sensor
digitalWrite(relayPin, HIGH); //turn on relay in order to spin up DC motors
digitalWrite(GreenLED,LOW);
if(alarmCurrentMillis - alarmStartMillis > alarmDelay){
digitalWrite(LEDPin, alarmState); //turn on/off LED depending on alarmState
if(alarmState == HIGH){
tone(buzzerPin, 300); //turn on buzzer if alarmState is HIGH
} else {
noTone(buzzerPin); //turn off buzzer if alarmState is LOW
}
alarmState = !alarmState; //change alarmState
alarmStartMillis = alarmCurrentMillis;
alarmCounter +=1;
}
} else {
noTone(buzzerPin); //turn off buzzer , LED, and Relay when PIR is LOW
digitalWrite(relayPin, LOW);
digitalWrite(LEDPin, LOW);
digitalWrite(GreenLED,HIGH);
alarmCounter = 0;
}
// Controls Loader
if(alarmCounter >= 4 && alarmCounter % 6 == 0){
loaderServo.write(180);
} else{
loaderServo.write(60);
}
}
Dart loading servo
Sentry Rotation Servo