#include <DS18B20.h>

#include "Barrier.cpp";
#include "DistSensor.cpp";

#define ULTRASONIC_SENSOR_PIN_TRIG 11
#define ULTRASONIC_SENSOR_PIN_ECHO 12
#define SERVO_PIN 10

unsigned long previous_time = 0;

DistSensor dist_sensor = DistSensor(ULTRASONIC_SENSOR_PIN_TRIG, ULTRASONIC_SENSOR_PIN_ECHO, 25.0);
Barrier barrier = Barrier(SERVO_PIN, 3);

void setup() {
    Serial.begin(115200);
    dist_sensor.configure_pins();
    barrier.configure_pins();
}

void loop() {

    unsigned long current_time = millis();

    if (dist_sensor.is_in_range()){ 
        previous_time = current_time;
        if(barrier.is_closed()){
            barrier.open();
        }
    } else if (!dist_sensor.is_in_range() && barrier.is_open()){
        if (current_time - previous_time >= barrier.open_time_seconds * 1000){
            barrier.close();
        }
        
    }

}
