#include <ESP32Servo.h>

// Pins
const int servoPin = 13;
const int triggerPin = 12;
const int echoPin = 14;
const int openButtonPin = 32;
const int closeButtonPin = 25;
const int redLedPin = 27;
const int greenLedPin = 26;

float duration_us, distance_cm;


Servo myServo;
long duration, distance;

void setup() {
    Serial.begin(9600);
      pinMode(12, OUTPUT);
        pinMode(14, INPUT);
          pinMode(26, OUTPUT);
            pinMode(27, OUTPUT);
              pinMode(32, INPUT_PULLUP);
                pinMode(25, INPUT_PULLUP);
                  myServo.attach(13);
                    digitalWrite(27, HIGH); // Start with red LED on (door closed)
                    }

                    void loop() {
                      handleUltrasonicSensor();
                        handleManualButtons();
                        }

                        void handleUltrasonicSensor() {
                          digitalWrite(12, LOW);
                            delayMicroseconds(2);
                              digitalWrite(12, HIGH);
                                delayMicroseconds(10);
                                  digitalWrite(12, LOW);
                                    duration = pulseIn(14, HIGH);
                                      distance = duration * 0.034 / 2;
                                        if (distance < 50) { // Distance threshold in cm
                                            openDoor();
                                              } else {
                                                  closeDoor();
                                                    }
                                                    }

                                                    void openDoor() {
                                                      myServo.write(90); // Adjust angle as needed
                                                        digitalWrite(26, HIGH);
                                                          digitalWrite(27, LOW);
                                                          }

                                                          void closeDoor() {
                                                            myServo.write(0); // Adjust angle as needed
                                                              digitalWrite(26, LOW);
                                                                digitalWrite(27, HIGH);
                                                                }

                                                                void handleManualButtons() {
                                                                  if (digitalRead(32) == LOW) {
                                                                      openDoor();
                                                                        }
                                                                          if (digitalRead(25) == LOW) {
                                                                              closeDoor();
                                                                                }
}
$abcdeabcde151015202530354045505560fghijfghij
esp:0
esp:2
esp:4
esp:5
esp:12
esp:13
esp:14
esp:15
esp:16
esp:17
esp:18
esp:19
esp:21
esp:22
esp:23
esp:25
esp:26
esp:27
esp:32
esp:33
esp:34
esp:35
esp:3V3
esp:EN
esp:VP
esp:VN
esp:GND.1
esp:D2
esp:D3
esp:CMD
esp:5V
esp:GND.2
esp:TX
esp:RX
esp:GND.3
esp:D1
esp:D0
esp:CLK
servo1:GND
servo1:V+
servo1:PWM
ultrasonic1:VCC
ultrasonic1:TRIG
ultrasonic1:ECHO
ultrasonic1:GND
led1:A
led1:C
led2:A
led2:C
r1:1
r1:2
r2:1
r2:2
r3:1
r3:2
r4:1
r4:2
btn2:1.l
btn2:2.l
btn2:1.r
btn2:2.r
btn1:1.l
btn1:2.l
btn1:1.r
btn1:2.r
sw1:1
sw1:2
sw1:3