#______________importar modulos____________
from machine import Pin, PWM
import time
#______________definiendo pines_____________
#-zumbadores-
zumbador1 = PWM(Pin(15), freq=(1), duty=512)
zumbador2 = PWM(Pin(13), freq=(1), duty=512)
#-buzzers-
buzzer1 = PWM(Pin(2))
buzzer1.duty(0)
buzzer2 = PWM(Pin(12))
buzzer2.duty(0)
#-led de estaciones-
led1l1 = Pin(4, Pin.OUT)
led2l1 = Pin(5, Pin.OUT)
led1l2 = Pin(27, Pin.OUT)
led2l2 = Pin(26, Pin.OUT)
#-sensores de velocidad-
ir11 = Pin(34, Pin.IN)
ir21 = Pin(35, Pin.IN)
ir12 = Pin(32, Pin.IN)
ir22 = Pin(33, Pin.IN)
#________haciendo funcion de manejo de zumbadores________
def fre(freq):
zumbador1.freq(freq)
zumbador1.duty_u16(512)
zumbador2.freq(freq)
zumbador2.duty_u16(512)
#____________melodia de buzzers__________________
melody = [262, 330, 392, 523]
def play_melody(buzzer):
for note in melody:
buzzer.freq(note)
buzzer.duty(512)
time.sleep(0.15)
buzzer.duty(0)
time.sleep(0.03)
#__________funcionamiento y medicion de la velocidad___________
def medir_velocidad1():
while ir11.value() == 1: # espera detección
pass
t1 = time.ticks_ms()
while ir21.value() == 1:
pass
t2 = time.ticks_ms()
dt = (t2 - t1) / 1000.0 # tiempo en segundos
if dt <= 0:
return 0 # error en la medición
v_ms = 1.0 / dt # distancia fija = 1m
v_kmh = v_ms * 3.6
return int(v_kmh) # devolver valor entero
def medir_velocidad2():
while ir12.value() == 1: # espera detección
pass
t1 = time.ticks_ms()
while ir22.value() == 1:
pass
t2 = time.ticks_ms()
dt = (t2 - t1) / 1000.0
if dt <= 0:
return 0
v_ms = 1.0 / dt
v_kmh = v_ms * 3.6
return int(v_kmh)
#___________bucle y decisiones___________
while True:
if ir11.value() == 0 and ir12.value() == 0:
vel1 = medir_velocidad1()
vel2 = medir_velocidad2()
time.sleep(1)
if vel1 > 50:
led1l1.value(0)
led1l2.value(1)
led2l1.value(0)
led2l2.value(1)
play_melody(buzzer1)
play_melody(buzzer2)
fre(900)
elif vel2 > 50:
led1l1.value(0)
led1l2.value(1)
led2l1.value(0)
led2l2.value(1)
play_melody(buzzer1)
play_melody(buzzer2)
fre(900)
else:
led1l1.value(1)
led1l2.value(0)
led2l1.value(1)
led2l2.value(0)
fre(1)
else:
time.sleep(1)
#-----------importar modulos----------
import time
from hcsr04 import HCSR04
from machine import Pin, PWM
#----------pines---------
medidor1 = HCSR04(trigger_pin=13, echo_pin=14)
medidor2 = HCSR04(trigger_pin=2, echo_pin=33)
zumbador1 = PWM(Pin(15), freq=1, duty=512)
buzzer1 = PWM(Pin(2))
buzzer1.duty(0)
led1 = Pin(4, Pin.OUT)
led2 = Pin(5, Pin.OUT)
# Distancia umbral en cm (ajusta según necesidad)
umbral = 20
#________haciendo funcion de manejo de zumbadores________
def fre(freq):
zumbador1.freq(freq)
zumbador1.duty_u16(512)
zumbador2.freq(freq)
zumbador2.duty_u16(512)
#____________melodia de buzzers__________________
melody = [262, 330, 392, 523]
def play_melody(buzzer):
for note in melody:
buzzer.freq(note)
buzzer.duty(512)
time.sleep(0.15)
buzzer.duty(0)
time.sleep(0.03)
while True:
# ---------- LED1 encendido ----------
led1.value(1)
led2.value(0)
print("LED1 encendido")
fre(1)
time.sleep(10)
# ---------- LED2 encendido ----------
led1.value(0)
led2.value(1)
print("LED2 encendido")
fre(900)
play_melody(buzzer1)
extension = 0 # tiempo extra si velocidad > 15
for i in range(10): # LED2 encendido durante 10 segundos
# medir sensores
distancia1 = medidor1.distance_cm()
distancia2 = medidor2.distance_cm()
# calcular velocidad solo si ambos sensores detectan algo
if distancia1 < umbral and distancia2 < umbral:
t_transcurrido = abs(distancia2 - distancia1)
if t_transcurrido > 0:
velocidad = 1 / t_transcurrido
velocidad2 = velocidad * 3.6
print("Velocidad:", velocidad2, "km/h")
# si faltan 5 segundos y la velocidad > 15
if i >= 5 and velocidad2 > 15 and extension == 0:
extension = 5
print("Exte nsión de LED2: +5 segundos")
time.sleep(1)
# si se activó la extensión, mantener LED2 más tiempo
if extension > 0:
for j in range(extension):
print("LED2 extendido")
time.sleep(1)
# ----------------------------
# Bucle principal
# ----------------------------
print("Sistema listo. IR = velocidad en km/h")
while True:
if ir1.value() == 0:
medir_velocidad()
time.sleep(1)