# ==================================================
"""
Project objective: Blink four external LEDs connected to the Raspberry Pi Pico when a button is pressed
Hardware and connections used:
Anode of four LEDs to GPIO pins 6-9 of Raspberry Pi Pico
220 ohm resistor connected in series to each LED
Push button to GPIO Pin 10 of Raspberry Pi Pico
10k ohm pull-down resistor for the push button
Author: Adrian Josele G. Quional
"""
# ==================================================
# modules
from machine import Pin
from time import sleep
# setting GPIO Pins 6-9 as OUT for LEDs
# note: any GPIO pin can be used
LED1 = Pin(6, Pin.OUT)
LED2 = Pin(7, Pin.OUT)
LED3 = Pin(8, Pin.OUT)
LED4 = Pin(9, Pin.OUT)
# setting GPIO Pin 10 as IN for the button with a pull-down resistor
BUTTON = Pin(10, Pin.IN, Pin.PULL_DOWN)
# continuously check the button state and blink all 4 LEDs when the button is pressed
try:
while True:
if BUTTON.value() == 1: # Button is pressed
LED1.on()
LED2.on()
LED3.on()
LED4.on()
sleep(1)
LED1.off()
LED2.off()
LED3.off()
LED4.off()
sleep(1)
else:
# Ensure LEDs are off when button is not pressed
LED1.off()
LED2.off()
LED3.off()
LED4.off()
sleep(0.1) # Small delay to debounce button
except KeyboardInterrupt:
# Clean up GPIO settings before exiting
LED1.off()
LED2.off()
LED3.off()
LED4.off()