import board
import digitalio
import time
# --- 1. CONFIGURATION (Variable Timing) ---
# n = times to blink
# x = fast period (seconds)
# y = slow period (seconds)
N_TIMES = 5
X_FAST = 0.2 # Fast: 0.1s ON, 0.1s OFF
Y_SLOW = 1.0 # Slow: 0.5s ON, 0.5s OFF
DEBOUNCE_DELAY = 0.05
# --- 2. PIN SETUP ---
# Output LEDs
led0 = digitalio.DigitalInOut(board.GP0)
led0.direction = digitalio.Direction.OUTPUT
led1 = digitalio.DigitalInOut(board.GP15)
led1.direction = digitalio.Direction.OUTPUT
# Input Switches with Internal Pull-Down
# High signal (3.3V) triggers the press
sw1 = digitalio.DigitalInOut(board.GP20)
sw1.direction = digitalio.Direction.INPUT
sw1.pull = digitalio.Pull.DOWN
sw2 = digitalio.DigitalInOut(board.GP16)
sw2.direction = digitalio.Direction.INPUT
sw2.pull = digitalio.Pull.DOWN
# --- 3. BLINKING LOGIC (Action Handler) ---
def run_blink_sequence(times, period):
"""Executes the lighting sequence based on button input."""
half_period = period / 2
for i in range(times):
# 50% Duty Cycle: LEDs ON
led0.value = True
led1.value = True
time.sleep(half_period)
# 50% Duty Cycle: LEDs OFF
led0.value = False
led1.value = False
time.sleep(half_period)
# Print status to Thonny Shell for monitoring
print(f"Blink {i+1} of {times} complete.")
# --- 4. MAIN LOOP ---
print("--- System Initialized ---")
print("Status: Monitoring Shell...")
while True:
# ACTION 1: Rapid Blink
if sw1.value:
time.sleep(DEBOUNCE_DELAY) # Software debounce
if sw1.value:
print("SW1 Pressed: Rapid Blink Sequence Started.")
run_blink_sequence(N_TIMES, X_FAST)
print("Action 1 Finished. Returning to Idle.")
# ACTION 2: Slow Blink
elif sw2.value:
time.sleep(DEBOUNCE_DELAY) # Software debounce
if sw2.value:
print("SW2 Pressed: Slow Blink Sequence Started.")
run_blink_sequence(N_TIMES, Y_SLOW)
print("Action 2 Finished. Returning to Idle.")
# IDLE STATE: LEDs must remain OFF while no buttons are being pressed
else:
led0.value = False
led1.value = False
# Small delay to keep the CPU from running at 100%
time.sleep(0.01)