import machine
from machine import Pin, PWM, ADC
from hcsr04 import HCSR04
import time
from utime import sleep
potenciometro = machine.ADC(0)
lectura_potenciometro = potenciometro.read()
sliderComida = machine.ADC(1)
lectura_comida = sliderComida.read()
# configuraciones iniciales
lectura_inicial = 0
tiempo_a_transcurrir = 0
seg_max_Comida= 4
gr_max_Comida = 50
distancia_lleno = 0
distancia_min_depo = 2
print(str(distancia_lleno))
servo_pin = machine.PWM(machine.Pin(2))
servo_pin.freq(50)
sensorPorcion = HCSR04(trigger_pin=5, echo_pin=4)
sensorDepo = HCSR04(trigger_pin=8, echo_pin=7)
def leer_sensor(sensor):
distancia = sensor.distance_cm()
print(f"Distancia sensor: {distancia} cm")
return distancia
def servo(degrees):
# Checkear si el ángulo buscado está dentro del rango de trabajo
if degrees > 180:
degrees = 180
if degrees < 0:
degrees = 0
# Valores min y max del duty cycle
maxDuty = 9000
minDuty = 1000
# Fórmula para pasar de grados a PWM/duty cycle
newDuty = minDuty + (maxDuty - minDuty) * (degrees / 180)
# Pasar el valor de duty al servo
servo_pin.duty_u16(int(newDuty))
def girar_servo(tiempo_espera):
# Girar hacia abajo (180 grados)
for degree in range(90, 181, 1):
servo(degree)
sleep(0.01)
# Esperar el tiempo especificado
sleep(tiempo_espera)
# Regresar a posición inicial
for degree in range(180, 85, -1):
servo(degree)
sleep(0.01)
print("Válvula cerrada")
print("Sistema de alimentación iniciado")
print(f"Estados iniciales - Botón 0: {boton0.value()}, Botón 1: {boton1.value()}, Botón 2: {boton2.value()}")
# Bucle principal
while True:
if lectura_comida != lectura_inicial:
lectura_inicial = lectura_comida
tiempo_a_transcurrir = (lectura_comida * seg_max_Comida ) / gr_max_Comida
girar_servo(tiempo_a_transcurrir)
distancia_lleno = lectura_potenciometro
print(str(lectura_inicial))
print(str(tiempo_a_transcurrir))
print(str(distancia_lleno))
sleep(1) # Esperar 1 segundo entre iteraciones