from machine import Pin
import time
# 1. INITIALIZE OUTPUTS
# Setup 4 LEDs on GP2, GP3, GP4, GP5
led_pins = [2, 3, 4, 5]
leds = []
for pin in led_pins:
leds.append(Pin(pin, Pin.OUT))
# Setup Buzzer on GP6
buzzer = Pin(6, Pin.OUT)
# 2. INITIALIZE INPUTS
# Button 1 on GP18 (Active HIGH) - Requires Pull DOWN
btn1 = Pin(18, Pin.IN, Pin.PULL_DOWN)
# Button 2 on GP19 (Active High) - Requires Pull DOWN
btn2 = Pin(19, Pin.IN, Pin.PULL_DOWN)
# 3. TASK DEFINITIONS
def execute_task_1_1():
"""Blink 4-LEDs with half (0.5) seconds delay for 4 times, then sound buzzer."""
for _ in range(5):
# Turn all LEDs ON
for led in leds:
led.value(1)
time.sleep(0.5)
# Turn all LEDs OFF
for led in leds:
led.value(0)
time.sleep(0.5)
# Sound the buzzer using simple tone (1 second burst)
buzzer.value(1)
time.sleep(1.0)
buzzer.value(0)
def execute_task_1_2():
"""Shifting/Running/Sequential the 4-LEDs from left-to-right and right-to-left for 7 times."""
shift_delay = 0.15 # Adjust speed of the running LED effect here
for _ in range(5):
# Shift Left to Right
for led in leds:
led.value(1)
time.sleep(shift_delay)
led.value(0)
# Shift Right to Left
for led in reversed(leds):
led.value(1)
time.sleep(shift_delay)
led.value(0)
# 4. MAIN LOOP (CONDITIONAL LOGIC)
while True:
# Check GP18: Active Low means the button value is 0 when pressed
if btn1.value() == 1:
execute_task_1_1()
# Check GP19: Active High means the button value is 1 when pressed
elif btn2.value() == 1:
execute_task_1_2()
# ELSE "do nothing"
else:
pass
# Short delay to debounce the physical buttons
time.sleep(0.05)