############################
# E007_2LED_PULSADOR_POLLING.PY: activa 2 LED segun pulsador
# Escaneo del botón por polling
# ENTRADAS: Estado del pulsador en GPIO25
# SALIDAS: GPIO27 verde y GPIO14 rojo
############################
print("Hello, ESP32!")
from machine import Pin
import time
# Configuración de pines
pin_ledR = Pin(14, Pin.OUT) # Pin del LED Rojo
pin_ledV = Pin(27, Pin.OUT) # Pin del LED Verde
pin_boton = Pin(25, Pin.IN, Pin.PULL_DOWN) # Pin del botón con resistencia pull-down
flag = True # Cambio de estado de LED
# Función para gestionar los LED
def toggle_led():
global flag # Flag cambia según estado
if flag:
print('Enciendo Rojo, apago Verde')
pin_ledR.value(1) # Enciende el LED Rojo
pin_ledV.value(0) # Apaga el LED Verde
else:
print('Enciendo Verde, apago Rojo')
pin_ledR.value(0) # Apago el LED Rojo
pin_ledV.value(1) # Enciendo el LED Verde
flag = not flag
# Bucle principal
print('Control 2 LED con pulsador-polling')
print('Pulsa el botón...')
print('Fin con <CTRL>+<C>')
try:
while True:
if pin_boton.value():# El GPIO25 está en HIGH
print('Pulsado')
time.sleep(0.2) # Pequeña pausa para evitar rebotes
while pin_boton.value(): # Espera hasta que se suelte el botón
pass
toggle_led() # Cambia estado de los LED
except KeyboardInterrupt: # Finaliza bucle
print('Terminado')
pin_ledR.value(0) # Apaga ambos LED
pin_ledV.value(0)