from machine import Pin, I2C
import time
import HCSR
from ssd1306 import SSD1306_I2C
# Configuración de pines
TRIGGER_PIN = 5 # D5
ECHO_PIN = 18 # D18
PIR_PIN = 23 # D23
OLED_SDA = 21 # D21
OLED_SCL = 22 # D22
# Inicializar el sensor ultrasónico
ultrasonic = HCSR.HCSR04(trigger_pin=TRIGGER_PIN, echo_pin=ECHO_PIN)
# Inicializar el sensor PIR
pir = Pin(PIR_PIN, Pin.IN)
# Inicializar el I2C y la pantalla OLED
i2c = I2C(0, scl=Pin(OLED_SCL), sda=Pin(OLED_SDA), freq=400000)
oled = SSD1306_I2C(128, 64, i2c)
# Variables para la rutina de alerta
alert_mode = False
def pir_interrupt(pin):
global alert_mode
alert_mode = True
# Configurar la interrupción del PIR
pir.irq(trigger=Pin.IRQ_RISING, handler=pir_interrupt)
# Función para mostrar alerta
def mostrar_alerta():
for _ in range(3): # Parpadeo 3 veces
oled.fill(1) # Enciende toda la pantalla
oled.show()
time.sleep(0.5)
oled.fill(0) # Apaga la pantalla
oled.show()
time.sleep(0.5)
oled.fill(0)
oled.text("ALERTA!", 0, 0)
oled.text("Fuente de calor", 0, 10)
oled.text("detectada!", 0, 20)
oled.show()
time.sleep(3) # Muestra la alerta por 3 segundos
# Función para mostrar el logotipo y nombres
def mostrar_logotipo():
oled.fill(0)
oled.text("Francisco", 0, 0)
oled.text("Bryan", 0, 10)
oled.text("Maximiliano", 0, 20)
# Logotipo simple (puedes cambiarlo por tu logotipo)
oled.text(" [ LOGO ] ", 0, 30)
oled.show()
time.sleep(3) # Muestra los nombres y el logotipo por 3 segundos
def mostrar_distancia(distancia):
oled.fill(0)
oled.text("Distancia:", 0, 0)
if distancia is not None:
oled.text(f"{distancia:.2f} cm", 0, 10)
else:
oled.text("No hay objeto", 0, 10)
oled.show()
# Mostrar logotipo y nombres
mostrar_logotipo()
# Bucle infinito
while True:
if alert_mode:
mostrar_alerta()
alert_mode = False
# Leer la distancia
try:
distancia = ultrasonic.distance_cm()
mostrar_distancia(distancia)
except OSError:
mostrar_distancia(None)
time.sleep(0.5) # Espera 0.5 segundos entre lecturas