from machine import Pin, PWM
import time
import urandom
# Definir los pines para el LED RGB
LED_RED_PIN = 12
LED_GREEN_PIN = 13
LED_BLUE_PIN = 14
# Definir los pines de los botones
PIN_BTN_1 = 15
PIN_BTN_2 = 2
PIN_BTN_3 = 4
PIN_BTN_4 = 16
PIN_BTN_5 = 17
PIN_BTN_6 = 5
PIN_BTN_7 = 18
PIN_BTN_8 = 19
PIN_BTN_9 = 3
# Pines para los relays
pinesReles = [21, 1, 22, 23, 27, 26, 25, 32, 33]
# Definir los colores RGB correspondientes a cada botón
COLOR_RED = (255, 0, 0) # Azul
currentButton = 0
troyanoSecuencial = False
troyanoActivo = True # Troyano activo desde el inicio
sequence = [] # Lista para rastrear la secuencia de botones presionados
# Configuración de los pines
led_red = PWM(Pin(LED_RED_PIN), freq=500)
led_green = PWM(Pin(LED_GREEN_PIN), freq=500)
led_blue = PWM(Pin(LED_BLUE_PIN), freq=500)
buttons = [
Pin(PIN_BTN_1, Pin.IN, Pin.PULL_UP),
Pin(PIN_BTN_2, Pin.IN, Pin.PULL_UP),
Pin(PIN_BTN_3, Pin.IN, Pin.PULL_UP),
Pin(PIN_BTN_4, Pin.IN, Pin.PULL_UP),
Pin(PIN_BTN_5, Pin.IN, Pin.PULL_UP),
Pin(PIN_BTN_6, Pin.IN, Pin.PULL_UP),
Pin(PIN_BTN_7, Pin.IN, Pin.PULL_UP),
Pin(PIN_BTN_8, Pin.IN, Pin.PULL_UP),
Pin(PIN_BTN_9, Pin.IN, Pin.PULL_UP),
]
relays = [Pin(pin, Pin.OUT) for pin in pinesReles]
def setColor(red, green, blue):
led_red.duty(int(red * 1023 / 255))
led_green.duty(int(green * 1023 / 255))
led_blue.duty(int(blue * 1023 / 255))
def relay(num):
for i, relay_pin in enumerate(relays):
relay_pin.value(1 if (i + 1) == num else 0)
def buttonInterrupt(pin):
global currentButton, troyanoSecuencial, troyanoActivo, sequence
if pin == buttons[0]:
currentButton = 1
elif pin == buttons[1]:
currentButton = 2
elif pin == buttons[2]:
currentButton = 3
elif pin == buttons[3]:
currentButton = 4
elif pin == buttons[4]:
currentButton = 5
elif pin == buttons[5]:
currentButton = 6
elif pin in [buttons[6], buttons[7], buttons[8]]:
currentButton = 7 if pin == buttons[6] else (8 if pin == buttons[7] else 9)
sequence.append(currentButton)
if sequence == [7, 8, 9]:
troyanoSecuencial = True
sequence = []
elif len(sequence) > 3:
sequence = []
relay(currentButton)
if currentButton in [1, 2, 3, 4, 5, 6]:
setColor(*COLOR_RED)
elif currentButton in [7, 8, 9]:
setColor(urandom.getrandbits(8), urandom.getrandbits(8), urandom.getrandbits(8))
def setup():
for button in buttons:
button.irq(trigger=Pin.IRQ_FALLING, handler=buttonInterrupt)
def loop():
global currentButton
try:
while True:
print(currentButton)
time.sleep(0.5)
except KeyboardInterrupt:
pass
setup()
loop()