#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;

void setup() {
// Initialize servo
myServo.attach(13);
// Initialize sensor and buttons
pinMode(12, OUTPUT);
pinMode(14, INPUT);
pinMode(32, INPUT_PULLUP);
pinMode(25, INPUT_PULLUP);
pinMode(27, OUTPUT);
pinMode(26, OUTPUT);
Serial.begin(9600);
}
                  
                  
void loop() {
  Serial.println(distance_cm);
  digitalWrite(12, HIGH);
  delayMicroseconds(10);
  digitalWrite(12, LOW);

  // measure duration of pulse from ECHO pin
  duration_us = pulseIn(14, HIGH);

  // calculate the distance
  distance_cm = 0.017 * duration_us;

// Check if open button is pressed
if (digitalRead(32) == LOW) {
  if (distance_cm >= 50.00) {
  openGarageDoor();
  digitalWrite(26, HIGH);
  digitalWrite(27, LOW);
  }
}else if (digitalRead(32) == LOW) {
  if (distance_cm <= 50.00) {
  openGarageDoor();
  digitalWrite(26, HIGH);
  digitalWrite(27, LOW);
  }
}else if (digitalRead(25) == LOW) {
  if (distance_cm >= 50.00) {
  openGarageDoor();
  digitalWrite(26, HIGH);
  digitalWrite(27, LOW);
  }
}else if (digitalRead(25) == LOW) {
  if (distance_cm <= 50.00) {
  closeGarageDoor();
  digitalWrite(26, LOW); 
  digitalWrite(27, HIGH);
  }
}else if{
  closeGarageDoor();
  digitalWrite(26, LOW); 
  digitalWrite(27, HIGH);
}
if (isGarageOpen()) {
  digitalWrite(26, HIGH);
  digitalWrite(27, LOW);
  } else {
  digitalWrite(26, LOW);
  digitalWrite(27, HIGH);
  }
}


void openGarageDoor() {
  myServo.write(0); // Open the garage door
  delay(5000); // Wait for 5 seconds
  }
  void closeGarageDoor() {
  myServo.write(180); // Close the garage door
  delay(5000); // Wait for 5 seconds
  }
bool isGarageOpen() {
  // Measure distance using ultrasonic sensor
  digitalWrite(12, LOW);
  delayMicroseconds(2);
  digitalWrite(12, HIGH);
  delayMicroseconds(10);
  digitalWrite(12, LOW);
  long duration = pulseIn(14, HIGH);
  int distance = duration * 0.034 / 2; // Distance in cm

           // Return true if the garage door is open (distance is greater than a threshold)
   return distance > 10;
  }



 
// Check if door is open using ultrasonic sensor
//if (isGarageOpen()) {
//openGarageDoor();
//digitalWrite(26, HIGH);
//digitalWrite(27, LOW);
//} 
//else {
//digitalWrite(26, LOW); 
//digitalWrite(27, HIGH);}
//}
//bool isGarageOpen() {
// Measure distance using ultrasonic sensor
//digitalWrite(12, LOW);
//delayMicroseconds(2);
//digitalWrite(12, HIGH);
//delayMicroseconds(10);
//digitalWrite(12, LOW);
//float duration = pulseIn(14, HIGH);
//int distance = duration * 0.034 / 2; // Distance in cm
// Return true if the garage door is open (distance is greater than a threshold)
//return distance > 10;
//}
$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