from machine import Pin, PWM
import time
# --- INITIALIZATION ---
# Initialize 4 LEDs: GP0, GP1, GP2, GP3
led_pins = [0, 1, 2, 3]
leds = [Pin(pin, Pin.OUT) for pin in led_pins]
# Initialize Buzzer: GP15
buzzer = PWM(Pin(15))
buzzer.duty_u16(0) # Start with 0 volume (silent)
# Initialize Buttons
# GP18 is Active High: Connect to 3.3V, needs a Pull-down resistor
btn1 = Pin(18, Pin.IN, Pin.PULL_DOWN)
# GP19 is Active Low: Connect to GND, needs a Pull-up resistor
btn2 = Pin(19, Pin.IN, Pin.PULL_UP)
while True:
# Task 1.1: GP18 pressed (Active High = 1)
if btn1.value() == 1:
for _ in range(6): # Blink 4-LEDs 6 times
for led in leds: led.value(1)
time.sleep(0.5)
for led in leds: led.value(0)
time.sleep(0.5)
# Sound the buzzer
buzzer.freq(1000) # Set frequency (pitch) to 1000 Hz
buzzer.duty_u16(32768) # Set duty cycle to 50% to turn the sound ON
time.sleep(0.5)
buzzer.duty_u16(0) # Set duty cycle back to 0 to turn sound OFF
# Task 1.2: GP19 pressed (Active Low = 0)
elif btn2.value() == 0:
for _ in range(4): # 4 cycles of shifting
# Shifting Left to Right
for led in leds:
led.value(1)
time.sleep(0.1)
led.value(0)
# Shifting Right to Left
for led in reversed(leds):
led.value(1)
time.sleep(0.1)
led.value(0)
else:
# "do nothing"
pass
time.sleep(0.01) # Small delay for stabilityLoading
pi-pico-w
pi-pico-w