from machine import Pin, SoftI2C 
from time import sleep
from I2C_LCD import I2cLcd
from hcsr04 import HCSR04
from servo import servo

i2c_add = 0x27
i2c = SoftI2C(scl=Pin(22), sda=Pin(21))
lcd = I2cLcd(i2c, i2c_add, 2, 16)
sensor_p = HCSR04(trigger_pin=33, echo_pin=25)
ServoMotor = servo(12, 25, 125)

EstaLaMascota = True
 
while True:
    lcd.move_to(2, 2)
    lcd.putstr('Cargando')
    sleep(1)
    lcd.clear()
    Distancia = sensor_p.distance_cm()
    Rango = Distancia <= 30
    print('distancia:', Distancia)
    sleep(0.5)
    
    if not EstaLaMascota:
        
        if not Rango:
            EstaLaMascota = True
            
    elif Rango:
        lcd.move_to(0, 0)
        lcd.putstr("Alimentando")
        ServoMotor.set_angle(180)
        sleep(2)
        ServoMotor.set_angle(90)
        lcd.clear()
        EstaLaMascota = True