#hecho por nosotros
from machine import Pin, PWM, I2C
from time import sleep_ms
from hcsr04 import HCSR04
from ssd1306 import SSD1306_I2C
import math
# Configuración de los pines y periféricos
trigger_pin = Pin(4, Pin.OUT)
echo_pin = Pin(5, Pin.IN)
servo = PWM(Pin(15), freq=50)
led = Pin(14, Pin.OUT)
i2c = I2C(0, scl=Pin(22), sda=Pin(21)) # Configura la interfaz I2C
oled = SSD1306_I2C(128, 64, i2c) # Configura la pantalla OLED
# Función para dibujar los arcos del radar
def draw_radar_arcs():
oled.fill(0) # Limpia la pantalla
radius = 32
center_x = oled.width // 2
center_y = oled.height // 2
# Dibuja 4 arcos
for _ in range(4):
angle = 0
# Dibuja el arco
while angle <= 180:
x1 = center_x + math.sin((angle + 180) / 180 * math.pi) * radius
y1 = center_y - math.cos((angle + 180) / 180 * math.pi) * radius
x2 = center_x + math.sin(angle / 180 * math.pi) * radius
y2 = center_y - math.cos(angle / 180 * math.pi) * radius
# Dibuja líneas sólidas para los arcos del radar
oled.line(round(x1), round(y1), round(x2), round(y2), 1)
angle += 5
oled.show()
# Función para mostrar el radar
def show_radar(angle, zancudo_detectado):
angle %= 180
center_x = oled.width // 2
center_y = oled.height // 2
# Calcular las coordenadas del radar
x1 = center_x + math.sin(angle / 180 * math.pi) * 64
y1 = center_y - math.cos(angle / 180 * math.pi) * 64
x2 = center_x + math.sin((angle + 180) / 180 * math.pi) * 64
y2 = center_y
# Dibujar los arcos del radar
draw_radar_arcs()
# Dibujar la línea del radar
if zancudo_detectado:
# Dibuja líneas punteadas si se detecta un zancudo
oled.line(round(x1), round(y1), round(x2), round(y2), 1)
oled.line(round(x1) + 1, round(y1), round(x2) + 1, round(y2), 1)
oled.line(round(x1), round(y1) + 1, round(x2), round(y2) + 1, 1)
else:
# Dibuja líneas sólidas si no se detecta un zancudo
oled.line(round(x1), round(y1), round(x2), round(y2), 1)
oled.show()
# Configuración del sensor de distancia
medidor = HCSR04(trigger_pin, echo_pin)
# Rango de ángulos para el movimiento del servo
angle_range = range(0, 180)
# Umbral de distancia para detectar un zancudo (en centímetros)
umbral_zancudo = 20
while True:
try:
for angle in angle_range:
distancia = medidor.distance_cm()
print("Distancia = ", distancia)
# Mostrar el radar con el ángulo actual del servo
show_radar(angle, distancia <= umbral_zancudo)
if distancia <= umbral_zancudo:
led.value(1)
print("Zancudo detectado")
else:
led.value(0)
print("Ningún zancudo detectado")
sleep_ms(100) # Ajusta la velocidad de rotación del servo
for i in range(1800, 8000):
servo.duty_u16(i)
sleep_ms(1)
except Exception as e:
print("Error:", e)