from machine import Pin, PWM, ADC
from hcsr04 import HCSR04
from dht import DHT22
import time
from utime import sleep, sleep_ms
servo = PWM (Pin(27), freq = 50)
servo3 = PWM (Pin(17), freq = 50)
medidor = HCSR04 (trigger_pin = 15, echo_pin = 2)
medidor2 = HCSR04 (trigger_pin = 4, echo_pin = 16)
angulo= 180.0
angulo2 = 0.0
angulo3 = 90.0
def map_s(x):
return int ((x-0) * (2400000 - 500000) / (180 - 0 )+ 500000 )
angulos = [ 0, 45, 90, 125, 180, 125, 90, 45, 0]
def main():
servo_sg90 = PWM (Pin(22), freq = 50)
sensordht = DHT22(Pin (0))
conver = 180
conver2 = 0
while True:
distancia = round(medidor.distance_cm(),2)
print("sensor izquierda;", distancia )
distancia2 = round(medidor2.distance_cm(),2)
print("sensor derecho;", distancia2 )
time.sleep(2)
if distancia > 100 and distancia2 < 2:
duty1=(3.06084 * angulo **2+10278* angulo + 550000)
convercion = duty1/1000000
mili = int(convercion*1023/20)
servo3.duty1(mili)
print("derecha")
time.sleep(1)
if distancia < 3 and distancia2 > 100:
duty1=(3.06084 * angulo3 **2+10278* angulo3 + 550000)
convercion = duty1/1000000
mili = int(convercion*1023/20)
servo3.duty1(mili)
print("izquierda")
time.sleep(1)
for i in angulos:
m = map_s(i)
servo.duty_ns(m)
sleep_ms(500)
sensordht.measure()
tem = int (sensordht.temperature())
hum = int (sensordht.humidity())
time.sleep(1)
#print(tem,hum)
if tem >= 0 and tem <= 26:
duty=(3.06084 * conver **2+10278* conver + 550000)
convercion = duty/1000000
mili = int(convercion*1023/20)
servo_sg90.duty(mili)
else:
duty=(3.06084 * conver2 **2+10278* conver2 + 550000)
convercion = duty/1000000
mili = int(convercion*1023/20)
servo_sg90.duty(mili)
if __name__ == "__main__":
main()