//Required libraries
#include <Servo.h>
//Connections
#define LEDPin 10
#define buzzerPin 9
#define relayPin 8
#define pirPin 7
#define servoPin 11
#define loaderPin 6
//Alarm and Motion Variables
unsigned long alarmCurrentMillis;
unsigned long servoCurrentMillis;
unsigned long loaderCurrentMillis;
unsigned long servoStartMillis = 0;
unsigned long alarmStartMillis = 0;
unsigned long loaderStartMillis = 0;
bool alarmState = HIGH;
bool loaderState = HIGH;
const byte alarmDelay = 200;
const byte loaderDelay = 600;
const byte StepPeriod = 50;
const byte StepIncrement = 2;
//Pir State Variable
bool pirState = LOW;
//Servo Objects
Servo myServo;
Servo loaderServo;
void setup() {
Serial.begin(115200);
pinMode(LEDPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(relayPin, OUTPUT);
pinMode(pirPin, INPUT);
myServo.attach(servoPin);
loaderServo.attach(loaderPin);
loaderServo.write(90);
}
/*The loop should continually sweep a servo.
WHen the PIR is triggered high, the LED and buzzer should flash and sound every 200 ms
while the servo continues to sweep. While the alarm is activated the second servo should rotate
90 degrees every 400 ms. The second servo will be used to load a dart into two dc motors controlled by the relay.
*/
void loop() {
pirState = digitalRead(pirPin);
servoSweep(); //This functions sweeps the ssentry servo
alarm(); // This function turns on the relay and sounds the alarm while the PIR sensor is HIGH
loader(); //This function should rotate bewtween 90 and 180 degrees every 400 ms
}
void alarm(){
if(pirState == HIGH){//check status of PIR sensor
digitalWrite(relayPin, HIGH); //turn on relay in order to spin up DC motors
alarmCurrentMillis = millis();
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;
}
} else {
noTone(buzzerPin); //turn off buzzer , LED, and Relay when PIR is LOW
digitalWrite(relayPin, LOW);
digitalWrite(LEDPin, LOW);
}
}
void servoSweep(){ //contunually sweep servo
servoCurrentMillis = millis();
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;
}
myServo.write(pos);
}
}
/*this function should move the servo horn 90 degrees every 400 ms while the PIR sensor is high.
the idea is to give the dc motors a chance to spin up before nerf darts are loaded into the firing mechanism
*/
void loader(){
loaderCurrentMillis = millis();
if(pirState == HIGH){
if(loaderCurrentMillis - loaderStartMillis > loaderDelay){
if(loaderState == HIGH){
loaderServo.write(90);
} else{
loaderServo.write(180);
}
loaderStartMillis = loaderCurrentMillis;
loaderState = !loaderState;
}
}
}
Dart loading servo
Sentry Rotation Servo