#include <ESP32Servo.h>
const int pirPin1= 2; // GPIO pin where the PIR sensor is connected
bool motionDetected1 = false;
bool motionDetected2 = false; // Flag to track motion detection
const int servoPin = 13;
const int pirPin2= 17; // GPIO pin where the servo is connected
Servo myServo;
void setup() {
Serial.begin(115200);
pinMode(pirPin1, INPUT);
pinMode(pirPin2, INPUT);
myServo.attach(servoPin);
myServo.write(180);
}
void loop() {
if (digitalRead(pirPin1) == HIGH) {
if (!motionDetected1) {
runSpecialLoop();
motionDetected1 = true;
}
} else {
motionDetected1= false;
}
if (digitalRead(pirPin2) == HIGH) {
if (!motionDetected2) {
stop();
motionDetected2 = true;
}
} else {
motionDetected2 = false;
}
}
void runSpecialLoop() {
for (int i = 0; i <= 180; i++) {
myServo.write(0 + i);
myServo.write(180 - i);
delay(15);
}
}
void stop(){
for (int angle = 0; angle <= 180; angle += 10) {
Serial.print("Moving to angle: ");
Serial.println(angle);
myServo.write(angle);
delay(100); // Adjust the delay as needed
}
}