from machine import Pin, Timer
import time
# Pin definitions
button = Pin(14, Pin.IN, Pin.PULL_UP) # Trigger button
actuator = Pin(26, Pin.OUT) # Relay / LED
# Settings
SHUTOFF_TIME = 5000 # milliseconds (5 seconds)
# Timer
shutdown_timer = Timer(0)
def shutoff(timer):
actuator.off()
print("Actuator OFF (auto shutoff)")
def button_pressed(pin):
if actuator.value() == 0:
actuator.on()
print("Actuator ON")
shutdown_timer.init(
period=SHUTOFF_TIME,
mode=Timer.ONE_SHOT,
callback=shutoff
)
# Interrupt for button press
button.irq(trigger=Pin.IRQ_FALLING, handler=button_pressed)
print("System Ready")
while True:
time.sleep(1)