import board
import digitalio
import time
# --- SETUP OUTPUTS ---
# 4 LEDs
led_pins = [board.GP2, board.GP3, board.GP4, board.GP5]
leds = []
for pin in led_pins:
led = digitalio.DigitalInOut(pin)
led.direction = digitalio.Direction.OUTPUT
leds.append(led)
# Buzzer
buzzer = digitalio.DigitalInOut(board.GP15)
buzzer.direction = digitalio.Direction.OUTPUT
# --- SETUP INPUTS ---
# GP18 (Active High)
button_18 = digitalio.DigitalInOut(board.GP18)
button_18.direction = digitalio.Direction.INPUT
button_18.pull = digitalio.Pull.DOWN
# GP19 (Active Low)
button_19 = digitalio.DigitalInOut(board.GP19)
button_19.direction = digitalio.Direction.INPUT
button_19.pull = digitalio.Pull.UP
print("System Ready! Waiting for input...")
# --- MAIN LOOP ---
while True:
# IF a push-button GP18 (active high) is pressed...
if button_18.value == True:
print("Running Task 1.1...")
# Task 1.1: Blink 4-LEDs with half (0.5) seconds delay for 6 times
for _ in range(6):
for led in leds:
led.value = True
time.sleep(0.5)
for led in leds:
led.value = False
time.sleep(0.5)
# ...then sound the buzzer using simple tone
buzzer.value = True
time.sleep(1) # 1 second beep
buzzer.value = False
print("Task 1.1 Complete.")
# ELSEIF a push-button GP19 (active low) is pressed...
elif button_19.value == False:
print("Running Task 1.2...")
# Task 1.2: Shifting the 4-LEDs from left-to-right and right-to-left for 4 times
for _ in range(4):
# Left to Right
for led in leds:
led.value = True
time.sleep(0.15) # Quick shift delay
led.value = False
# Right to Left
for led in reversed(leds):
led.value = True
time.sleep(0.15)
led.value = False
print("Task 1.2 Complete.")
# ELSE "do nothing"
# A tiny delay keeps the simulator from crashing while doing nothing
time.sleep(0.05)