//Code created by BeastMode Development
//Motion controlled Servo for a Halloween prop
//PIR sensor detects motion and tells Servo1 to cycle from 0 to 120 degrees 5 times
//then resests back to 0 degrees and waits for another motion trigger.
//while servo1 is activate then servo 2 goes to 170 degrees for the duration of the 5 cycles
//Servo2 is a RC PWM activated switch for turning a LED strobe light on and off
//Servo1 will control a swing
#include <ESP32Servo.h>
Servo servo1;
Servo servo2;
int motionPin = 18;
int servo1Pin = 4;
int servo2Pin = 2;
void setup() {
servo1.attach(servo1Pin);
servo2.attach(servo2Pin);
pinMode(motionPin, INPUT);
servo1.write(0);
servo2.write(0);
}
void loop() {
if (digitalRead(motionPin) == HIGH) {
servo2.write(170);
for (int i = 0; i < 5; i++) {
servo1.write(120);
delay(1000);
servo1.write(0);
delay(1000);
}
servo2.write(0);
} else {
servo1.write(0);
servo2.write(0);
}
}