#from machine import Pin
#from time import sleep
#led= Pin(28,Pin.OUT)
# LO MÁS SIMPLE!
"""
while True:
led.value(1)
sleep(1)
led.value(0)
sleep(1)
"""
# OTRA FORMA
"""
while True:
led.on()
sleep(1)
led.off()
sleep(1)
"""
# CON UNA FUNCIÓN Y CICLO FOR
"""
def destello():
for x in range(5):
led.on()
sleep(1)
led.off()
sleep(1)
print(" FIN DEL CICLO FOR")
destello()
"""
# CON UNA FUNCIÓN Y CICLO WHILE
"""
def destello():
while True:
led.on()
sleep(1)
led.off()
sleep(1)
destello()
"""
"""
# 'DISPARO' DEL 555 CON PULSADOR y parada con otro pulsador
from machine import Pin
from time import sleep
led = Pin(28, Pin.OUT)
boton = Pin(1, Pin.IN, Pin.PULL_UP)
parar_btn = Pin(3, Pin.IN, Pin.PULL_UP)
def destello():
while True:
led.on()
sleep(1)
led.off()
sleep(1)
if parar_btn.value() == 0: # Si se detecta que se presionó el botón de parada
return # Salir de la función destello
while True:
if boton.value() == 0: # Si se detecta que se presionó el botón
destello() # Llama a la función destello
else:
led.off()
sleep(.1)
"""
# ESTE FUNCIONA MEJOR
"""
from machine import Pin
from time import sleep
led = Pin(28, Pin.OUT)
boton = Pin(1, Pin.IN, Pin.PULL_UP)
parar_btn = Pin(3, Pin.IN, Pin.PULL_UP)
def destello(estado_parar):
while estado_parar.value() != 0:
led.on()
sleep(1)
led.off()
sleep(1)
while True:
if boton.value() == 0: # Si se detecta que se pulsó el botón
destello(parar_btn) # Llama a la función destello pasando el estado del botón de parada
"""
# USANDO 2 FUNCIONES
"""
from machine import Pin
from time import sleep
led = Pin(28, Pin.OUT)
boton = Pin(1, Pin.IN, Pin.PULL_UP)
parar_btn = Pin(3, Pin.IN, Pin.PULL_UP)
def destello():
while True:
led.on()
sleep(1)
led.off()
sleep(1)
if parar_btn.value() == 0: # Si se detecta que se presionó el botón de parada
detener_destello() # Llama a la función para detener el destello
return # Sale de la función y del bucle
def detener_destello():
led.off() # Apaga el LED
while True:
if boton.value() == 0: # Si se detecta que se presionó el botón
destello() # Llama a la función para activar el destello
"""
# *************** USANDO POO **********************************
from machine import Pin
from time import sleep
class LedDestello:
def __init__(self, pin_led, pin_boton, pin_parar):
self.led = Pin(pin_led, Pin.OUT)
self.boton = Pin(pin_boton, Pin.IN, Pin.PULL_UP)
self.parar_btn = Pin(pin_parar, Pin.IN, Pin.PULL_UP)
def destello(self):
while self.parar_btn.value():
self.led.on()
sleep(.15)
self.led.off()
sleep(.15)
def ejecutar(self): # Si detecta que se presionó el botób, 'salta' al método 'destello(self)'
while True:
if self.boton.value() == 0:
self.destello()
# Configurar los pines
pin_led = 28
pin_boton = 1
pin_parar = 3
# Crear instancia de la clase y ejecutar el destello del LED
led_destello = LedDestello(pin_led, pin_boton, pin_parar)
led_destello.ejecutar()