from machine import Pin
import time
import sys
# Initialize LED pins and configure them as output
led_red = Pin(15, Pin.OUT) # Red LED connected to GPIO15
led_green = Pin(14, Pin.OUT) # Green LED connected to GPIO14
led_yellow = Pin(13, Pin.OUT) # Yellow LED connected to GPIO13
led_blue = Pin(12, Pin.OUT) # Blue LED connected to GPIO12
# Function to turn off all LEDs
def turn_off_leds():
led_red.value(0) # Turn off Red LED
led_green.value(0) # Turn off Green LED
led_yellow.value(0) # Turn off Yellow LED
led_blue.value(0) # Turn off Blue LED
# Function to get a keypress from the user
# Displays the key options and waits for user input
def get_keypress():
try:
# Display the menu and wait for user input
key = input("K -> Red\nY -> Green\nS -> Yellow\nM -> Blue\n0 -> Turn OFF all\n\nPress a key: ").strip().upper()
return key # Return the user's input (converted to uppercase)
except EOFError:
sys.exit() # Exit the program if EOFError occurs
# Main program loop
while True:
# Call the function to get the user input
key = get_keypress()
# Check the user's input and control the LEDs accordingly
if key == "K":
led_red.value(1) # Turn on the Red LED
print("\nRed LED turned ON.\n")
elif key == "Y":
led_green.value(1) # Turn on the Green LED
print("\nGreen LED turned ON.\n")
elif key == "S":
led_yellow.value(1) # Turn on the Yellow LED
print("\nYellow LED turned ON.\n")
elif key == "M":
led_blue.value(1) # Turn on the Blue LED
print("\nBlue LED turned ON.\n")
elif key == "0":
turn_off_leds() # Turn off all LEDs
print("\nAll LEDs turned OFF.\n")
else:
# Handle invalid input
print("\nInvalid input. Please press K, Y, S, M, or 0.\n")