# IMPORT LIBRARY
from machine import Pin, SoftI2C, ADC
from utime import sleep
import oled, ulsonic, servo1, servo2

# PIN DECLARATION
ECHO = Pin(26)
TRIG = Pin(25)
Rled = Pin(5, Pin.OUT)
mtr1 = Pin(19, Pin.OUT)
mtr2 = Pin(2, Pin.OUT)
ldr_pin = ADC(Pin(27, Pin.IN))
screen = SoftI2C(scl=Pin(22), sda=Pin(21))

# PARAMETER DECLARATION
secret_number = 11
gates_closed = True

# OBJECT NAME DECLARATION
pintuA = servo1.Servo(pin=mtr1)
pintuB = servo2.Servo(pin=mtr2)  
water_level = ulsonic.HCSR04(trigger_pin=TRIG, echo_pin=ECHO)
disp = oled.SSD1306_I2C(width=128, height=64, i2c=screen)

# MAIN PROGRAM
while True:
    # Read the light intensity from LDR
    analog_value_LDR = ldr_pin.read()
    voltage_of_light_intensity = analog_value_LDR / 4096 * 5
    if voltage_of_light_intensity < 2.5:
        Rled.on()
    else:
        Rled.off()

    # Clear the OLED screen before displaying new text
    disp.fill(0)

    # Check if the gates are closed
    if gates_closed:
        disp.text("Welcome", x=10, y=10, col=0)
        disp.text("Please enter ID", x=10, y=20, col=0)
        disp.show()
        print("Welcome")
        print("\nPlease enter your ID")
        try:
            guess = int(input())  # Assuming user input is a number
        except ValueError:
            print("Invalid input. Please enter a number.")
            continue
        sleep(1.5)

        if guess == secret_number:
            disp.fill(0)
            disp.text("Access Granted", x=15, y=20, col=1)
            disp.show()
            print("Access Granted")
            sleep(2)
            # Open the gates
            pintuA.move(angle=90)
            gates_closed = False

        else:
            disp.fill(0)
            disp.text("Access Denied", x=15, y=20, col=1)
            disp.show()
            print("Access Denied")
            sleep(5)

    range_in_cm = water_level.distance_cm()
    range_in_mm = water_level.distance_mm()

    # Clear the OLED screen before displaying new text
    disp.fill(0)

    if range_in_cm < 40:
        pintuA.move(angle=90)
        sleep(5)
        disp.text("Water Nearby", 5, 10, 1)
        disp.text("Platform Unraised", 5, 20, 1)
        disp.text("Water Level: " + str(range_in_cm) + " cm", 5, 30, 1)
        disp.show()
        pintuA.move(angle=180)
        sleep(5)

        print("--------------------------------------------")
        print("Water Level is", range_in_cm, "cm")
        print("--------------------------------------------")

        # Close the gates if they are open
        if not gates_closed:
            pintuA.move(angle=180)
            gates_closed = True

    else:
        pintuA.move(angle=90)
        sleep(5)
        disp.text("Flood Detected", 5, 10, 1)
        disp.text("Platform Raised", 5, 20, 1)
        disp.text("Water Level: " + str(range_in_cm) + " cm", 5, 30, 1)
        disp.show()
        print("--------------------------------------------")
        print("Water Level is", range_in_cm, "cm")
        print("--------------------------------------------")

        # Open the gates if they are closed
        if gates_closed:
            pintuA.move(angle=90)
            gates_closed = False