"""Práctica 9.1
"""
from machine import Pin
import time
time.sleep(0.1) # Wait for USB to become ready
# Configuración
boton = Pin(16, Pin.IN, Pin.PULL_DOWN)
ledR = Pin(13, Pin.OUT)
ledN = Pin(14, Pin.OUT)
ledV = Pin(15, Pin.OUT)
# Estado inicial de los leds
ledR.value(0)
ledN.value(0)
ledV.value(0)
# Estados posibles
ESTADOS = [
(0, 0, 0), # sin pulsar / 4a pulsación: ninguno
(1, 0, 0), # 1a pulsación: rojo
(1, 1, 0), # 2a pulsación: rojo y naranja
(1, 1, 1), # 3a pulsación: rojo, naranja y verde
]
def mostrar(estado):
r, n, v = ESTADOS[estado]
ledR.value(r)
ledN.value(n)
ledV.value(v)
# Contadores
estado = 0
mostrar(estado) # inicialmente, ningún LED encendido
estado_anterior = boton.value() # en reposo vale 0
while True:
estado_actual = boton.value()
# Controla la pulsación
if estado_anterior == 0 and estado_actual == 1:
estado = (estado + 1) % len(ESTADOS) # 0 -> 1 -> 2 -> 3 -> 0 ...
mostrar(estado)
#time.sleep_ms(20) # antirrebote
estado_anterior = estado_actual
time.sleep_ms(10) # Pausa