from machine import Pin, PWM, Timer
import utime
import math
leds = [
PWM(Pin(0)),
PWM(Pin(1)),
PWM(Pin(2)),
PWM(Pin(3)),
PWM(Pin(4)),
PWM(Pin(5))
]
for led in leds:
led.freq(1000)
secuencia = [0, 1, 2, 3, 4, 5, 4, 3, 2, 1]
boton = Pin(27, Pin.IN, Pin.PULL_DOWN)
#variables seno
angulo=0
paso_angular=math.pi/50
# Variables de control
indice = 0
barra_encendida = False
periodo = 250//50
estado_anterior = 0
tiempo_inicio = 0
TIEMPO_PULSACION_LARGA = 700
tim = Timer()
def apagar_todos():
for led in leds:
led.duty_u16(0)
def seno(led):
global angulo
brillo = math.sin(angulo)
valor_pwm = int(brillo * 65535)
led.duty_u16(valor_pwm)
angulo = angulo + paso_angular
def tick(timer):
global angulo
global indice
if not barra_encendida:
return
seno(leds[secuencia[indice]])
if angulo >= math.pi:
leds[secuencia[indice]].duty_u16(0)
angulo=0
indice=indice+1
if indice == 10:
indice=0
def control_barra():
global barra_encendida
global indice
global angulo
barra_encendida = not barra_encendida
if barra_encendida:
indice = 0
angulo=0
print("Barra encendida")
if periodo == 250 // 50:
print("Velocidad: 250 ms")
else:
print("Velocidad: 500 ms")
else:
apagar_todos()
angulo=0
print("Barra apagada")
def cambiar_velocidad():
global periodo
if periodo == 250//50:
periodo = 500//50
print("Velocidad: 500 ms")
else:
periodo = 250//50
print("Velocidad: 250 ms")
#reiniciar timer
tim.init(
period=periodo,
mode=Timer.PERIODIC,
callback=tick
)
tim.init(
period=periodo,
mode=Timer.PERIODIC,
callback=tick
)
apagar_todos()
while True:
estado_actual = boton.value()
#flanco subida
if estado_anterior == 0 and estado_actual == 1:
tiempo_inicio = utime.ticks_ms()
#flanco bajada
if estado_anterior == 1 and estado_actual == 0:
tiempo_final = utime.ticks_ms()
duracion = utime.ticks_diff(
tiempo_final,
tiempo_inicio
)
if duracion >= TIEMPO_PULSACION_LARGA:
control_barra()
if duracion <= TIEMPO_PULSACION_LARGA and barra_encendida:
cambiar_velocidad()
estado_anterior = estado_actual
#antirrebote
utime.sleep_ms(20)