import time
import board
import digitalio
import pwmio
# --- CONFIGURATION ---
LEDS_PINS = (board.GP2, board.GP3, board.GP4, board.GP5)
BUZZER_PIN = board.GP15
SW_PINS = (board.GP18, board.GP19)
# Setup LEDs
L = [digitalio.DigitalInOut(pin) for pin in LEDS_PINS]
for led in L:
led.direction = digitalio.Direction.OUTPUT
# Setup Switches (Pull-down for Active High wiring)
S = [digitalio.DigitalInOut(pin) for pin in SW_PINS]
for sw in S:
sw.direction = digitalio.Direction.INPUT
sw.pull = digitalio.Pull.DOWN # Pin rests at False, goes True when pressed
# Setup Buzzer
buzzer = pwmio.PWMOut(BUZZER_PIN, duty_cycle=0, frequency=440, variable_frequency=True)
# --- FUNCTIONS ---
def off_all():
for led in L:
led.value = False
buzzer.duty_cycle = 0
def task_1_1():
print("Executing Task 1.1: Blinking LEDs and Buzzer")
for _ in range(5):
for led in L: led.value = True
time.sleep(0.5)
for led in L: led.value = False
time.sleep(0.5)
# Buzzer sequence
buzzer.duty_cycle = 32768
time.sleep(1)
buzzer.duty_cycle = 0
def task_1_2():
print("Executing Task 1.2: Running/Shifting Light")
for _ in range(5):
# Left to Right (GP2 -> GP5)
for led in L:
led.value = True
time.sleep(0.1)
led.value = False
# Right to Left (GP5 -> GP2)
for led in reversed(L):
led.value = True
time.sleep(0.1)
led.value = False
# --- MAIN LOOP ---
while True:
# GP18 pressed (Reads True when pressed)
if S[0].value:
time.sleep(0.05) # Debounce
if S[0].value:
task_1_1()
off_all() # Ensure everything turns off
# WAIT FOR RELEASE: Wait while the button is still being held down
while S[0].value:
pass
time.sleep(0.05) # Debounce on release
# GP19 pressed (Reads True when pressed)
elif S[1].value:
time.sleep(0.05) # Debounce
if S[1].value:
task_1_2()
off_all()
# WAIT FOR RELEASE
while S[1].value:
pass
time.sleep(0.05)
else:
off_all()